Backgroundworker and ConsoleApp

G

Guest

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
 
P

Peter Duniho

Vogeler said:
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
 
G

Guest

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
 
C

Chris Mullins [MVP]

I see Peter already answered, but I'll second what he said: The behavior
difference is due to the environment you're running in.

The Console App doesn't have a Message Pump, doesn't have "control.Invoke",
and doesn't have the silly threading requirements that a WinForms app has.
I'm honestly surprised the BackgroundWorker class works at all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top