Threading

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

    Threading

    Hi,
    Normally in what kind of situation system will abort a thread created
    manually ? And how do I know the thread is aborted by system after I start
    it?

    Thanks!
    Mark


  • Willy Denoyette [MVP]

    #2
    Re: Threading

    Not sure what you mean with "system will abort thread", but in general such
    threads are (should) never aborted, threads exists for the duration of your
    thread procedure. If you need to know when a thread procedure has ended, you
    have to call Thread.Join()
    Willy.

    "Mark" <mxu@rogers.com > wrote in message
    news:kpidnWVW4p ggBALcRVn-vQ@rogers.com.. .[color=blue]
    > Hi,
    > Normally in what kind of situation system will abort a thread created
    > manually ? And how do I know the thread is aborted by system after I start
    > it?
    >
    > Thanks!
    > Mark
    >
    >[/color]


    Comment

    • David Levine

      #3
      Re: Threading

      The system will inject a ThreadAbortExce ption into all threads that
      originated in an appdomain that is being unloaded. This is the only time
      AFAIK when the system will do that.

      "Mark" <mxu@rogers.com > wrote in message
      news:kpidnWVW4p ggBALcRVn-vQ@rogers.com.. .[color=blue]
      > Hi,
      > Normally in what kind of situation system will abort a thread created
      > manually ? And how do I know the thread is aborted by system after I start
      > it?
      >
      > Thanks!
      > Mark
      >
      >[/color]


      Comment

      • Patrik Löwendahl [C# MVP]

        #4
        Re: Threading

        Are you sure about that?

        All tests I've ever done never indicates a ThreadAbortExce ption is
        injected when the application domain is unloading. Isn't CLR just
        killing them?

        --
        Patrik Löwendahl [C# MVP]
        cshrp.net - 'Elegant code by witty programmers'
        cornerstone.se 'IT Training for professionals'

        David Levine wrote:[color=blue]
        > The system will inject a ThreadAbortExce ption into all threads that
        > originated in an appdomain that is being unloaded. This is the only time
        > AFAIK when the system will do that.
        >
        > "Mark" <mxu@rogers.com > wrote in message
        > news:kpidnWVW4p ggBALcRVn-vQ@rogers.com.. .
        >[color=green]
        >>Hi,
        >>Normally in what kind of situation system will abort a thread created
        >>manually ? And how do I know the thread is aborted by system after I start
        >>it?
        >>
        >>Thanks!
        >>Mark
        >>
        >>[/color]
        >
        >
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Threading

          "Patrik Löwendahl [C# MVP]" <patrik.lowenda hl.no.freakin.s pam@home.se>
          wrote:[color=blue]
          > Are you sure about that?
          >
          > All tests I've ever done never indicates a ThreadAbortExce ption is
          > injected when the application domain is unloading. Isn't CLR just
          > killing them?[/color]

          According to the docs for AppDomain.Unloa d:

          <quote>
          The threads in domain are terminated using the Abort method, which
          throws the thread an instance of ThreadAbortExce ption. Although the
          thread should terminate promptly, it can continue executing for an
          unpredictable amount of time in its finally clause.
          </quote>

          Here's a complete pair of programs to demonstrate it:

          Test.cs - compile to test.exe
          using System;

          class Test
          {
          static void Main()
          {
          AppDomain dom = AppDomain.Creat eDomain ("Foo");

          dom.ExecuteAsse mbly ("test2.exe" );

          // By the time this returns, the other thread will be running

          AppDomain.Unloa d(dom);
          }
          }

          Test2.cs - compile to test2.exe
          using System;
          using System.Threadin g;

          class Test2
          {
          static void Main()
          {
          new Thread (new ThreadStart(Thr eadJob)).Start( );
          Thread.Sleep(10 0);
          }

          static void ThreadJob()
          {
          Console.WriteLi ne ("Executing in other app domain thread");
          try
          {
          Thread.Sleep(20 00);
          }
          catch (Exception e)
          {
          Console.WriteLi ne ("Got exception "+e);
          }
          }
          }

          Results:
          Executing in other app domain thread
          Got exception System.Threadin g.ThreadAbortEx ception: Thread was being
          aborted.
          at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
          at Test2.ThreadJob ()

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Patrik Löwendahl [C# MVP]

            #6
            Re: Threading

            Thanks for the clarification.

            A thought though.

            What if the tread is in unmanaged space? The CLR will still need to wait
            for it to come back to managed space?

            --
            Patrik Löwendahl [C# MVP]
            cshrp.net - 'Elegant code by witty programmers'
            cornerstone.se 'IT Training for professionals'

            Jon Skeet [C# MVP] wrote:[color=blue]
            > "Patrik Löwendahl [C# MVP]" <patrik.lowenda hl.no.freakin.s pam@home.se>
            > wrote:
            >[color=green]
            >>Are you sure about that?
            >>
            >>All tests I've ever done never indicates a ThreadAbortExce ption is
            >>injected when the application domain is unloading. Isn't CLR just
            >>killing them?[/color]
            >
            >
            > According to the docs for AppDomain.Unloa d:
            >
            > <quote>
            > The threads in domain are terminated using the Abort method, which
            > throws the thread an instance of ThreadAbortExce ption. Although the
            > thread should terminate promptly, it can continue executing for an
            > unpredictable amount of time in its finally clause.
            > </quote>
            >
            > Here's a complete pair of programs to demonstrate it:
            >
            > Test.cs - compile to test.exe
            > using System;
            >
            > class Test
            > {
            > static void Main()
            > {
            > AppDomain dom = AppDomain.Creat eDomain ("Foo");
            >
            > dom.ExecuteAsse mbly ("test2.exe" );
            >
            > // By the time this returns, the other thread will be running
            >
            > AppDomain.Unloa d(dom);
            > }
            > }
            >
            > Test2.cs - compile to test2.exe
            > using System;
            > using System.Threadin g;
            >
            > class Test2
            > {
            > static void Main()
            > {
            > new Thread (new ThreadStart(Thr eadJob)).Start( );
            > Thread.Sleep(10 0);
            > }
            >
            > static void ThreadJob()
            > {
            > Console.WriteLi ne ("Executing in other app domain thread");
            > try
            > {
            > Thread.Sleep(20 00);
            > }
            > catch (Exception e)
            > {
            > Console.WriteLi ne ("Got exception "+e);
            > }
            > }
            > }
            >
            > Results:
            > Executing in other app domain thread
            > Got exception System.Threadin g.ThreadAbortEx ception: Thread was being
            > aborted.
            > at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
            > at Test2.ThreadJob ()
            >[/color]

            Comment

            • Patrik Löwendahl [C# MVP]

              #7
              Re: Threading

              Hmm, now I see why I was sceptic about the thread abort exception. Just
              tried a similar scenario in the default domain. For that scenario, no
              threadabort exception is caught by the try catch.

              Could one assume then that an appdomain isn't unloaded in the same
              manner as doing it manually and the process just dies taking it's
              (bakground)thre ads with it?

              test:

              static void ThreadProc()
              {
              try
              {
              while(true)
              {
              Thread.Sleep(50 0);
              Console.WriteLi ne("Tick...");
              }

              }
              catch ( ThreadAbortExce ption teax )
              {
              StreamWriter sw = new StreamWriter("C :\\temp\\meppo. txt");

              sw.WriteLine(te ax.ToString());

              Thread.Sleep(10 000);

              Console.WriteLi ne ( "Thread aborted " );
              Console.ReadLin e();
              }
              catch ( Exception e )
              {
              Console.WriteLi ne ( "Exception {0}", e.Message);
              }

              }

              static void Main(string[] args)
              {
              Thread t = new Thread( new ThreadStart ( ThreadProc ) );
              t.IsBackground = true;
              t.Start();

              Thread.Sleep ( 2000 );
              }

              --
              Patrik Löwendahl [C# MVP]
              cshrp.net - 'Elegant code by witty programmers'
              cornerstone.se 'IT Training for professionals'

              Jon Skeet [C# MVP] wrote:[color=blue]
              > "Patrik Löwendahl [C# MVP]" <patrik.lowenda hl.no.freakin.s pam@home.se>
              > wrote:
              >[color=green]
              >>Are you sure about that?
              >>
              >>All tests I've ever done never indicates a ThreadAbortExce ption is
              >>injected when the application domain is unloading. Isn't CLR just
              >>killing them?[/color]
              >
              >
              > According to the docs for AppDomain.Unloa d:
              >
              > <quote>
              > The threads in domain are terminated using the Abort method, which
              > throws the thread an instance of ThreadAbortExce ption. Although the
              > thread should terminate promptly, it can continue executing for an
              > unpredictable amount of time in its finally clause.
              > </quote>
              >
              > Here's a complete pair of programs to demonstrate it:
              >
              > Test.cs - compile to test.exe
              > using System;
              >
              > class Test
              > {
              > static void Main()
              > {
              > AppDomain dom = AppDomain.Creat eDomain ("Foo");
              >
              > dom.ExecuteAsse mbly ("test2.exe" );
              >
              > // By the time this returns, the other thread will be running
              >
              > AppDomain.Unloa d(dom);
              > }
              > }
              >
              > Test2.cs - compile to test2.exe
              > using System;
              > using System.Threadin g;
              >
              > class Test2
              > {
              > static void Main()
              > {
              > new Thread (new ThreadStart(Thr eadJob)).Start( );
              > Thread.Sleep(10 0);
              > }
              >
              > static void ThreadJob()
              > {
              > Console.WriteLi ne ("Executing in other app domain thread");
              > try
              > {
              > Thread.Sleep(20 00);
              > }
              > catch (Exception e)
              > {
              > Console.WriteLi ne ("Got exception "+e);
              > }
              > }
              > }
              >
              > Results:
              > Executing in other app domain thread
              > Got exception System.Threadin g.ThreadAbortEx ception: Thread was being
              > aborted.
              > at System.Threadin g.Thread.Sleep( Int32 millisecondsTim eout)
              > at Test2.ThreadJob ()
              >[/color]

              Comment

              • Willy Denoyette [MVP]

                #8
                Re: Threading


                ""Patrik Löwendahl [C# MVP]"" <patrik.lowenda hl.no.freakin.s pam@home.se>
                wrote in message news:ezm1deB0EH A.1308@TK2MSFTN GP09.phx.gbl...[color=blue]
                > Hmm, now I see why I was sceptic about the thread abort exception. Just
                > tried a similar scenario in the default domain. For that scenario, no
                > threadabort exception is caught by the try catch.
                >
                > Could one assume then that an appdomain isn't unloaded in the same manner
                > as doing it manually and the process just dies taking it's
                > (bakground)thre ads with it?[/color]

                The default application domain is never unloaded, it is thrown away when the
                process terminates as a result of an orderly/unorderly shutdown.

                Willy.



                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: Threading


                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  news:MPG.1c0b19 a32863340598b95 9@msnews.micros oft.com...
                  "Patrik Löwendahl [C# MVP]" <patrik.lowenda hl.no.freakin.s pam@home.se>
                  wrote:[color=blue]
                  > Are you sure about that?
                  >
                  > All tests I've ever done never indicates a ThreadAbortExce ption is
                  > injected when the application domain is unloading. Isn't CLR just
                  > killing them?[/color]

                  According to the docs for AppDomain.Unloa d:

                  <quote>
                  The threads in domain are terminated using the Abort method, which
                  throws the thread an instance of ThreadAbortExce ption. Although the
                  thread should terminate promptly, it can continue executing for an
                  unpredictable amount of time in its finally clause.
                  </quote>

                  This is not exactly true, I guess it was done like this in an early beta of
                  the CLR, but now the CLR will inject a ThreadAbortExce ptions into the
                  threads that must be unwound. This is more scalable than calling Abort, but
                  the effect is the same.

                  Willy.


                  Comment

                  • Mark

                    #10
                    Re: Threading

                    Thanks a lot.
                    If I start 500 threads from a win app inside manually, how does system
                    handle it? Does system queue them up and run them once the system resource
                    (thread, memory etc.) available? All 500 threads would be guarantied to be
                    executed?

                    Thanks again,
                    Mark

                    "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                    news:#zesvWC0EH A.3336@TK2MSFTN GP11.phx.gbl...[color=blue]
                    >
                    > ""Patrik Löwendahl [C# MVP]"" <patrik.lowenda hl.no.freakin.s pam@home.se>
                    > wrote in message news:ezm1deB0EH A.1308@TK2MSFTN GP09.phx.gbl...[color=green]
                    > > Hmm, now I see why I was sceptic about the thread abort exception. Just
                    > > tried a similar scenario in the default domain. For that scenario, no
                    > > threadabort exception is caught by the try catch.
                    > >
                    > > Could one assume then that an appdomain isn't unloaded in the same[/color][/color]
                    manner[color=blue][color=green]
                    > > as doing it manually and the process just dies taking it's
                    > > (bakground)thre ads with it?[/color]
                    >
                    > The default application domain is never unloaded, it is thrown away when[/color]
                    the[color=blue]
                    > process terminates as a result of an orderly/unorderly shutdown.
                    >
                    > Willy.
                    >
                    >
                    >[/color]


                    Comment

                    • Ravichandran J.V.

                      #11
                      Re: Threading

                      Yes, all 500 threads would be started. The system does not queue them
                      up, the app will attempt to start all the 500 threads and execute each
                      according to the time-slicing factor.

                      with regards,


                      J.V.Ravichandra n
                      - http://www.geocities.com/
                      jvravichandran
                      - http://www.411asp.net/func/search?
                      qry=Ravichandra n+J.V.&cob=aspn etpro
                      - http://www.southasianoutlook.com
                      - http://www.MSDNAA.Net
                      - http://www.csharphelp.com
                      - http://www.poetry.com/Publications/
                      display.asp?ID= P3966388&BN=999 &PN=2
                      - Or, just search on "J.V.Ravichandr an"
                      at http://www.Google.com

                      *** Sent via Developersdex http://www.developersdex.com ***
                      Don't just participate in USENET...get rewarded for it!

                      Comment

                      • David Levine

                        #12
                        Re: Threading


                        "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
                        news:%23zesvWC0 EHA.3336@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                        >
                        > ""Patrik Löwendahl [C# MVP]"" <patrik.lowenda hl.no.freakin.s pam@home.se>
                        > wrote in message news:ezm1deB0EH A.1308@TK2MSFTN GP09.phx.gbl...[color=green]
                        >> Hmm, now I see why I was sceptic about the thread abort exception. Just
                        >> tried a similar scenario in the default domain. For that scenario, no
                        >> threadabort exception is caught by the try catch.
                        >>
                        >> Could one assume then that an appdomain isn't unloaded in the same manner
                        >> as doing it manually and the process just dies taking it's
                        >> (bakground)thre ads with it?[/color]
                        >
                        > The default application domain is never unloaded, it is thrown away when
                        > the process terminates as a result of an orderly/unorderly shutdown.
                        >
                        > Willy.
                        >[/color]
                        That's basically it, but (as usual) it's a bit more complex then that...it
                        follows the procedure outlined by Chris Brumme in these blogs:




                        Finalizers are still run. Aborts are injected into threads in appdomains
                        other then the default. It's the difference between unloading an appdomain
                        and shutting down the application.



                        Comment

                        • Willy Denoyette [MVP]

                          #13
                          Re: Threading


                          "David Levine" <noSpamdlevineN NTP2@wi.rr.com> wrote in message
                          news:%23dTex2H0 EHA.3708@TK2MSF TNGP14.phx.gbl. ..
                          [color=blue]
                          > That's basically it, but (as usual) it's a bit more complex then that...it
                          > follows the procedure outlined by Chris Brumme in these blogs:
                          >
                          > http://blogs.msdn.com/cbrumme/archiv.../01/51466.aspx
                          > http://blogs.msdn.com/cbrumme/archiv.../20/51504.aspx
                          >
                          > Finalizers are still run. Aborts are injected into threads in appdomains
                          > other then the default. It's the difference between unloading an appdomain
                          > and shutting down the application.
                          >[/color]

                          Davig,
                          True, but I didn't want to enter into these (implementation ) details as
                          they may change from one version of the runtime to another.
                          My point was that Abort() wasn't used at all, "ThreadAbortExc eptions" are
                          injected into the selected threads when unloading a (non default) domain.
                          When there's no other domain than the default domain, the CLR just starts
                          the process shutdown procedure perfectly outlined by Chris, there are no
                          thread aborts injected and no domain unloaded (the CLR doesn't even care
                          about it), but you should not take this as gospel as it as some details have
                          changed considerably in v2.

                          Willy.



                          Comment

                          • David Levine

                            #14
                            Re: Threading

                            > True, but I didn't want to enter into these (implementation ) details as[color=blue]
                            > they may change from one version of the runtime to another.
                            > My point was that Abort() wasn't used at all, "ThreadAbortExc eptions" are
                            > injected into the selected threads when unloading a (non default) domain.
                            > When there's no other domain than the default domain, the CLR just starts
                            > the process shutdown procedure perfectly outlined by Chris, there are no
                            > thread aborts injected and no domain unloaded (the CLR doesn't even care
                            > about it), but you should not take this as gospel as it as some details
                            > have changed considerably in v2.
                            >[/color]
                            I've played with Whidbey a little but currently I am still developing on
                            v1.1. I know there's lots of new goodies coming down that should improve
                            things quite a bit. Thanks for the heads up.


                            Comment

                            Working...