Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Making a C# program wait without blocking the GUI

c# wpf gui wait sleep system blocking gui

Best Answer BlackRabbit, 02 March 2015 - 08:21 PM

You need to introduce yourself to the MethodInvoker. Here's the tutorial.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 01 March 2015 - 06:54 AM

Hello!

 

In my program I have been using the 

System.Threading.Thread.Sleep();

method but this blocks all use of the program while the program waits.

 

Is a simple way to make the program wait before doing something but still have full control of the main GUI?

 

I have read about using another thread, but I am not sure how to use them.



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 01 March 2015 - 11:36 AM

Welcome aboard,

 

You could use a Timer instead, it will call your next method to execute after the time lapse you program it to, in the meanwhile your form will be active just fine.

 

In case you have heavy duty thread you can always call Application.DoEvents() to make sure the form keeps working correctly.



#3 MeekLogic

MeekLogic

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 177 posts

Posted 01 March 2015 - 11:52 AM

In the end you will need to learn about threading, and how the form and the class you are coding in is by default on the same thread. Threads work like lines, one at a time.

 

See this to create a new thread - http://www.csharp-ex...ate-new-thread/


Edited by logicPwn, 01 March 2015 - 11:53 AM.

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

#4 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 02 March 2015 - 11:30 AM

Thanks for the welcome!

 

LogicPwn that c# website is so good! - Easy to read and the examples are simple, thanks for finding it!

 

I think I will go off and try to learn and use a thread.



#5 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 02 March 2015 - 11:56 AM

I have used the thread method and it works but:

 

Also guys, how do you stop threads from crashing the program when you get the error message about object ownership?

 

Thank you!



#6 MeekLogic

MeekLogic

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 177 posts

Posted 02 March 2015 - 05:31 PM

I have used the thread method and it works but:

 

Also guys, how do you stop threads from crashing the program when you get the error message about object ownership?

 

Thank you!

 

 

So I'm not 100% sure what you are talking about but I believe you have ran into the problem that another thread cannot update the GUI. The objects in the GUI are owned by the form thread. We need to delegate a function to update the GUI which will run on the form thread. Hopefully I didn't lose you.

 

Basically, we need a function and a delegate to perform certain tasks.

private delegate void DelegatedFunctionD();

void DelegatedFunction()
{
    if (InvokeRequired)
    {
        BeginInvoke(new DelegatedFunctionD(DelegatedFunction));
        return;
    }
    // Add code here to update GUI that fails in another thread
}

Edited by logicPwn, 02 March 2015 - 09:12 PM.

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

#7 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 02 March 2015 - 08:21 PM   Best Answer

You need to introduce yourself to the MethodInvoker. Here's the tutorial.



#8 SickHeadPro

SickHeadPro

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 03 March 2015 - 06:31 AM

Yes I was talking about another thread not being able to update the GUI, I used the code you sent and the tutorial Black Rabbit sent as well. It all works now.

 

Thanks for the help guys! I will rate you up in this post!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download