kenetik
kenetik

Reputation: 350

Threading Structure

I wrote some C# code and in it I initiate a class by placing it in its own thread so it doesnt freeze up my GUI from where I initiate it:

This is from my Form Class:

    Execute_Recipe execute;
    execute = new Execute_Recipe(XCoordinatesList, YCoordinatesList, Zref, Voltref,
              widget, record, filename);
    Thread executethread = new Thread(new ThreadStart(execute.RunRecipe));

And then in my Execute Class I create a new class to record the data. This is from my execute class:

    record1 = new Record_Recipe(XCoordinateList1, YCoordinateList1, Zref1, Voltref1, filename1);

And finally in my record class I send the data to a new form. To be displayd. So in the end all of the sub classes I initiate are all created within this thread. I know the structure is absolutley crazy right now, the software is deep in its development stage, but here is my question:

How can I keep BOTH Form Classes in their own threads while having all the execution and recording of my procedure in its own thread?

Do I create 1 thread for my execution, 1 thread for my recording, and a backgrounworker for my displaying? and then talk between threads? (from what I understand talking between threads is not easy)

I am only an intermediate programmer at best and I thank you in advance if you able / willing to help with this problem.

Upvotes: 1

Views: 212

Answers (2)

parapura rajkumar
parapura rajkumar

Reputation: 24413

Talking between threads is very easy in C#

            yourform.BeginInvoke((MethodInvoker)delegate()
            {
                yourform.button.Text = "new label";
                //More stuff here
            });

kind of construct can be used from a worker thread to updated the UI thread. If you are creating lot of non UI threads that have smaller lifetime consider using a ThreadPool instead.

For simple ui updates also look at INotifyPropertyChanged that lets you updated data bound controls easily.

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26446

The thread you call Application.Run() on is the thread that must do all the GUI stuff. The Windows.Forms.Control base class has an Invoke() method that allows a delegate to be called whenever the GUI thread is idle (a Form is such a Control). There are quite a few other mechanisms in .NET to communicate between threads.

Communicating to a non-GUI thread is a bit getting used to, but there are quite a few options. An example of for instance using the WaitHandle is as follows:

  • receiving thread waits for a signal
  • sending thread writes data
  • sending thread sets signal
  • receiving thread reads data and performs the task

Usually one thread ("the main thread") leads and the others either wait for work, or are created, do the work and are destroyed afterwards.

I suggest you check the System.Threading namespace on MSDN and see what's available. Also http://www.albahari.com/threading/ seems to cover it all.

Upvotes: 0

Related Questions