473,395 Members | 1,608 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,395 software developers and data experts.

How to send a notification to another thread/process in C#?

1
My question has such a scenario:
Two threads, thread1(e.g. UI thread in WPF) and thread2,

thread1 starts thread2 in threadpool and returns to handle other UI events. What thread2 does is to start a process until complete, then notify thread1 it is done, returns an exitcode to thread1.
The problem is how to make thread1 do other things while thread2 is busy with some computation, but responds immediately when the process in thread2 returns.

public class Window1:Window
{
onbuttonrunclick()
{
start thread2
}
.....
}

thread2
{
create WorkProcess.
WorkProcess.start();
WorkProcess.waitforexit();
}

Even further, is it possible get rid of thread2, and use some inter-process communication mechanism to notify UI the exitcode of the WorkProcess.

Any suggestion is greatly appreciated.
Oct 2 '08 #1
1 5133
mldisibio
190 Expert 100+
If Thread1 is truly a GUI/Windows Form, then the safest and easiest way to accomplish what you want is using the BackgroundWorker Class .

Also, please consult a very authoritative and freely available overview of threading and synchronization: Threading in C# by Joseph Albahari. He has an extensive example of using the BackgoundWorker.

A Framework GUI is single threaded. That is, any updates or communication to the GUI thread can only be done on that thread. If your second thread attempts to update the GUI without being marshalled back to the original GUI thread, ThreadExceptions will occur.

There are ways to accomplish this with thread callbacks, delegates and the use of Form.InvokeRequired and Form.Invoke. Happily however, the BackgroundWorker does all this work for you. As long as the BackgroundWorker is created by the GUI, then its callback (RunWorkerCompleted) will also be marshalled back to the GUI thread.
Oct 2 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with...
3
by: cnu | last post by:
Hi, I am starting a new threading to do some process. I want to get notified when the thread does it's job and exits. After receiving the notification, I want to refresh the form. I am...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
3
by: Kiran | last post by:
Hi, Was wondering if its possible to have a event notification in Thread when the thread is about to start and the thread has just finished doing its work and about to terminate. If its not...
1
by: Joey | last post by:
I am using the below code to run a dos based program. While it is running you can hit ctrl-c to cancel the process. Does anyone know how I can send the same keystroke to the already runing...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
1
by: Dave | last post by:
I'm sending data to a website with a WebRequest, string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response =...
5
by: Markgoldin | last post by:
I am searching for a solution of sending messages from not .Net process into a .Net process written in C#. I have gone already thru sockets solution and have porblems with it. I am not a C# coder...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.