473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1388


On Jan 29, 9:41 am, "DaTurk" <mmagd...@hotma il.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******@hotma il.comwrote in message
news:11******** **************@ l53g2000cwa.goo glegroups.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...@pokerca t.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...@hotma il.comwrote in messagenews:11* *************** ******@l53g2000 cwa.googlegroup s.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******@hotma il.comwrote in message
news:11******** **************@ l53g2000cwa.goo glegroups.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...@hotma il.comwrote in messagenews:11* *************** ******@l53g2000 cwa.googlegroup s.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 ProgressBarUpda ter(int progress, int
total, string action);

private void ReportProgress( object sender,
ReportProgressH andlerArgs e)
{
if (this.InvokeReq uired)
{
int progress =
((ReportProgres sHandlerArgs)e) .Progress;
int total = ((ReportProgres sHandlerArgs)e) .Total;
string action = ((ReportProgres sHandlerArgs)e) .Action;
ProgressBarUpda ter d = new
ProgressBarUpda ter(UpdateProgr essBar);
this.Invoke(d, new object[] { progress, total,
action });
}
else
{
UpdateProgressB ar(e.Progress, e.Total, e.Action);
}
}
private void UpdateProgressB ar(int progress, int total, string
action)
{
progressBar.Max imum = total;
progressBar.Val ue = progress;
labelProgressBa r.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
1435
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 framework, let us call it that, consists in two parts. The first part is just a basic thread class deriving from threading.Thread with a few extra...
10
4931
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. My problem is that I must not be doing it right because only some of the stuff works as would be expected. I'll post what exactly is happening, then...
5
2592
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 be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at...
20
3942
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 SomeDelegateSignature foo2Event; { add { // some code here } remove { // some code here }
15
2603
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, the main thread waits. At the completion of each subthread, the mainthread checks all 12 thread objects to see if they are done. If they are, raise an...
14
2815
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 every weird way that comes to my mind. Particularly I'm struggling to find a way to use events between threads. In particular I'd like to know how to...
4
2044
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 status of the download changes. Some of the events are used to tell the next file to download while others manager other resources. However, on...
5
2794
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 other thread will modify the list while operations are running. However, I at the end of the critical section, I need to fire an OnChange type...
23
4253
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 different modules, and have a dedicated thread for the each module. Modules exchanged info through a in-memory message queue. In my opinion, such a...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.