473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
6 1378


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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
10
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading....
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
20
by: David Levine | last post by:
I ran into a problem this morning with event accessor methods that appears to be a bug in C# and I am wondering if this a known issue. public event SomeDelegateSignature fooEvent; public event...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
4
by: jehugaleahsa | last post by:
Hello: Is there a way to prevent one event from firing while another event is already being fired? I have a tool that extracts media from web pages and it has multiple events firing when the...
5
by: =?Utf-8?B?VmFubmk=?= | last post by:
Hi, I have a component where I need to have thread-safe access to a list. Operations on the list are done in critical sections (lock (lockObject) { ... } ) in the usual way, to make sure that no...
23
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, Recently I had a new coworker. There is some dispute between us. The last company he worked for has a special networking programming model. They split the business logic into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.