HighTechTalks DotNet Forums  

AsyncOperation not working as I expected

Dotnet Framework microsoft.public.dotnet.framework


Discuss AsyncOperation not working as I expected in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Lee Alexander
 
Posts: n/a

Default AsyncOperation not working as I expected - 11-19-2007 , 08:50 AM






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



Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: AsyncOperation not working as I expected - 11-19-2007 , 10:44 AM






On Nov 19, 1:50 pm, "Lee Alexander" <lee@feedghost_dot_com> wrote:
Quote:
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


Reply With Quote
  #3  
Old   
Lee Alexander
 
Posts: n/a

Default Re: AsyncOperation not working as I expected - 11-19-2007 , 12:48 PM



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



"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

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



Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: AsyncOperation not working as I expected - 11-19-2007 , 05:49 PM



Lee Alexander <lee@feedghost_dot_com> wrote:
Quote:
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 - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.