locked
Threading - how can thread update a variable shared with the Main thread RRS feed

  • Question

  • User-721008300 posted

    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:

     

    public partial class ThreadVariableSharing : System.Web.UI.Page<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    {<o:p></o:p>

        protected void Page_Load(object sender, EventArgs e)<o:p></o:p>

        {<o:p></o:p>

            if (!IsPostBack)<o:p></o:p>

            {<o:p></o:p>

                MyThreadClass workerObject = new MyThreadClass();<o:p></o:p>

                Thread workerThread = new Thread(workerObject.StartTheThread);<o:p></o:p>

                workerThread.Start();<o:p></o:p>

    <o:p> </o:p>

                Session["workerObject"] = workerObject;<o:p></o:p>

            }<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

        <o:p></o:p>

        protected void Button1_Click(object sender, EventArgs e)<o:p></o:p>

        {<o:p></o:p>

            MyThreadClass workerObject = (MyThreadClass)Session["workerObject"];<o:p></o:p>

    <o:p> </o:p>

            //this is how I currently update CommonVariable from the Main Thread<o:p></o:p>

            workerObject.CommonVariable = 5;<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    class MyThreadClass<o:p></o:p>

    {<o:p></o:p>

        public int CommonVariable;<o:p></o:p>

    <o:p> </o:p>

        public void StartTheThread()<o:p></o:p>

        {<o:p></o:p>

            //this is where I'd like to update some shared variable from the Spawned Thread<o:p></o:p>

            //and have that effect 'visible' from the main thread<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    Any suggestions on how to allow the spawned thread to reference (and update) variables in the main thread?

    Thanks in advance...

    Brad

    Wednesday, November 24, 2010 12:21 PM

Answers

All replies

  • User-721008300 posted

    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?

    Thanks,

    Brad

    Monday, November 29, 2010 12:09 PM
  • User-1184423958 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 30, 2010 4:50 AM