473,662 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Events in a .NET thread

Tom
First of all, is it 'legal' (i.e. thread-safe) to pass events back from
a worker thread to the main thread? I.E. Can one put PUBLIC EVENT XYZ
in their worker thread and then raise that event for processing back in
the main thread? I believe this is OK, but I want to check first...

Secondly, if this is 'legal', what happens in the following scenario:
Lets say that in my main (Win Form) program/thread I spawn two worker
threads. Thread one does some work then raises the 'ImAlmostDone' event
back to my main thread. Now, at about the same time, my second thread
also raises a 'ImAlmostDone' event back. In this case, do both events
get processed in the main thread? And do they get processed in the
proper order - i.e. thread 1's event, then thread 2. Or do I need to do
any thread locking before I process the event (i.e. to make sure the
main thread finishes the event before the next thread raises the same
event)

Before I start passing events back, I want to make sure this is going
to work and I am not going to screw things up.

Thanks in advance.

Tom
Nov 21 '05 #1
1 1231
A thread that raises an event handles the event. So in this case, your
threads will handle the events and not your main thread. If you want the
form's thread to handle the events for you, you should use Me.Invoke or
Me.BeginInvoke to call a delegate.
"Tom" <to*@nospam.com > wrote in message
news:uM******** ******@TK2MSFTN GP10.phx.gbl...
First of all, is it 'legal' (i.e. thread-safe) to pass events back from
a worker thread to the main thread? I.E. Can one put PUBLIC EVENT XYZ
in their worker thread and then raise that event for processing back in
the main thread? I believe this is OK, but I want to check first...

Secondly, if this is 'legal', what happens in the following scenario:
Lets say that in my main (Win Form) program/thread I spawn two worker
threads. Thread one does some work then raises the 'ImAlmostDone' event
back to my main thread. Now, at about the same time, my second thread
also raises a 'ImAlmostDone' event back. In this case, do both events
get processed in the main thread? And do they get processed in the
proper order - i.e. thread 1's event, then thread 2. Or do I need to do
any thread locking before I process the event (i.e. to make sure the
main thread finishes the event before the next thread raises the same
event)

Before I start passing events back, I want to make sure this is going
to work and I am not going to screw things up.

Thanks in advance.

Tom

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
375
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can fail if the event is emptied of all methods between the two statements, implying that some sort of synchronization between this and removals from EventName is needed. The other problem is that if an event with a set of delegates is in the process...
10
4942
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 I'll post the sample code I'm using that is giving me the problems. I'm sure its something I've...
2
18990
by: Gulshan Oshan | last post by:
I want to implement a simple console that continuously listens for an event from a custom object. I am unable to capture the events from the object. If I subscribe to the events in a windows app it works fine. Any idea? using System; using MyTestApp.Messaging; using MyTestApp.BusinessLayer; using System.Threading;
3
506
by: Jacob | last post by:
I'm working on a class that needs to be called from a windows form, do it's work, and then, show progress back to the main form. I'm well aware that worker threads need to call Invoke for updates to the main thread to be threadsafe. I want to make this worker class I'm writing a self contained assembly so that other's can drop it into their projects. My question is: How can I NOT force those implementing my class to have to call...
3
14280
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for the first thread. I can't really see the sence in this. Aren't the events of the...
12
2785
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown when menu items are clicked.Besides these i would like to put a custom status bar. Any error message encountered in any of the webpage will be displayed in the banner. The problem iam encountering is how to access the customer status bar in child...
14
2828
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 rise and event from a thread and have it received from the delegate in another thread (I mean the...
4
2046
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 occasion, one event will
15
6532
by: damiensawyer | last post by:
Hi, I am creating a class (from a base class) that needs to trigger events (code at bottom). I am instatiating the classes and wiring up the events as follows. clsDetermineConnection oDC = new clsDetermineConnection(Request); oDC.LogMessage += new RunTraceBase.TraceArguments(LogMessagesFromEvents);
0
8762
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.