|
|
|
start date: Thu, 23 Aug 2007 00:48:00 -0700,
posted on: microsoft.public.dotnet.framework
back
| Thread Index |
|
1
Vogeler, volkhard
|
|
2
Peter Duniho
|
|
3
Vogeler, volkhard
|
Backgroundworker and ConsoleApp
Hello,
i´m using the backgroundworker in an Consolenapplication. For testing i
write the current ManagedThreadid from Main, the Backgroundworker
Dowork-delegate and RunworkerCOmpleted-delegate. i expected that the ids from
Main and RunworkerCompleted are the saim - but they are not!
can anyone help? here is the coding:
class Program
{
static BackgroundWorker bw = new BackgroundWorker();
static void Main(string[] args)
{
// On my machine thread id 10
Console.WriteLine("Main started. Thread id = " +
Thread.CurrentThread.ManagedThreadId.ToString());
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
Console.WriteLine("Press return");
Console.ReadLine();
}
static void bw_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// On my machine thread id is 11 not 10, which is unexpected
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Console.WriteLine("Completed. Thread id = " +
Thread.CurrentThread.ManagedThreadId.ToString());
}
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
// On my machine thread id is 7 not 10, which is expected
Console.WriteLine("Background thread started. Thread id = " +
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
best regards
Volkhard
Date:Thu, 23 Aug 2007 00:48:00 -0700
Author:
|
Re: Backgroundworker and ConsoleApp
Vogeler wrote:
> Hello,
>
> im using the backgroundworker in an Consolenapplication. For testing i
> write the current ManagedThreadid from Main, the Backgroundworker
> Dowork-delegate and RunworkerCOmpleted-delegate. i expected that the ids from
> Main and RunworkerCompleted are the saim - but they are not!
Well, how could the RunWorkerCompleted delegate get executed on the main
thread, given that your main thread doesn't have any way to execute
invoked delegates? With a Forms-based application, there's an event
loop and a specific async context in which to execute the delegate. The
delegate can essentially be executed using Control.Invoke() on the class
that owns the delegate (I'm not sure if that's what actually happens,
but the net effect is the same).
But your console application has nothing like that.
I'm not even sure there's a way to add one...if there is, I'd guess it
would involve having to create some kind of context, like an
AsyncOperation or something. I've never tried it, so I don't know.
Is there some specific reason you want the delegate to be executed on
the main thread? In a Forms application, you generally want to because
you're not allowed to access the Control instances from other threads
anyway. But in a console application, you don't have that restriction,
and generally executing the delegate on some other thread shouldn't be a
problem (assuming you've addressed other synchronization issues, if any).
If you can be more specific about what you're trying to do, maybe
someone can offer some advice that addresses the problem more directly.
Pete
Date:Thu, 23 Aug 2007 01:25:07 -0700
Author:
|
Re: Backgroundworker and ConsoleApp
Hi Pete,
thanks for your answer - this code was just to test the behaviour of the
Backgroundworkler. i didn´t expeteced that the backgroundworker has another
behaviour on Console-Applications than on forms.
thanks for your help!
best regards
"Peter Duniho" wrote:
> Vogeler wrote:
> > Hello,
> >
> > i´m using the backgroundworker in an Consolenapplication. For testing i
> > write the current ManagedThreadid from Main, the Backgroundworker
> > Dowork-delegate and RunworkerCOmpleted-delegate. i expected that the ids from
> > Main and RunworkerCompleted are the saim - but they are not!
>
> Well, how could the RunWorkerCompleted delegate get executed on the main
> thread, given that your main thread doesn't have any way to execute
> invoked delegates? With a Forms-based application, there's an event
> loop and a specific async context in which to execute the delegate. The
> delegate can essentially be executed using Control.Invoke() on the class
> that owns the delegate (I'm not sure if that's what actually happens,
> but the net effect is the same).
>
> But your console application has nothing like that.
>
> I'm not even sure there's a way to add one...if there is, I'd guess it
> would involve having to create some kind of context, like an
> AsyncOperation or something. I've never tried it, so I don't know.
>
> Is there some specific reason you want the delegate to be executed on
> the main thread? In a Forms application, you generally want to because
> you're not allowed to access the Control instances from other threads
> anyway. But in a console application, you don't have that restriction,
> and generally executing the delegate on some other thread shouldn't be a
> problem (assuming you've addressed other synchronization issues, if any).
>
> If you can be more specific about what you're trying to do, maybe
> someone can offer some advice that addresses the problem more directly.
>
> Pete
>
Date:Thu, 23 Aug 2007 04:48:02 -0700
Author:
|
|
|