473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing Data between threads

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


Nov 20 '06 #1
2 1632
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

Nov 20 '06 #2

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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
Nov 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
2
by: James Zhuo | last post by:
Accessing Items in a Collection works with the as suggested by Kevin and Chris (thx) But when i tried to use the same technic with databinding within the aspx file, it doesn't seem to work. This...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
2
by: Alan Kemp | last post by:
Hi, I have a problem that is half python, half design. I have a multithreaded network server working, each client request spawns a new thread which deals with that client for as long as it is...
2
by: Jon C | last post by:
Hi, I've written a multi-threaded web spider which stores search results in a Shared ArrayList. Since each thread can launch further threads, the problem I have is pausing my main sub until all...
3
by: Arv Carsen | last post by:
Im a beginner with C#, but as I try to access a textbox from a thread I get the error message : "An unhandled exception of type 'System.InvalidOperationException' occurred in...
2
by: Joel Moore | last post by:
I'm looking at my debugger right now and can clearly see that two threads have their execution pointer residing within the same Synclock block. One thread is executing a Monitor.Wait(). I know...
5
by: Hyun-jik Bae | last post by:
I got a curiosity while using CLR. How can it be done that accessing object data by CLR application is safe even while garbage collector is working? Is the GC thread running in realtime priority...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.