473,396 Members | 2,036 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,396 software developers and data experts.

Threads, InvokeRequired & Form Controls

Hello,
A timer control (I think) runs in another thread apart from a main form.
In the OnTick event, you can update some form control with no worries.

I'm making an AsyncronousServer class that runs in a separate thread and
I'd like to have server generated events (OnListen, OnConnect, OnSend,
OnRecieve, and so on) update a form control.

The only way I can figure out how to do this is to check InvokeRequired
and use Invoke on the form control from the event handler.

Is this the best way to do this? How does the timer control do this?
Cheers,
Bill
Apr 26 '07 #1
4 25455
Hello,
A timer control (I think) runs in another thread
System.Windows.Forms.Timer runs on the UI thread and can freely interact
with controls.

The alternative Timer classes in another namespaces run on another threads
and they cannot interact with the UI.
The only way I can figure out how to do this is to check
InvokeRequired and use Invoke on the form control from the event
handler.

Is this the best way to do this?
Yes, it's just OK. Here is some reading on how it's done in .NET Framework
3.0: http://msdn2.microsoft.com/en-us/library/ms741870.aspx.

(H) Serge
Apr 26 '07 #2
"Bill McCormick" <wp*********@gmail.comwrote in message
news:Iz*****************@newssvr14.news.prodigy.ne t...
A timer control (I think) runs in another thread apart from a main form.
In the OnTick event, you can update some form control with no worries.

I'm making an AsyncronousServer class that runs in a separate thread and
I'd like to have server generated events (OnListen, OnConnect, OnSend,
OnRecieve, and so on) update a form control.

The only way I can figure out how to do this is to check InvokeRequired
and use Invoke on the form control from the event handler.

Is this the best way to do this? How does the timer control do this?
I haven't used the timer control, but if it truly runs on a different thread
then it does have to do Invoke or BeginInvoke to run code on the main UI
thread. (If the timer control actually just uses WM_TIMER messages, then it
doesn't really run on a different thread per se, as the OS is handling the
timing issues for you, and the issue goes away).

And yes, you need to also use Invoke or BeginInvoke to do the same.
Personally, I remain unconvinced that there's any good reason to use
InvokeRequired. I prefer to have the invoking method different from the
method that actually does the work anyway, and breaking the recursion of
Invoking the same method that does the Invoke is the only real benefit of
using InvokeRequired (and so isn't necessary when two different methods are
used).

For example:

private void _CrossThreadSetText(string strNew)
{
Invoke(new SetTextDelegate(_SetText), new object[] { strNew });
}

private void _SetText(string strNew)
{
label1.Text = strNew;
}

In the above example, all callers could use _CrossThreadSetText, or you
could limit its use to just other threads, letting callers you know are
running on the main thread call _SetText directly. Either will work fine,
and there's no need to call InvokeRequired.

Frankly, I find the convention used everywhere in the MSDN documentation of
having a re-entrant invoking method to be very awkward.

Pete

Apr 26 '07 #3
Serge Baltic wrote:
Hello,
>A timer control (I think) runs in another thread


System.Windows.Forms.Timer runs on the UI thread and can freely interact
with controls.

The alternative Timer classes in another namespaces run on another
threads and they cannot interact with the UI.
>The only way I can figure out how to do this is to check
InvokeRequired and use Invoke on the form control from the event
handler.

Is this the best way to do this?


Yes, it's just OK. Here is some reading on how it's done in .NET
Framework 3.0: http://msdn2.microsoft.com/en-us/library/ms741870.aspx.
Wow! Thanks! This will take some time to get through. Does this have
anything to do with Marshalling? I found something about Marshalling at
http://weblogs.asp.net/justin_rogers...s/126345.aspx; Marshalling
and Remoting is a topic that I conviently skipped in my Programming C#
book. Maybe I need to look more closely at using Marshalling?

Regards,

Bill
Apr 26 '07 #4
Hello,
Wow! Thanks! This will take some time to get through.
Please note that the above link about .NET 3.0 describes the threading model
of the 3.0-and-above framework, and won't work out of the box on 2.0 and
below. It's possible to implement that model on 2.0 and 1.x frameworks, but
that's worth the effort in relatively large apps only, or if you're going
to reuse it. Writing all that stuff for one-time use in a small utility is
more like a waste of time (or pure self-education, whichever you prefer :).
Does this have
anything to do with Marshalling? I found something about Marshalling
at http://weblogs.asp.net/justin_rogers...s/126345.aspx;
Marshalling and Remoting is a topic that I conviently skipped in my
Programming C# book. Maybe I need to look more closely at using
Marshalling?
No, not quite. Unlike COM/ActiveX, .NET has almost no marshalling when calling
across the threads, reference and value types are passed transparently. As
for Remoting, I think it's an overkill for the task, also. Creating an invisible
form on the main thread and using Invoke on it will be just enough to fire
a couple events on a single class.

(H) Serge
Apr 27 '07 #5

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

Similar topics

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...
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 =...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
1
by: Mesan | last post by:
I'm getting a "Cross-thread operation not valid" Exception and I can't figure out why. I've got a BackgroundWorker that creates a UserControl with a whole lot of other user controls inside of...
5
by: Cliff | last post by:
I have a Class FWControl defined in FWControl.dll (a .net class library). It doesn't inherit anything other than Object. FWControl has an event StatusChanged, which is actually raised in...
2
by: Anatoli | last post by:
I've got an incrementTimer()-Function, which is called on mous click. It increments Windows.Forms.Timer interval and starts the timer... No problem so far. Now I want to do the same when...
0
by: Kate77 | last post by:
Hi, I have another question, I try to make my threads updating the form, it works fine, for the first thread , but afterwards its not updating the labels on the form anymore not from inside...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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,...

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.