Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Accessing Data between threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon Slaughter - 20 Nov 2006 20:04 GMT
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
Marc Gravell - 20 Nov 2006 22:13 GMT
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
Jon Slaughter - 20 Nov 2006 23:24 GMT
> Yes, you broadly need the Invoke approach (although .BeginInvoke can
> help avoid deadlocks in some specific situations). In 2.0 you can make
[quoted text clipped - 12 lines]
> http://www.yoda.arachsys.com/csharp/threads/ (note: several pages
> deep).

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.