Home All Groups Group Topic Archive Search About

AsyncOperation not working as I expected

Author
19 Nov 2007 1:50 PM
Lee Alexander
Hi,

I'm trying to use AsyncOperation to execute a method in a main thread
context from  a worker thread. If I run the program from a console
application then it doesn't work. If however I run it from  a WindowsForms
application it does. Basically I set the name of the main thread to "Main
Thread", when the "ShouldBeInMainThreadCall" is called the
Thread.CurrentThread.Name is null. However if I run it in a WindowsForms
application it has the correct name. Could anyone help me with this?

Find below the source for the console app:

using System;
using System.Threading;
using System.ComponentModel;

namespace TestThreadAffinityEvents
{
class Program
{
  private static AsyncOperation operation;
  private static void DoWork()
  {
   operation.Post( new SendOrPostCallback( delegate( object state )
   {
    ShouldBeInMainThreadCall();
   } ), null );

   operation.OperationCompleted();
  }

  static void Main( string[] args )
  {
   Thread.CurrentThread.Name = "Main thread";

   Thread workerThread = new Thread( new ThreadStart( DoWork ) );
   operation = AsyncOperationManager.CreateOperation( null );
   workerThread.Start();

   int i = 0;
   while( true )
   {
    Console.WriteLine( string.Format( "Hello from {0} {1}",
Thread.CurrentThread.Name, i++ ) );
    Thread.Sleep( 500 );
   }
  }

  static void ShouldBeInMainThreadCall()
  {
   Console.WriteLine( string.Format( "Calling you from {0}",
Thread.CurrentThread.Name ) );
  }
}
}


Cheers
Lee

Author
19 Nov 2007 3:44 PM
Jon Skeet [C# MVP]
On Nov 19, 1:50 pm, "Lee Alexander" <lee@feedghost_dot_com> wrote:
> I'm trying to use AsyncOperation to execute a method in a main thread
> context from  a worker thread. If I run the program from a console
> application then it doesn't work. If however I run it from  a WindowsForms
> application it does. Basically I set the name of the main thread to "Main
> Thread", when the "ShouldBeInMainThreadCall" is called the
> Thread.CurrentThread.Name is null. However if I run it in a WindowsForms
> application it has the correct name. Could anyone help me with this?

Console applications don't (normally, at least) run a message pump or
anything similar - the main thread just runs until it's finished all
its work. There's no way of getting back to the "main" thread of a
console application (without extra work to basically have your own
producer/consumer queue).

Jon
Author
19 Nov 2007 5:48 PM
Lee Alexander
Hi Jon,



Yeah I figured that was the case, the thing I find perplexing is the
difference in behaviour, in that when run under Windows Forms the delegate
gets run in the main thread SynchronizationContext whereas under the console
application it runs in a seperate thread. It seems to be a recipe for
problems if a component that relies on this is run within both Console and
Windows Forms (message pump) scenarios.



Maybe I'll add a little comment on the Community Content mentioning this..



Cheers

Lee



Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:c0aaec4d-a8b1-47a3-9e79-18505a1d7778@n20g2000hsh.googlegroups.com...
> On Nov 19, 1:50 pm, "Lee Alexander" <lee@feedghost_dot_com> wrote:
>> I'm trying to use AsyncOperation to execute a method in a main thread
>> context from  a worker thread. If I run the program from a console
>> application then it doesn't work. If however I run it from  a
>> WindowsForms
>> application it does. Basically I set the name of the main thread to "Main
>> Thread", when the "ShouldBeInMainThreadCall" is called the
>> Thread.CurrentThread.Name is null. However if I run it in a WindowsForms
>> application it has the correct name. Could anyone help me with this?
>
> Console applications don't (normally, at least) run a message pump or
> anything similar - the main thread just runs until it's finished all
> its work. There's no way of getting back to the "main" thread of a
> console application (without extra work to basically have your own
> producer/consumer queue).
>
> Jon
Author
19 Nov 2007 10:49 PM
Jon Skeet [C# MVP]
Lee Alexander <lee@feedghost_dot_com> wrote:
> Yeah I figured that was the case, the thing I find perplexing is the
> difference in behaviour, in that when run under Windows Forms the delegate
> gets run in the main thread SynchronizationContext whereas under the console
> application it runs in a seperate thread. It seems to be a recipe for
> problems if a component that relies on this is run within both Console and
> Windows Forms (message pump) scenarios.
>
> Maybe I'll add a little comment on the Community Content mentioning this..

Well, it's doing the most appropriate thing for the context in which
it's running. The idea is that if you've got an appropriate context,
you can get the framework to use it (although I don't know the details)
- but console apps just don't have that synchronization context.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AddThis Social Bookmark Button