Accessing Data between threads

  • Thread starter Thread starter Jon Slaughter
  • Start date Start date
J

Jon Slaughter

What concepts do I have to know to access data between two threads safely?

Right now I'm using a delegate method and Invoke to call a function in the
other thread that handles the data. This method seems a little bulky and I
read that it can have problems as it stalls.

for example, heres how I update the text to a textbox and the a status bar
label

this.Invoke(new TextDelegate(UpdateTextBox2), text);

this.Invoke(new TextDelegate(UpdateStatus), new object[] { "Finished" });



I call this function which is inside the forms class which is attached to an
event in another class which is controled by a thread.



i.e.



I have two classes, One is a the main gui form and the other is a
"computation" class that does things that take a long time.



In the computation class I have an event that the computation class calls
when it is finished with its computation to let the form know it can then
display the results. I then attach a function to this event that is in the
forms class so I can use the data in the form directly.



The methodology I use seems kinda bulky and probably has issues.



The project is located at

http://www.jonslaughter.com/files/SerialRow.rar



if anyone is interested.

-----



Also, something that I'm not sure about is when I attach a function to a
thread, when I call thread.Start(); thread.Stop(); Thread.Start(); Each time
on Thread.Start() does it re enter the function? (I'm using abort which I
read somewhere was bad)



Basically what I want call the function to compute and when it finished
display the result but the computation must be done in the background. I
also want to be able to "abort" the computation and restart it with
different parameters because the user might want to change something.



Anyways, any help would be appreciated. I guess I have to get a good book on
threading as I can't seem to find any coherent information the net.



Thanks,

Jon
 
Yes, you broadly need the Invoke approach (although .BeginInvoke can
help avoid deadlocks in some specific situations). In 2.0 you can make
life easier by using inline delegates, though:

string text = "something";
this.BeginInvoke((MethodInvoker) delegate {
txtSomething.Text = text;
// other code
});

For abort, *don't do this*. You need to use a safer approach. If using
BackgroundWorker, you can use the in-built cancel features;
alternatively a thread-sync'd bool would do. For a good write-up on
threading, Jon Skeet has a very good article at
http://www.yoda.arachsys.com/csharp/threads/ (note: several pages
deep).

Marc
 
Marc Gravell said:
Yes, you broadly need the Invoke approach (although .BeginInvoke can
help avoid deadlocks in some specific situations). In 2.0 you can make
life easier by using inline delegates, though:

string text = "something";
this.BeginInvoke((MethodInvoker) delegate {
txtSomething.Text = text;
// other code
});

For abort, *don't do this*. You need to use a safer approach. If using
BackgroundWorker, you can use the in-built cancel features;
alternatively a thread-sync'd bool would do. For a good write-up on
threading, Jon Skeet has a very good article at
http://www.yoda.arachsys.com/csharp/threads/ (note: several pages
deep).

Thanks. I'll read this and see what happens.
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Thread GC collection 6
Threading 8
Thread 4
Is this a BackgroundWorker? 3
InvokeRequired? (multithreading) 8
using application domain does not work as expected 4
timer/threading 10
Thread.MemoryBarrier() 6

Back
Top