Threading question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom B

    #1

    Threading question

    I've written the code below to try and figure out how threading works.
    My assumption is that when starting a new thread it would run at the same
    time as the original thread.
    Am I wrong?

    I've made a simple form with two text labels and a button. When I click the
    button, it creates a thread that updates one label, and the main code
    updates the other label.

    It appears that the created thread doesn't run until the main thread is
    done?!?

    Is my code wrong? Is my logic wrong? Am I an idiot?

    Thanks for taking a look


    private void button1_Click(o bject sender, System.EventArg s e)
    {

    ThreadStart ts=new ThreadStart(add Text);
    Thread t = new Thread(ts);
    t.Start();


    //for (int iLoop=0;iLoop<1 0;iLoop++)
    //{
    //lblWorking.Text ="";
    lblWorking.Text =DateTime.Now.T oString();
    lblWorking.Refr esh();
    //Thread.Sleep(10 00);
    //}
    }

    private void addText()
    {
    lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
    //MessageBox.Show ("In the thread");
    for (int iLoop=0;iLoop<1 0;iLoop++)
    {
    Thread.Sleep(30 0);
    lblText.Refresh ();
    }

    lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
    lblText.Refresh ();
    }



  • Michael Mayer

    #2
    Re: Threading question

    My advice to you is this:
    use a console application for your simple threading tests (and use
    Console.Writeli ne() for output). Windows GUI adds an extra level of
    complication. Once you understand threading, then add the GUI on top. The
    following two articles will help you:

    (this one is a 4 part article)
    http://msdn.microsoft.com/library/de...ms01232003.asp

    http://msdn.microsoft.com/msdnmag/is...g/default.aspx

    --
    Mike Mayer

    mike@mag37.com


    "Tom B" <shuckle@hotmai l.com> wrote in message
    news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=blue]
    > I've written the code below to try and figure out how threading works.
    > My assumption is that when starting a new thread it would run at the same
    > time as the original thread.
    > Am I wrong?
    >
    > I've made a simple form with two text labels and a button. When I click[/color]
    the[color=blue]
    > button, it creates a thread that updates one label, and the main code
    > updates the other label.
    >
    > It appears that the created thread doesn't run until the main thread is
    > done?!?
    >
    > Is my code wrong? Is my logic wrong? Am I an idiot?
    >
    > Thanks for taking a look
    >
    >
    > private void button1_Click(o bject sender, System.EventArg s e)
    > {
    >
    > ThreadStart ts=new ThreadStart(add Text);
    > Thread t = new Thread(ts);
    > t.Start();
    >
    >
    > //for (int iLoop=0;iLoop<1 0;iLoop++)
    > //{
    > //lblWorking.Text ="";
    > lblWorking.Text =DateTime.Now.T oString();
    > lblWorking.Refr esh();
    > //Thread.Sleep(10 00);
    > //}
    > }
    >
    > private void addText()
    > {
    > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
    > //MessageBox.Show ("In the thread");
    > for (int iLoop=0;iLoop<1 0;iLoop++)
    > {
    > Thread.Sleep(30 0);
    > lblText.Refresh ();
    > }
    >
    > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
    > lblText.Refresh ();
    > }
    >
    >
    >[/color]


    Comment

    • Lee Alexander

      #3
      Re: Threading question

      Well for one thing you shouldn't be calling methods or properties on a
      control directly from another thread. You need to call them via
      Control.Invoke. Apart from that it may *seem* that the thread isn't starting
      until the main thread has finished its bit of code but in reality it's a
      toss up as to what code executes first. On a SMP machine the spawned thread
      may get there first. Your example though will always probably show the main
      thread updating it's label first since the main thread has direct access to
      the control and the message queue whilst the other threads UI update will
      only happen when the main thread processes the message queue which will be
      after 'button1_Click' has executed.

      Regards
      Lee

      "Tom B" <shuckle@hotmai l.com> wrote in message
      news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > I've written the code below to try and figure out how threading works.
      > My assumption is that when starting a new thread it would run at the same
      > time as the original thread.
      > Am I wrong?
      >
      > I've made a simple form with two text labels and a button. When I click[/color]
      the[color=blue]
      > button, it creates a thread that updates one label, and the main code
      > updates the other label.
      >
      > It appears that the created thread doesn't run until the main thread is
      > done?!?
      >
      > Is my code wrong? Is my logic wrong? Am I an idiot?
      >
      > Thanks for taking a look
      >
      >
      > private void button1_Click(o bject sender, System.EventArg s e)
      > {
      >
      > ThreadStart ts=new ThreadStart(add Text);
      > Thread t = new Thread(ts);
      > t.Start();
      >
      >
      > //for (int iLoop=0;iLoop<1 0;iLoop++)
      > //{
      > //lblWorking.Text ="";
      > lblWorking.Text =DateTime.Now.T oString();
      > lblWorking.Refr esh();
      > //Thread.Sleep(10 00);
      > //}
      > }
      >
      > private void addText()
      > {
      > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
      > //MessageBox.Show ("In the thread");
      > for (int iLoop=0;iLoop<1 0;iLoop++)
      > {
      > Thread.Sleep(30 0);
      > lblText.Refresh ();
      > }
      >
      > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
      > lblText.Refresh ();
      > }
      >
      >
      >[/color]


      Comment

      • Tom B

        #4
        Re: Threading question

        Thanks, I'll read those two articles. The samples I'd seen were Console
        apps, I just figured they did that to minimize the code, didn't occur to me
        that it would actually make a difference.

        Again, thanks for the articles.

        Tom B

        "Michael Mayer" <mike@mag37.com > wrote in message
        news:OL7cF65eDH A.988@TK2MSFTNG P10.phx.gbl...[color=blue]
        > My advice to you is this:
        > use a console application for your simple threading tests (and use
        > Console.Writeli ne() for output). Windows GUI adds an extra level of
        > complication. Once you understand threading, then add the GUI on top.[/color]
        The[color=blue]
        > following two articles will help you:
        >
        > (this one is a 4 part article)
        >[/color]
        http://msdn.microsoft.com/library/de...ms01232003.asp[color=blue]
        >
        > http://msdn.microsoft.com/msdnmag/is...g/default.aspx
        >
        > --
        > Mike Mayer
        > http://www.mag37.com/csharp/
        > mike@mag37.com
        >
        >
        > "Tom B" <shuckle@hotmai l.com> wrote in message
        > news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=green]
        > > I've written the code below to try and figure out how threading works.
        > > My assumption is that when starting a new thread it would run at the[/color][/color]
        same[color=blue][color=green]
        > > time as the original thread.
        > > Am I wrong?
        > >
        > > I've made a simple form with two text labels and a button. When I click[/color]
        > the[color=green]
        > > button, it creates a thread that updates one label, and the main code
        > > updates the other label.
        > >
        > > It appears that the created thread doesn't run until the main thread is
        > > done?!?
        > >
        > > Is my code wrong? Is my logic wrong? Am I an idiot?
        > >
        > > Thanks for taking a look
        > >
        > >
        > > private void button1_Click(o bject sender, System.EventArg s e)
        > > {
        > >
        > > ThreadStart ts=new ThreadStart(add Text);
        > > Thread t = new Thread(ts);
        > > t.Start();
        > >
        > >
        > > //for (int iLoop=0;iLoop<1 0;iLoop++)
        > > //{
        > > //lblWorking.Text ="";
        > > lblWorking.Text =DateTime.Now.T oString();
        > > lblWorking.Refr esh();
        > > //Thread.Sleep(10 00);
        > > //}
        > > }
        > >
        > > private void addText()
        > > {
        > > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
        > > //MessageBox.Show ("In the thread");
        > > for (int iLoop=0;iLoop<1 0;iLoop++)
        > > {
        > > Thread.Sleep(30 0);
        > > lblText.Refresh ();
        > > }
        > >
        > > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
        > > lblText.Refresh ();
        > > }
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • 100

          #5
          Re: Threading question

          Hi Tom,
          When you start a new thread the new thread and the main thread do execute at
          the same time. How ever you have a couple of problems in the code:
          1. The main thread owns the form and all controls on it. Updating the form
          or its controls form outside the owning thred thread is wrong and has to be
          avoided. So the code like

          lblWorking.Text =DateTime.Now.T oString();
          lblWorking.Refr esh();

          in the worker thread is wrong.

          Insted, you should use Control.Invoke to start a method in the main thread's
          contex that can update the UI

          2. Looping in the main thread stops the message loop and no win messages are
          processed. So I'm not sure that calling Refresh method will get through
          until resuming the message loop. So the worker thread works, but the UI gets
          updated when the loop in the main thread is done.

          HTH
          B\rgds
          100


          "Tom B" <shuckle@hotmai l.com> wrote in message
          news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=blue]
          > I've written the code below to try and figure out how threading works.
          > My assumption is that when starting a new thread it would run at the same
          > time as the original thread.
          > Am I wrong?
          >
          > I've made a simple form with two text labels and a button. When I click[/color]
          the[color=blue]
          > button, it creates a thread that updates one label, and the main code
          > updates the other label.
          >
          > It appears that the created thread doesn't run until the main thread is
          > done?!?
          >
          > Is my code wrong? Is my logic wrong? Am I an idiot?
          >
          > Thanks for taking a look
          >
          >
          > private void button1_Click(o bject sender, System.EventArg s e)
          > {
          >
          > ThreadStart ts=new ThreadStart(add Text);
          > Thread t = new Thread(ts);
          > t.Start();
          >
          >
          > //for (int iLoop=0;iLoop<1 0;iLoop++)
          > //{
          > //lblWorking.Text ="";
          > lblWorking.Text =DateTime.Now.T oString();
          > lblWorking.Refr esh();
          > //Thread.Sleep(10 00);
          > //}
          > }
          >
          > private void addText()
          > {
          > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
          > //MessageBox.Show ("In the thread");
          > for (int iLoop=0;iLoop<1 0;iLoop++)
          > {
          > Thread.Sleep(30 0);
          > lblText.Refresh ();
          > }
          >
          > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
          > lblText.Refresh ();
          > }
          >
          >
          >[/color]


          Comment

          • Tom B

            #6
            Re: Threading question


            I've been reading the articles suggested by Michael Mayer, and now see the
            errors in my ways.
            Thanks for taking the time to answer.

            "Lee Alexander" <lee@No_Spam_Pl ease_Digita.com > wrote in message
            news:%23hAPH85e DHA.1820@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > Well for one thing you shouldn't be calling methods or properties on a
            > control directly from another thread. You need to call them via
            > Control.Invoke. Apart from that it may *seem* that the thread isn't[/color]
            starting[color=blue]
            > until the main thread has finished its bit of code but in reality it's a
            > toss up as to what code executes first. On a SMP machine the spawned[/color]
            thread[color=blue]
            > may get there first. Your example though will always probably show the[/color]
            main[color=blue]
            > thread updating it's label first since the main thread has direct access[/color]
            to[color=blue]
            > the control and the message queue whilst the other threads UI update will
            > only happen when the main thread processes the message queue which will be
            > after 'button1_Click' has executed.
            >
            > Regards
            > Lee
            >
            > "Tom B" <shuckle@hotmai l.com> wrote in message
            > news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=green]
            > > I've written the code below to try and figure out how threading works.
            > > My assumption is that when starting a new thread it would run at the[/color][/color]
            same[color=blue][color=green]
            > > time as the original thread.
            > > Am I wrong?
            > >
            > > I've made a simple form with two text labels and a button. When I click[/color]
            > the[color=green]
            > > button, it creates a thread that updates one label, and the main code
            > > updates the other label.
            > >
            > > It appears that the created thread doesn't run until the main thread is
            > > done?!?
            > >
            > > Is my code wrong? Is my logic wrong? Am I an idiot?
            > >
            > > Thanks for taking a look
            > >
            > >
            > > private void button1_Click(o bject sender, System.EventArg s e)
            > > {
            > >
            > > ThreadStart ts=new ThreadStart(add Text);
            > > Thread t = new Thread(ts);
            > > t.Start();
            > >
            > >
            > > //for (int iLoop=0;iLoop<1 0;iLoop++)
            > > //{
            > > //lblWorking.Text ="";
            > > lblWorking.Text =DateTime.Now.T oString();
            > > lblWorking.Refr esh();
            > > //Thread.Sleep(10 00);
            > > //}
            > > }
            > >
            > > private void addText()
            > > {
            > > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
            > > //MessageBox.Show ("In the thread");
            > > for (int iLoop=0;iLoop<1 0;iLoop++)
            > > {
            > > Thread.Sleep(30 0);
            > > lblText.Refresh ();
            > > }
            > >
            > > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
            > > lblText.Refresh ();
            > > }
            > >
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Tom B

              #7
              Re: Threading question

              I've been reading more stuff and have found the problems you mention.
              Thanks for taking the time to respond.


              "100" <100@100.com> wrote in message
              news:O3VlIG6eDH A.632@TK2MSFTNG P10.phx.gbl...[color=blue]
              > Hi Tom,
              > When you start a new thread the new thread and the main thread do execute[/color]
              at[color=blue]
              > the same time. How ever you have a couple of problems in the code:
              > 1. The main thread owns the form and all controls on it. Updating the form
              > or its controls form outside the owning thred thread is wrong and has to[/color]
              be[color=blue]
              > avoided. So the code like
              >
              > lblWorking.Text =DateTime.Now.T oString();
              > lblWorking.Refr esh();
              >
              > in the worker thread is wrong.
              >
              > Insted, you should use Control.Invoke to start a method in the main[/color]
              thread's[color=blue]
              > contex that can update the UI
              >
              > 2. Looping in the main thread stops the message loop and no win messages[/color]
              are[color=blue]
              > processed. So I'm not sure that calling Refresh method will get through
              > until resuming the message loop. So the worker thread works, but the UI[/color]
              gets[color=blue]
              > updated when the loop in the main thread is done.
              >
              > HTH
              > B\rgds
              > 100
              >
              >
              > "Tom B" <shuckle@hotmai l.com> wrote in message
              > news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=green]
              > > I've written the code below to try and figure out how threading works.
              > > My assumption is that when starting a new thread it would run at the[/color][/color]
              same[color=blue][color=green]
              > > time as the original thread.
              > > Am I wrong?
              > >
              > > I've made a simple form with two text labels and a button. When I click[/color]
              > the[color=green]
              > > button, it creates a thread that updates one label, and the main code
              > > updates the other label.
              > >
              > > It appears that the created thread doesn't run until the main thread is
              > > done?!?
              > >
              > > Is my code wrong? Is my logic wrong? Am I an idiot?
              > >
              > > Thanks for taking a look
              > >
              > >
              > > private void button1_Click(o bject sender, System.EventArg s e)
              > > {
              > >
              > > ThreadStart ts=new ThreadStart(add Text);
              > > Thread t = new Thread(ts);
              > > t.Start();
              > >
              > >
              > > //for (int iLoop=0;iLoop<1 0;iLoop++)
              > > //{
              > > //lblWorking.Text ="";
              > > lblWorking.Text =DateTime.Now.T oString();
              > > lblWorking.Refr esh();
              > > //Thread.Sleep(10 00);
              > > //}
              > > }
              > >
              > > private void addText()
              > > {
              > > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
              > > //MessageBox.Show ("In the thread");
              > > for (int iLoop=0;iLoop<1 0;iLoop++)
              > > {
              > > Thread.Sleep(30 0);
              > > lblText.Refresh ();
              > > }
              > >
              > > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
              > > lblText.Refresh ();
              > > }
              > >
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Alvin Bruney

                #8
                Re: Threading question

                you sir, need to be commended for bravely venturing into the threaded world.
                threading is easy to do but difficult to master. good luck. post here often
                the community is very knowledgeable on advanced issues

                "Tom B" <shuckle@hotmai l.com> wrote in message
                news:en#AT56eDH A.3284@tk2msftn gp13.phx.gbl...[color=blue]
                > I've been reading more stuff and have found the problems you mention.
                > Thanks for taking the time to respond.
                >
                >
                > "100" <100@100.com> wrote in message
                > news:O3VlIG6eDH A.632@TK2MSFTNG P10.phx.gbl...[color=green]
                > > Hi Tom,
                > > When you start a new thread the new thread and the main thread do[/color][/color]
                execute[color=blue]
                > at[color=green]
                > > the same time. How ever you have a couple of problems in the code:
                > > 1. The main thread owns the form and all controls on it. Updating the[/color][/color]
                form[color=blue][color=green]
                > > or its controls form outside the owning thred thread is wrong and has to[/color]
                > be[color=green]
                > > avoided. So the code like
                > >
                > > lblWorking.Text =DateTime.Now.T oString();
                > > lblWorking.Refr esh();
                > >
                > > in the worker thread is wrong.
                > >
                > > Insted, you should use Control.Invoke to start a method in the main[/color]
                > thread's[color=green]
                > > contex that can update the UI
                > >
                > > 2. Looping in the main thread stops the message loop and no win messages[/color]
                > are[color=green]
                > > processed. So I'm not sure that calling Refresh method will get through
                > > until resuming the message loop. So the worker thread works, but the UI[/color]
                > gets[color=green]
                > > updated when the loop in the main thread is done.
                > >
                > > HTH
                > > B\rgds
                > > 100
                > >
                > >
                > > "Tom B" <shuckle@hotmai l.com> wrote in message
                > > news:%236IIKr5e DHA.3324@TK2MSF TNGP11.phx.gbl. ..[color=darkred]
                > > > I've written the code below to try and figure out how threading[/color][/color][/color]
                works.[color=blue][color=green][color=darkred]
                > > > My assumption is that when starting a new thread it would run at the[/color][/color]
                > same[color=green][color=darkred]
                > > > time as the original thread.
                > > > Am I wrong?
                > > >
                > > > I've made a simple form with two text labels and a button. When I[/color][/color][/color]
                click[color=blue][color=green]
                > > the[color=darkred]
                > > > button, it creates a thread that updates one label, and the main code
                > > > updates the other label.
                > > >
                > > > It appears that the created thread doesn't run until the main thread[/color][/color][/color]
                is[color=blue][color=green][color=darkred]
                > > > done?!?
                > > >
                > > > Is my code wrong? Is my logic wrong? Am I an idiot?
                > > >
                > > > Thanks for taking a look
                > > >
                > > >
                > > > private void button1_Click(o bject sender, System.EventArg s e)
                > > > {
                > > >
                > > > ThreadStart ts=new ThreadStart(add Text);
                > > > Thread t = new Thread(ts);
                > > > t.Start();
                > > >
                > > >
                > > > //for (int iLoop=0;iLoop<1 0;iLoop++)
                > > > //{
                > > > //lblWorking.Text ="";
                > > > lblWorking.Text =DateTime.Now.T oString();
                > > > lblWorking.Refr esh();
                > > > //Thread.Sleep(10 00);
                > > > //}
                > > > }
                > > >
                > > > private void addText()
                > > > {
                > > > lblText.Text+= "Starting: " + DateTime.Now.To String() + "\n";
                > > > //MessageBox.Show ("In the thread");
                > > > for (int iLoop=0;iLoop<1 0;iLoop++)
                > > > {
                > > > Thread.Sleep(30 0);
                > > > lblText.Refresh ();
                > > > }
                > > >
                > > > lblText.Text+=" Finishing: " + DateTime.Now.To String() + "\n";
                > > > lblText.Refresh ();
                > > > }
                > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...