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 thread that you can use to update controls have to be the
thread that you were on when you instanced the form?
2. Can 2 forms in the same application have affinity for different threads,
or do you want all of the forms in an application to be created on the main
thread?
Why I'm asking:
I'm working on a module that will place an item in system tray. The code
associated with the tray item will use a secondary thread to listen for
system events, and based on some of the events, it might have to launch a
form. Do I need to work in some logic to make sure that the form launch
code is always executed by the main thread? 10 2784
Hi J.Marsch, 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 thread that you can use to update controls have to be the thread that you were on when you instanced the form?
The thread which has created the control (this thread owns the underlying
windows' control handle) can update the control. This is Windows requirement
rather than Windows Form's one.
2. Can 2 forms in the same application have affinity for different
threads, or do you want all of the forms in an application to be created on the
main thread?
You may have many UI threads any of them may has created bunch of forms and
controls. The rule for the version of .NET running on windows platform is:
You have to update a control only from the thread that owns the control's
HWND (this is thread created the control)
Why I'm asking:
I'm working on a module that will place an item in system tray. The code associated with the tray item will use a secondary thread to listen for system events, and based on some of the events, it might have to launch a form. Do I need to work in some logic to make sure that the form launch code is always executed by the main thread?
From any thread you can call Control.Invoke to execute a method in the UI
thread created the control or form and let this method udate the controls.
Usually main form's Invoke is used.
HTH
B\rgds
100
Hi,
In article <un************ **@TK2MSFTNGP09 .phx.gbl>, je****@ctcdevel oper.com says... 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 thread that you can use to update controls have to be the thread that you were on when you instanced the form?
2. Can 2 forms in the same application have affinity for different threads, or do you want all of the forms in an application to be created on the main thread?
Why I'm asking:
I'm working on a module that will place an item in system tray. The code associated with the tray item will use a secondary thread to listen for system events, and based on some of the events, it might have to launch a form. Do I need to work in some logic to make sure that the form launch code is always executed by the main thread?
Every form has Form.InvokeRequ ired, which you have to inspect to see if
you should need Invoke.
Maybe this is not good solution, but in my form (I want to change a
label field) I use this additions:
private delegate void ChangeActionTex tDelegate(strin g newtext);
public void SetActionText(s tring txt)
{
if (this.InvokeReq uired)
this.Invoke(new ChangeActionTex tDelegate(
this.ChangeActi onText),new object[] {txt});
else
this.ChangeActi onText(txt);
}
private void ChangeActionTex t(string newtext)
{
this.lAction.Te xt = newtext;
this.lAction.Re fresh();
}
So, whenever you want to change the text, doesn't matter from which
thread, you call the public method SetActionText, and it makes sure that
the call is forwarded to the form thread if necessary.
Also, it seems possible the public method to look like:
public void SetActionText (string txt)
{
if (this.InvokeReq uired)
this.Invoke(new ChangeActionTex tDelegate(
this.SetActionT ext), new object[] {txt});
else
{
this.lAction.Te xt = newtext;
this.lAction.Re fresh();
}
}
I.e, you may skip the private method, and to make the form thread to
invoke the same public method.
I just don't like this, becausee I'm not so sure that it is safe at all
(I tried, it worked), and because in the first approach the things are
more readable.
So, you can use similar things for every form control, which you have to
change.
!!!CAUTION!!!
I do not know why, but this approach does not work with properties. If
you put the if statement in a public property, and not in a method, it
hangs the same way, as if you are trying to change a control directly.
Hope that helps
Sunny
Hi,
In winform, controls in Windows Forms are bound to a specific thread and
are not thread safe.
There are four methods on a control that are safe to call from any thread:
Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
calls, you should use one of these invoke methods when calling from a
different thread.
The sample below tells you how to manipulate multithreading winform
controls: http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Thank you all for your input, but there's a fine point here that I'm trying
to close in on. We are on the same page regarding Invoke and that the
"owning" thread must fire events on the control, and I agree that this is a
Windows issue, not winforms.
What I'm lost on:
What would be best practice for instancing a form? In my example, a
background thread will possibly need to throw up a form in response to a
system event. When that happens, from a best-practice point of view, am I
ok just instancing a form on the spot, or am I better off creating a pattern
that will cause the form to be created on the application's Main thread?
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:ub******** ******@cpmsftng xa07.phx.gbl... Hi,
In winform, controls in Windows Forms are bound to a specific thread and are not thread safe. There are four methods on a control that are safe to call from any thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of these invoke methods when calling from a different thread.
The sample below tells you how to manipulate multithreading winform controls: http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
Hope this helps, Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Do what works for you. Why Must you follow everybody else when you're
solution is for you.
If I told you to shove yer hand in the oven , would you? Or cut on yer
gonads?
"J.Marsch" <je****@ctcdeve loper.com> wrote in message
news:#w******** ******@TK2MSFTN GP12.phx.gbl... Thank you all for your input, but there's a fine point here that I'm
trying to close in on. We are on the same page regarding Invoke and that the "owning" thread must fire events on the control, and I agree that this is
a Windows issue, not winforms.
What I'm lost on: What would be best practice for instancing a form? In my example, a background thread will possibly need to throw up a form in response to a system event. When that happens, from a best-practice point of view, am I ok just instancing a form on the spot, or am I better off creating a
pattern that will cause the form to be created on the application's Main thread?
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message news:ub******** ******@cpmsftng xa07.phx.gbl... Hi,
In winform, controls in Windows Forms are bound to a specific thread and are not thread safe. There are four methods on a control that are safe to call from any
thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of these invoke methods when calling from a different thread.
The sample below tells you how to manipulate multithreading winform controls:
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
Hope this helps, Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no
rights.
Well, with all due respect (though you've shown me none). What works for me
may not work for Windows. If I'm about to make a design decision that will
affect the architecture of a UI layer that will be used in multiple
applications, and as the basis for the design of hundreds of forms, don't
you sort of think that it would be a good idea to make sure that the
threading model is compatible with the Windows UI layer???
That's called called due dilligence and common sense, Alvin.
If you don't have anything useful to add to this conversation, why don't
you just crawl back under whatever rock you came out from under.
"Alvin Bruney" <al**********@t elia.com> wrote in message
news:eX******** ******@TK2MSFTN GP10.phx.gbl... Do what works for you. Why Must you follow everybody else when you're solution is for you.
If I told you to shove yer hand in the oven , would you? Or cut on yer gonads?
"J.Marsch" <je****@ctcdeve loper.com> wrote in message news:#w******** ******@TK2MSFTN GP12.phx.gbl... Thank you all for your input, but there's a fine point here that I'm trying to close in on. We are on the same page regarding Invoke and that the "owning" thread must fire events on the control, and I agree that this
is a Windows issue, not winforms.
What I'm lost on: What would be best practice for instancing a form? In my example, a background thread will possibly need to throw up a form in response to a system event. When that happens, from a best-practice point of view, am
I ok just instancing a form on the spot, or am I better off creating a pattern that will cause the form to be created on the application's Main thread?
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message news:ub******** ******@cpmsftng xa07.phx.gbl... Hi,
In winform, controls in Windows Forms are bound to a specific thread
and are not thread safe. There are four methods on a control that are safe to call from any
thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other
method calls, you should use one of these invoke methods when calling from a different thread.
The sample below tells you how to manipulate multithreading winform controls: http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
Hope this helps, Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Hi J.Marsch, What I'm lost on: What would be best practice for instancing a form? In my example, a background thread will possibly need to throw up a form in response to a system event. When that happens, from a best-practice point of view, am I ok just instancing a form on the spot, or am I better off creating a
pattern that will cause the form to be created on the application's Main thread?
You don't have options in this case. if you create the form in the worker
thread it won't receive any win messages and will stay frozen. If you want
your form to do something you have to pump messages (Application.Ru n or
Application.DoE vents) in your worker thread, which is not what you want to
do in your worker thread I believe.
So the only option is to let the main UI thread throw up the form. Use
Control.Invoke to switch to the main UI thread, etc.
HTH
B\rgds
100 ""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message news:ub******** ******@cpmsftng xa07.phx.gbl... Hi,
In winform, controls in Windows Forms are bound to a specific thread and are not thread safe. There are four methods on a control that are safe to call from any
thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of these invoke methods when calling from a different thread.
The sample below tells you how to manipulate multithreading winform controls:
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
Hope this helps, Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no
rights.
Gotcha. That's exactly what I needed to know. Thank you very much.
Regards.
-- Jeremy
"100" <10*@100.com> wrote in message
news:OF******** ********@TK2MSF TNGP10.phx.gbl. .. Hi J.Marsch, What I'm lost on: What would be best practice for instancing a form? In my example, a background thread will possibly need to throw up a form in response to a system event. When that happens, from a best-practice point of view, am
I ok just instancing a form on the spot, or am I better off creating a pattern that will cause the form to be created on the application's Main thread?
You don't have options in this case. if you create the form in the worker thread it won't receive any win messages and will stay frozen. If you want your form to do something you have to pump messages (Application.Ru n or Application.DoE vents) in your worker thread, which is not what you want to do in your worker thread I believe. So the only option is to let the main UI thread throw up the form. Use Control.Invoke to switch to the main UI thread, etc.
HTH B\rgds 100
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message news:ub******** ******@cpmsftng xa07.phx.gbl... Hi,
In winform, controls in Windows Forms are bound to a specific thread
and are not thread safe. There are four methods on a control that are safe to call from any
thread: Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other
method calls, you should use one of these invoke methods when calling from a different thread.
The sample below tells you how to manipulate multithreading winform controls:
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
Hope this helps, Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Its called MSDN, go read it.
"J.Marsch" <je****@ctcdeve loper.com> wrote in message
news:Oy******** ******@TK2MSFTN GP10.phx.gbl... Well, with all due respect (though you've shown me none). What works for
me may not work for Windows. If I'm about to make a design decision that
will affect the architecture of a UI layer that will be used in multiple applications, and as the basis for the design of hundreds of forms, don't you sort of think that it would be a good idea to make sure that the threading model is compatible with the Windows UI layer???
That's called called due dilligence and common sense, Alvin.
If you don't have anything useful to add to this conversation, why don't you just crawl back under whatever rock you came out from under.
"Alvin Bruney" <al**********@t elia.com> wrote in message news:eX******** ******@TK2MSFTN GP10.phx.gbl... Do what works for you. Why Must you follow everybody else when you're solution is for you.
If I told you to shove yer hand in the oven , would you? Or cut on yer gonads?
"J.Marsch" <je****@ctcdeve loper.com> wrote in message news:#w******** ******@TK2MSFTN GP12.phx.gbl... Thank you all for your input, but there's a fine point here that I'm trying to close in on. We are on the same page regarding Invoke and that
the "owning" thread must fire events on the control, and I agree that this is a Windows issue, not winforms.
What I'm lost on: What would be best practice for instancing a form? In my example, a background thread will possibly need to throw up a form in response to
a system event. When that happens, from a best-practice point of view,
am I ok just instancing a form on the spot, or am I better off creating a pattern that will cause the form to be created on the application's Main
thread? ""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message news:ub******** ******@cpmsftng xa07.phx.gbl... > > Hi, > > In winform, controls in Windows Forms are bound to a specific thread and > are not thread safe. > There are four methods on a control that are safe to call from any thread: > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method > calls, you should use one of these invoke methods when calling from
a > different thread. > > The sample below tells you how to manipulate multithreading winform > controls: > http://msdn.microsoft.com/library/de...us/cpguide/htm > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp > > Hope this helps, > Best regards, > Jeffrey Tan > Microsoft Online Partner Support > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Noble |
last post by:
We've been developing a web site using 3-tier architecture for 18
months now.
There is a common layer that defines the classes - using XML schemas.
The data layer acts as a wrapper to 3 databases - SQL Server, Oracle
and AS400.
The business layer exposes web services which communicate with the
front end, ASP.Net. All 3 tiers are on different boxes. This works
well.
Now I am leading a team to build a winforms app. I need some advice as
|
by: Steve Smith |
last post by:
I have written an application that launches approximately 150 threads with
Thread.ThreadStart()
Each thread uses CDO 1.21 to logon to a different Exchange mailbox and
send/receive a number of mail messages, reporting back to the UI thread
through the use of a Queue object. When all messages that are expected have
been received, each thread sends a final update to the UI and the method
should exit, which should terminate the thread.
...
|
by: Steve Smith |
last post by:
I have written a winforms application that launches approximately 150
threads with
Thread.ThreadStart()
Each thread uses CDO 1.21 to logon to a different Exchange mailbox and
send/receive a number of mail messages, reporting back to the UI thread
through the use of a Queue object. When all messages that are expected have
been received, each thread sends a final update to the UI and the method
should exit, which should terminate the...
|
by: vdahiya |
last post by:
Hi All,
I am using winform threads and I have craeted a new thread to perform
soem lengthy operations asynchronously.
My problem is that while this operation is being performed i am
updating the window status bar to display the status of the operation.
But as soon as the window loses the focus it freezes (mean the staus
bar is not updated anymore).
following is my code (in UI thread):
|
by: brian.wilson4 |
last post by:
Our group is currently comparing winforms vs webforms.....app is Corp
LAN based - we have control of desktops.....Below is pros and cons list
we have come up with - if anything strikes you as untrue or you would
like to add - please comment - thanks.....
Rich Client PROS
1. User experience (* indicates feasible on web with AJAX) -
a. Single, unified application experience
b. Windows/Office-like look and feel -
i.Tabs - drag and drop...
| |
by: D. Patrick |
last post by:
I need to guide the training for some good ASP.NET programmers who don't
have Windows Forms experience, and they will soon be put onto a project for
multi-threaded winforms development.
Can anyone suggest a good book for multi-threaded winforms learning? Or
even some good URLs?
|
by: Peted |
last post by:
Just some threading questions
1. in a MDI app if you have multiple child forms, does each child
form run in its own thread ? Eg do you get error trying to update one
control on form1 from form 2
2. if you have a timer control running on a childform, and you either
a. minimize or
|
by: =?Utf-8?B?Y2hlbmRyaWNrcw==?= |
last post by:
I have a C# winforms application that makes periodic web service calls in
background thread to my web service server. These calls work fine almost all
the time but on rare occassions the web service method call will never return
and the entire application will freeze, even though the call is being made on
its own background thread, not the main UI thread.
Does anyone know any reason why a particular web service method call would
cause my...
|
by: =?Utf-8?B?cmFuZHkxMjAw?= |
last post by:
Visual Studio 2005, C# WinForms application:
Here’s the question: How can I increase the standard 1 MB stack size of the
UI thread in a C# WinForms application?
Here’s why I ask:
I’ve inherited some code that at the view (User Interface) layer kicks off a
background worker thread. At the service layer (think CAB service layer),
there’s quite a lot of the following:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |