473,387 Members | 1,540 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.

Threads, forms, and controls

Is this scenario possible?

One has a form, a thread and a control. The thread adds a control to the
form. The form adds handlers to every control added but because the control
is in a different thread the handler might cause an exception.

If this is possible how to I prevent it from happening?

in my form I do

this.ControlRemoved += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Removed);

this.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Added);

private void Control_Added(object sender,
System.Windows.Forms.ControlEventArgs e)

{

e.Control.LostFocus += new EventHandler(Control_LostFocus);

e.Control.MouseEnter += new EventHandler(Control_MouseEnter);

e.Control.MouseLeave += new EventHandler(Control_MouseLeave);

}

private void Control_Removed(object sender,
System.Windows.Forms.ControlEventArgs e)

{

e.Control.LostFocus -= new EventHandler(Control_LostFocus);

e.Control.MouseEnter -= new EventHandler(Control_MouseEnter);

e.Control.MouseLeave -= new EventHandler(Control_MouseLeave);

}

and I'm worried that it could cause issues ;/

Thanks,

Jon
Nov 16 '06 #1
2 1631
Hi,

Jon Slaughter wrote:
Is this scenario possible?

One has a form, a thread and a control. The thread adds a control to the
form. The form adds handlers to every control added but because the control
is in a different thread the handler might cause an exception.
You cannot modify the UI elements from another thread than the UI
thread. To pass events to the UI thread from another thread, you must
use the System.Windows.Forms.Control.Invoke method.
http://msdn2.microsoft.com/en-us/lib...ol.invoke.aspx

To check if you need Invoke or not, use the Control.InvokeRequired property.

HTH
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 16 '06 #2

"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:ek**************@TK2MSFTNGP02.phx.gbl...
Hi,

Jon Slaughter wrote:
>Is this scenario possible?

One has a form, a thread and a control. The thread adds a control to the
form. The form adds handlers to every control added but because the
control is in a different thread the handler might cause an exception.

You cannot modify the UI elements from another thread than the UI thread.
To pass events to the UI thread from another thread, you must use the
System.Windows.Forms.Control.Invoke method.
http://msdn2.microsoft.com/en-us/lib...ol.invoke.aspx

To check if you need Invoke or not, use the Control.InvokeRequired
property.
ok, thanks. This means I don't have to worry about that case.

Jon
Nov 16 '06 #3

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

Similar topics

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....
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
1
by: Wesman | last post by:
Threads, textboxes and scrolling Thanks in advance for any information on this matter. I have run into a small richtextbox, scrolling and tread issue. Which has me totally confused. Instead of...
10
by: J.Marsch | last post by:
I know that the controls on a Winform are not thread safe, and that you want to do all of your UI updates on a single thread -- generally the main thread. Now, 2 questions: 1. Does the one...
0
by: Adonai | last post by:
Hi, I'm having an issue with this code that I'm writing, I'm curious as to why the windows form label (lblTime) isn't getting updated with the new variable when I use the timer. I made a blank form...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
12
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a...
4
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired =...
3
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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.