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

Cross thread question .....

Hello,

I have an app that reads data params from a stream and updates
controls accordingly. The stream reader is on a different thread than
the main thread that created the controls. I fully realize it's not
wise, and in some circumstances, not even possible to update the
controls from the stream other than the main stream. I believe there
are two recommended ways to handle this situation.

1. Use Invoke() to "call" an updater method on the main thread from
the stream reader thread.

2. Use the BackgroundWorker class and update controls with the
RunWorkerCompleted event handler.
Question is: Which one is better? I have to update about 75 controls
at a 10Hz rate (10 times a second) so I'm looking for speed here.
TIA

using 2.0, VS2005

Feb 1 '07 #1
5 3004

Use option 3. BeginInvoke() which calls the method on the UI thread
without blocking the stream thread.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 1 Feb 2007 11:21:18 -0800, te***@cfl.rr.com wrote:
>Hello,

I have an app that reads data params from a stream and updates
controls accordingly. The stream reader is on a different thread than
the main thread that created the controls. I fully realize it's not
wise, and in some circumstances, not even possible to update the
controls from the stream other than the main stream. I believe there
are two recommended ways to handle this situation.

1. Use Invoke() to "call" an updater method on the main thread from
the stream reader thread.

2. Use the BackgroundWorker class and update controls with the
RunWorkerCompleted event handler.
Question is: Which one is better? I have to update about 75 controls
at a 10Hz rate (10 times a second) so I'm looking for speed here.
TIA

using 2.0, VS2005
Feb 1 '07 #2
On Feb 1, 11:21 am, t...@cfl.rr.com wrote:
Hello,

I have an app that reads data params from a stream and updates
controls accordingly. The stream reader is on a different thread than
the main thread that created the controls. I fully realize it's not
wise, and in some circumstances, not even possible to update the
controls from the stream other than the main stream. I believe there
are two recommended ways to handle this situation.

1. Use Invoke() to "call" an updater method on the main thread from
the stream reader thread.

2. Use the BackgroundWorker class and update controls with the
RunWorkerCompleted event handler.
Question is: Which one is better? I have to update about 75 controls
at a 10Hz rate (10 times a second) so I'm looking for speed here.
TIA

using 2.0, VS2005
Here's another option.

If you _know_ that you're going to be updating the controls rapidly,
10 times a second, as you said, an you're going to do this until, say,
the user presses Cancel or something, you could create an intermediate
area where the values for the controls are stored. So, conceptually,
you have your UI controls, which are displaying results, your stream,
which is reading results to display, and a "neutral zone" where the
results are stored.

Your stream thread just reads results and updates the "neutral zone"
values.

Your UI thread runs in a loop and (at some regular interval) picks up
values from the "neutral zone" and updates all of the contols.

Good points:

No need to marshal calls across threads. The two threads run
independently. I anticipate that this will be a big time saver.

The UI thread can be tuned (UI updates) separately from the stream
thread. In other words, you've decoupled the need for every-input-must-
be-displayed logic.

So... the UI can never "get behind" the updates and have a huge number
of calls build up on its event queue because there are no calls from
the stream thread to the UI thread.

Bad points:

Because input and display are decoupled, any given display may show
inconsistent data. However, if you're really updating 10 times a
second, I doubt that the user will notice.

The "neutral zone" data area must have proper locking done on it, so
that the UI thread doesn't read a value as its being updated by the
stream thread.

You may miss displaying some results, if the stream thread updates a
result twice before the UI thread displays it. Again, I doubt that
your user will notice.

You may do many more UI updates than are necessary. If one UI control
changes frequently while another changes rarely, and if this is
unpredictable, you will be updating all 75 controls every cycle,
because you have no idea what has changed and what hasn't. (This can
be mitigated by taking a snapshot of the "neutral zone" and then
updating the UI from that, giving you a basis for comparison next
time, but that's getting rather complicated, IMHO.)

Anyway... some food for thought.

Feb 1 '07 #3
Hey,

What if you create a delegate and pass this delegate as a parameter to your
thread? The delegate would be within the same context as the main thread. I
did this already before and works just fine.

--
Regards,
Robson Siqueira
Enterprise Architect
<te***@cfl.rr.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
Hello,

I have an app that reads data params from a stream and updates
controls accordingly. The stream reader is on a different thread than
the main thread that created the controls. I fully realize it's not
wise, and in some circumstances, not even possible to update the
controls from the stream other than the main stream. I believe there
are two recommended ways to handle this situation.

1. Use Invoke() to "call" an updater method on the main thread from
the stream reader thread.

2. Use the BackgroundWorker class and update controls with the
RunWorkerCompleted event handler.
Question is: Which one is better? I have to update about 75 controls
at a 10Hz rate (10 times a second) so I'm looking for speed here.
TIA

using 2.0, VS2005

Feb 1 '07 #4
On Feb 1, 1:21 pm, t...@cfl.rr.com wrote:
Hello,

I have an app that reads data params from a stream and updates
controls accordingly. The stream reader is on a different thread than
the main thread that created the controls. I fully realize it's not
wise, and in some circumstances, not even possible to update the
controls from the stream other than the main stream. I believe there
are two recommended ways to handle this situation.

1. Use Invoke() to "call" an updater method on the main thread from
the stream reader thread.

2. Use the BackgroundWorker class and update controls with the
RunWorkerCompleted event handler.
Question is: Which one is better? I have to update about 75 controls
at a 10Hz rate (10 times a second) so I'm looking for speed here.
TIA

using 2.0, VS2005
Hello,

I agree with Bruce on this one. Have your UI thread poll a shared
data structure that contains the data coming in from the stream. This
will be both faster (I think) and less risky since it eliminates the
possibility of the worker thread overrunning the UI thread with
BeginInvoke/Invoke calls.

Brian

Feb 1 '07 #5
Just to clarify, you would still have to call the delegate using
Invoke or BeginInvoke. You can't just call it outright, as it would
then run in the context of the background thread.

There's nothing about a delegate that ensures that it always runs on a
particular thread; a delegate is just a pointer to a method to run,
nothing more.

On Feb 1, 1:29 pm, "Robson Siqueira" <rob...@robsonfelix.comwrote:
Hey,

What if you create a delegate and pass this delegate as a parameter to your
thread? The delegate would be within the same context as the main thread. I
did this already before and works just fine.

--
Regards,
Robson Siqueira
Enterprise Architect<t...@cfl.rr.comwrote in message

news:11**********************@m58g2000cwm.googlegr oups.com...
Hello,
I have an app that reads data params from a stream and updates
controls accordingly. The stream reader is on a different thread than
the main thread that created the controls. I fully realize it's not
wise, and in some circumstances, not even possible to update the
controls from the stream other than the main stream. I believe there
are two recommended ways to handle this situation.
1. Use Invoke() to "call" an updater method on the main thread from
the stream reader thread.
2. Use the BackgroundWorker class and update controls with the
RunWorkerCompleted event handler.
Question is: Which one is better? I have to update about 75 controls
at a 10Hz rate (10 times a second) so I'm looking for speed here.
TIA
using 2.0, VS2005
Feb 1 '07 #6

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

Similar topics

23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
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...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
3
by: jlamanna | last post by:
I was wondering if there was a utility that could tell you when your C# application is making cross-apartment COM calls. I have a fairly large application that makes extensive use of a 3rd party...
11
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C#...
3
by: Pieter Coucke | last post by:
Hi, In my VB.NET 2005 application I'm generating and sending emails using the outlook-object model (2003). When a mail is Send (MailObject_Send), I raise an event in a global class, that is...
15
by: dani kotlar | last post by:
I am trying to make a client-server application to run, but I am running into this problem: in the server Form the call InitializeComponent(); creates a text-box. Later the calls: readThread =...
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
0
by: Peter Duniho | last post by:
On Mon, 02 Jun 2008 17:14:31 -0700, NvrBst <nvrbst@gmail.comwrote: The general rule is that if it's not one of the five members listed in the docs (under Control.Invoke and other pages), you...
3
by: kimiraikkonen | last post by:
Hi, I was looking for an example on CodeProject and saw an interesting thing, i downloaded the article source code and converted to my VB 2005 Express and compiled with no problem, however when i...
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:
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...
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...

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.