Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#.net - How to alert program that the thread is finished (event driven)?

this is a snippet from my class:

public bool start()
{
   Thread startThread = new Thread(this.ThreadDealer);
   startThread.Start();
   return _start;
}

In ThreadDealer() I'm setting the boolean variable "_start" to false or true. What I need now but can't seem to figure out is an event to alert start() to execute its return statement when the ThreadDealer()-Thread has finished.

I tried something with an AutoResetEvent and .WaitOne() but since I have a GUI that just blocks everything and while it does what I need it to do (wait for the Thread to finish) it is useless if it blocks my GUI.

Any help would be much appreciated.

like image 408
unknownNewbie Avatar asked Apr 05 '11 11:04

unknownNewbie


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

What you want to do -- wait for the background thread in a method of the UI thread but still allow the UI to be responsive -- is not possible. You need to split your code into two parts: One executed before starting (or parallel to) the background thread and the other one running after the background thread has finished.

The easiest way is to use the BackgroundWorker class. It raises an event in the UI thread (RunWorkerCompleted) after its work is done. Here's an example:

public void start()
{
    var bw = new BackgroundWorker();

    // define the event handlers
    bw.DoWork += (sender, args) => {
        // do your lengthy stuff here -- this will happen in a separate thread
        ...
    };
    bw.RunWorkerCompleted += (sender, args) => {
        if (args.Error != null)  // if an exception occurred during DoWork,
            MessageBox.Show(args.Error.ToString());  // do your error handling here

        // Do whatever else you want to do after the work completed.
        // This happens in the main UI thread.
        ...
    };

    bw.RunWorkerAsync(); // starts the background worker

    // execution continues here in parallel to the background worker
}
like image 96
Heinzi Avatar answered Sep 22 '22 20:09

Heinzi


Just raise an event. It will run on the wrong thread so whatever event handler has to deal with that by marshaling the call if necessary to update any UI. By using Control.Begin/Invoke or Dispatcher.Begin/Invoke, depending what class library you use.

Or use the BackgroundWorker class, it does it automatically.

like image 29
Hans Passant Avatar answered Sep 19 '22 20:09

Hans Passant