BeginInvoke, EndInvoke leaves extra thread

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

    BeginInvoke, EndInvoke leaves extra thread

    Hello,

    I am experimenting with async callbacks using BeginInvoke and EndInvoke, and
    although my code which I have shown below works, when the program stops at
    the end (on the ReadLine()), there are still 2 threads running if i pause
    the execution, even if I kow that EndInvoke has been called on the thread.
    This also happens in the async callback sample on msdn. Is this anything I
    need to wory about, because it doesn't seen too good to me??

    Also, if you want to share resources between threads, is it enough to
    declare it volatile, or should I lock it as well?? And, erm, how does one go
    about locking variables in c#?

    Cheers,
    Nick.


    using System;
    using System.Runtime. Remoting.Messag ing;
    class AsyncTestClass
    {
    public delegate void AsyncTestDel (int Num1, int Num2, int Num3);
    public static void AsyncTest(int Num1, int Num2, int Num3)
    {
    Console.WriteLi ne(Num1);
    Console.WriteLi ne(Num2);
    Console.WriteLi ne(Num3);
    }
    public static void Finished(IAsync Result ar)
    {
    // Extract the delegate from the AsyncResult.
    AsyncTestDel fd = (AsyncTestDel)( (AsyncResult)ar ).AsyncDelegate ;

    // Free up resourses
    fd.EndInvoke(ar );

    Console.WriteLi ne("Callback Finished");
    }
    static void Main()
    {
    // Stops the app, only one thread running here
    Console.Write(" Press any key to start demo >");
    Console.ReadLin e();

    // Create delegate and begin Async invocation
    AsyncTestDel d = new AsyncTestDel(As yncTest);
    // Even if you call this many times, you will
    // still only be left with one thread.
    d.BeginInvoke(1 05, 106, 107,
    new AsyncCallback(F inished), null);

    // Do some work
    for (int i = 0; i < 100; i++)
    Console.WriteLi ne(i);

    // Break program here, there are now 2 threads...
    Console.Write(" Press any key to end >");
    Console.ReadLin e();
    }
    }


  • Jon Skeet [C# MVP]

    #2
    Re: BeginInvoke, EndInvoke leaves extra thread

    Nick Palmius <NickPalmius*NO SPAM*@onetel.ne t.uk> wrote:[color=blue]
    > I am experimenting with async callbacks using BeginInvoke and EndInvoke, and
    > although my code which I have shown below works, when the program stops at
    > the end (on the ReadLine()), there are still 2 threads running if i pause
    > the execution, even if I kow that EndInvoke has been called on the thread.
    > This also happens in the async callback sample on msdn. Is this anything I
    > need to wory about, because it doesn't seen too good to me??[/color]

    It's a thread-pool thread. Nothing to worry about.
    [color=blue]
    > Also, if you want to share resources between threads, is it enough to
    > declare it volatile, or should I lock it as well?? And, erm, how does
    > one go about locking variables in c#?[/color]

    See http://www.pobox.com/~skeet/csharp/multithreading.html

    --
    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

    • Nick Palmius

      #3
      Re: BeginInvoke, EndInvoke leaves extra thread

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1b69a4 0c7afc71c598af6 5@msnews.micros oft.com...[color=blue]
      > Nick Palmius <NickPalmius*NO SPAM*@onetel.ne t.uk> wrote:[color=green]
      > > I am experimenting with async callbacks using BeginInvoke and EndInvoke,[/color][/color]
      and[color=blue][color=green]
      > > although my code which I have shown below works, when the program stops[/color][/color]
      at[color=blue][color=green]
      > > the end (on the ReadLine()), there are still 2 threads running if i[/color][/color]
      pause[color=blue][color=green]
      > > the execution, even if I kow that EndInvoke has been called on the[/color][/color]
      thread.[color=blue][color=green]
      > > This also happens in the async callback sample on msdn. Is this anything[/color][/color]
      I[color=blue][color=green]
      > > need to wory about, because it doesn't seen too good to me??[/color]
      >
      > It's a thread-pool thread. Nothing to worry about.
      >[color=green]
      > > Also, if you want to share resources between threads, is it enough to
      > > declare it volatile, or should I lock it as well?? And, erm, how does
      > > one go about locking variables in c#?[/color]
      >
      > See http://www.pobox.com/~skeet/csharp/multithreading.html[/color]

      Great article (and site) thanks.


      Comment

      Working...