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

Handling Control UI Events in Worker Threads

I need to handle UI events in a worker thread instead of the primary UI
thread. In C#, is the normal UI event handling behavior to run in a
context thread on the thread pool or are events always invoked on the
primary UI thread?

Thanks,
Jordan

Aug 22 '06 #1
2 2793
Jordan wrote:
I need to handle UI events in a worker thread instead of the primary UI
thread. In C#, is the normal UI event handling behavior to run in a
context thread on the thread pool or are events always invoked on the
primary UI thread?

Thanks,
Jordan
Normally the events are invoked and handled inside the GUI thread, but
nothing keeps you from handling them in another thread. You just have to
be aware that WinForms are not thread safe out of the box.

Here is an example of a form that handles the KeyDown event of a TextBox
in a ThreadPool thread and syncronizes back to the UI:

namespace TestApp
{
class Program
{
[STAThread]
static void Main()
{
Application.Run(new UIEventInThreadTestForm());
}
}
class UIEventInThreadTestForm : Form
{
public UIEventInThreadTestForm()
{
Size = new Size(500, 500);

textBox.Location = new Point(10, 10);
textBox.Size = new Size(100, 10);
Controls.Add(textBox);

textBox.KeyUp += new KeyEventHandler(textBox_KeyUp);

label.Location = new Point(120, 10);
label.Size = new Size(100, 20);
Controls.Add(label);
}

void textBox_KeyUp(object sender, KeyEventArgs e)
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(HandleKeyDownAsync), e);
}
private void HandleKeyDownAsync(object keyEventArgs)
{
KeyEventArgs e = (KeyEventArgs)keyEventArgs;

UpdateLabel((char)e.KeyCode);
}

private void UpdateLabel(char key)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<char>(UpdateLabel), key);
return;
}

label.Text = label.Text + key;
}

private TextBox textBox = new TextBox();
private Label label = new Label();
}
}

HTH,
Andy

--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Aug 22 '06 #2
Andreas,

Thank you for your speedy reply. This answers what I needed to know.
Thank you very much!

Jordan

Aug 22 '06 #3

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

Similar topics

3
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...
2
by: Natalia DeBow | last post by:
Hi there, I am working on an client-server app, where the client asynchronously issues a request for the server to perform some action and the server is supposed to notify the client when the...
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...
7
by: Waleed AlRashoud | last post by:
Hi All, I hvae worker class to manage the application inside worker : worker creates (n) number of threads to do IO operations. The Problem Is: to handle all threads : , , I have to...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
1
by: Tom | last post by:
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...
4
by: MadSage | last post by:
I currently have a multi-threaded server application with worker threads and a core thread. For all of these threads I have something like the following code: uint32 __stdcall...
9
by: thiago777 | last post by:
Question details: VB .NET / threads / events / GUI Imagine the following situation: A method from object "A" creates "n" threads. Variables from these threads contains values that should...
0
by: thiago777 | last post by:
Hi! Im still trying to make my application work in an event-driven way. It had worked so far with my threads, but the problem Im having is only when modifying a GUI component from the event handler...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
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...

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.