Last post Nov 30, 2010 04:50 AM by Saravanan M
Nov 24, 2010 12:21 PM|bradwoody|LINK
I'm new to threading and have used it successfully, but limited. I can spawn a thread and have the main thread reference variables in the spawned thread, but I don't know how to allow the spawned thread to reference (and update) variables in the main thread.
Any example threading code I've seen on the web appears to be WAY more complicated than what I do, so I am unable to understand or integrate into my code.
Here is a quick example of how I use threading:
ThreadVariableSharing : System.Web.UI.Page
protected void Page_Load(object sender,
MyThreadClass workerObject =
Thread workerThread =
Session["workerObject"] = workerObject;
protected void Button1_Click(object sender,
MyThreadClass workerObject = (MyThreadClass)Session["workerObject"];
//this is how I currently update CommonVariable from the Main Thread
workerObject.CommonVariable = 5;
public int CommonVariable;
public void StartTheThread()
//this is where I'd like to update some shared variable from the Spawned Thread
//and have that effect 'visible' from the main thread
Any suggestions on how to allow the spawned thread to reference (and update) variables in the main thread?
Thanks in advance...
Nov 24, 2010 01:21 PM|sachingusain|LINK
Nov 29, 2010 12:09 PM|bradwoody|LINK
Hi sachingusain, thanks for the quick response, I really appreciate it.
I had already read the page you directed me to. It made sense, but didn't seem to directly answer my question.
I just realized how I should have described my needs better: My main thread (essentially the user's "session") will run until the user is through with the application. The spawned thread will be spawned and will run until it has done all its work, then
terminate. While the spawned thread is running, I'd like the main thread to be able to check the status of the spawned thread's progress (variables defined in the spawned thread which get updated with counts, boolean values, and string values). This works
as I have it coded, so long as the spawned thread is running. But before the thread is spawned and after it has terminated, those variables are undefined.
The best solution seems to be to define the variables in the main thread, not in the spawned thread. This way, after the spawned thread terminates, the status variables in the main thread still hold the spawned thread's final status values.
I hope that makes more sense. Any suggestions?
Nov 30, 2010 04:50 AM|Saravanan M|LINK
Look at the excellent sample given at MSDN site.
http://msdn.microsoft.com/en-us/library/3s8xdz5c.aspx. In sample, check for variable name
searching which is being used in main thread as well as in thread procedure.