By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,338 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,338 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Jan 29 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a


On Jan 29, 9:41 am, "DaTurk" <mmagd...@hotmail.comwrote:
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

Jan 29 '07 #2

P: n/a
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
"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
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?

Jan 29 '07 #3

P: n/a
Thanks for the replies. I thought that's what was going on, but I
just needed to be sure.

On Jan 29, 10:14 am, "Daniel" <nos...@pokercat.co.ukwrote:
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

"DaTurk" <mmagd...@hotmail.comwrote in messagenews:11**********************@l53g2000cwa.g ooglegroups.com...
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?- Hide quoted text -- Show quoted text -
Jan 29 '07 #4

P: n/a
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

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
| 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?
|
Jan 29 '07 #5

P: n/a
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.

On Jan 29, 10:33 am, "William Stacey [C# MVP]"
<william.sta...@gmail.comwrote:
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

"DaTurk" <mmagd...@hotmail.comwrote in messagenews:11**********************@l53g2000cwa.g ooglegroups.com...
| 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?
|
Jan 29 '07 #6

P: n/a
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

Jan 30 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.