Events and threads ... whose thread am I on anyway?

D

DaTurk

Here's a quick question, say I have a class with a main routine, we'll
just call it main.

Main is on the "main" thread.

Now lets have main creat an instance of another class, which creates a
thread, and has it do some
calculation, and on completion calls an event which main has += on.
Lets call this class, calcClass.

CalcClass is on the "calc" thread. "<I'm just making these statements
to confirm we have two threads>

OK, so we have main += on calcClass's event, and sending it to do it's
thing. So here's the question, Events are called synchronously by
default right, unless you use begin invoke or something. So, when an
event gets popped from calcClass, and we enter the eventhandling code
in main, we will be on calcClass's thread? Is this correct?
 
B

Brian Gideon

Here's a quick question, say I have a class with a main routine, we'll
just call it main.

Main is on the "main" thread.

Now lets have main creat an instance of another class, which creates a
thread, and has it do some
calculation, and on completion calls an event which main has += on.
Lets call this class, calcClass.

CalcClass is on the "calc" thread. "<I'm just making these statements
to confirm we have two threads>

OK, so we have main += on calcClass's event, and sending it to do it's
thing. So here's the question, Events are called synchronously by
default right, unless you use begin invoke or something. So, when an
event gets popped from calcClass, and we enter the eventhandling code
in main, we will be on calcClass's thread? Is this correct?

Hi,

That is correct. Event handlers are executed on the thread that
raised the event. In your case the completion event is raised from
the calcClass thread and so the event handlers subscribed to that
event will also run on that thread.

Brian
 
D

Daniel

So your saying:

Main thread code makes a second thread calc thread.

2 threads running now calc and main.

main listens for an event e

calc fires that event.

I believe you would be on calcs thread yes.

Best way to test is this.

- Create a form on main have it listen for an event
- Now trigger an event in new thread created
- have the handling code in main change the form ui....if you get a cross
thread exception then you know calc is the running thread
 
W

William Stacey [C# MVP]

Also note, some events "may" be raised using a TP thread if the class
designer invoked the handlers that way. This is something you may also need
to do yourself under some cases (i.e. invoke them manually).

--
William Stacey [C# MVP]
PCR concurrency library: www.codeplex.com/pcr

| Here's a quick question, say I have a class with a main routine, we'll
| just call it main.
|
| Main is on the "main" thread.
|
| Now lets have main creat an instance of another class, which creates a
| thread, and has it do some
| calculation, and on completion calls an event which main has += on.
| Lets call this class, calcClass.
|
| CalcClass is on the "calc" thread. "<I'm just making these statements
| to confirm we have two threads>
|
| OK, so we have main += on calcClass's event, and sending it to do it's
| thing. So here's the question, Events are called synchronously by
| default right, unless you use begin invoke or something. So, when an
| event gets popped from calcClass, and we enter the eventhandling code
| in main, we will be on calcClass's thread? Is this correct?
|
 
D

DaTurk

Thanks for the replies. I thought that's what was going on, but I
just needed to be sure.
 
D

DaTurk

This class was designed specifically to stay away from TP threads. SO
we can just create a single worker thread that handles the task
alotted for it.
 
J

joachim

I have a question: when programming libraries, is it a good practice
to design a class (let's say a parser class)
that raises events, with a handling form in the following manner, or
are there better ways?

private delegate void ProgressBarUpdater(int progress, int
total, string action);

private void ReportProgress(object sender,
ReportProgressHandlerArgs e)
{
if (this.InvokeRequired)
{
int progress =
((ReportProgressHandlerArgs)e).Progress;
int total = ((ReportProgressHandlerArgs)e).Total;
string action = ((ReportProgressHandlerArgs)e).Action;
ProgressBarUpdater d = new
ProgressBarUpdater(UpdateProgressBar);
this.Invoke(d, new object[] { progress, total,
action });
}
else
{
UpdateProgressBar(e.Progress, e.Total, e.Action);
}
}


private void UpdateProgressBar(int progress, int total, string
action)
{
progressBar.Maximum = total;
progressBar.Value = progress;
labelProgressBar.Text = action;
}

thanks,
Joachim
 

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

Similar Threads


Top