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

Multithreading and updating the GUI

Hi all,

I have a workerthread which notyfies the GUI through a delegate like this:

if (deviceInsertedDelegate) deviceInsertedDelegate(stringAddress);

Now in my Form's code I write code like this:

private delegate void DeviceInsertedCallback(string Address);

public void DeviceInserted(string Address)
{
if (this.label1.InvokeRequired)
{
Invoke(new DeviceInsertedCallback(DeviceInserted), new
object[] { Address });
}
else
{
label1.Text = "Login : " + Address;
}
}

Now my question is if it's possible not to have the code as above in my
form. Would it be possible to synchronize (or whatever) from within my
worker thread.

Thanks in advance,

Bart

Jun 27 '08 #1
12 2178
On Tue, 24 Jun 2008 12:59:44 -0700, <bwrote:
[...]
Now my question is if it's possible not to have the code as above in my
form. Would it be possible to synchronize (or whatever) from within my
worker thread.
Yes, it is possible, and without even changing the API you've designed.
But IMHO it's better not to. Your form delegate method has more implicit
information and can more appropriately manage the decision as to whether
to call Invoke() or not. Unless your worker thread is specifically
designed to deal with GUI classes (the BackgroundWorker class would be an
example of this type of exception to the rule), I think it's better to
leave the invoking to the actual GUI code.

For what it's worth, you could simplify your delegate method though:

public void DeviceInserted(string Address)
{
Invoke((MethodInvoker)delegate { label1.Text = "Login: "
+ Address; });
}

You're not specific about why you'd prefer not to have code like that
which you posted, but it's possible you'd find the above preferable.

I personally dislike the MSDN-recommended technique of checking
InvokeRequired. In almost every example I've ever run into, the method is
used only when invoking is required. That along with my distaste for a
method that is basically doing two entirely different things (invoking
itself, or actually doing some work) leads me to the alternate
implementation I propose above.

Even if the method is sometimes used when invoking isn't required, there's
no harm in calling Invoke(). The invoked delegate will simply be called
directly.

Pete
Jun 27 '08 #2
b wrote:
Hi all,

I have a workerthread which notyfies the GUI through a delegate like this:

if (deviceInsertedDelegate) deviceInsertedDelegate(stringAddress);

Now in my Form's code I write code like this:

private delegate void DeviceInsertedCallback(string Address);

public void DeviceInserted(string Address)
{
if (this.label1.InvokeRequired)
{
Invoke(new DeviceInsertedCallback(DeviceInserted), new
object[] { Address });
}
else
{
label1.Text = "Login : " + Address;
}
}

Now my question is if it's possible not to have the code as above in my
form. Would it be possible to synchronize (or whatever) from within my
worker thread.

Thanks in advance,

Bart
Only the GUI thread can update the controls, so you need to make the
code run in the GUI thread somehow.

Instead of checking if invoke is required, you can just make the
background thread make the invoke directly. You know that the invoke is
always required from the background thread.

An alternative to invoking a method, is to put the data in some
(synchronised or volatile) variable that both threads have access to,
and have a timer in the GUI that checks the variable for a value to put
in the label.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3
Hi Peter,

Thanks for your quick answer
For what it's worth, you could simplify your delegate method though:

public void DeviceInserted(string Address)
{
Invoke((MethodInvoker)delegate { label1.Text = "Login: " +
Address; });
}
personally I don't like it when my worker thread has to know anything about
the gui.......what if a textbox is used next time.....

You're not specific about why you'd prefer not to have code like that
which you posted, but it's possible you'd find the above preferable.
I don't want to saddle up the user which uses my code with the responsibilty
to handle it the way I wrote. That's why

Any ideas left ?

Bart

Jun 27 '08 #4
Hi Göran,

Thanks for your reply
>
Only the GUI thread can update the controls, so you need to make the code
run in the GUI thread somehow.

Instead of checking if invoke is required, you can just make the
background thread make the invoke directly. You know that the invoke is
always required from the background thread.

An alternative to invoking a method, is to put the data in some
(synchronised or volatile) variable that both threads have access to, and
have a timer in the GUI that checks the variable for a value to put in the
label.
Could you elaborate on this because I am not sure what you mean here.

Thanks again,

Bart

Jun 27 '08 #5
On Tue, 24 Jun 2008 13:54:19 -0700, <bwrote:
personally I don't like it when my worker thread has to know anything
about the gui.......what if a textbox is used next time.....
I don't understand the question. My proposed change is still in your Form
subclass. If you want to change to a different control, just do that.
The worker thread doesn't need to know anything about the form at all,
just as in the code you posted it doesn't.

There's really no difference between the code I posted and the code you
posted, except mine is more concise. They both do exactly the same thing,
and have exactly the same amount of information or lack thereof with
respect to the classes involved.
>You're not specific about why you'd prefer not to have code like that
which you posted, but it's possible you'd find the above preferable.

I don't want to saddle up the user which uses my code with the
responsibilty to handle it the way I wrote. That's why
I'm not convinced that's a suitable reason. If your class isn't
specifically like the BackgroundWorker -- that is, it's not specifically
designed to work with GUI classes but rather is more general purpose than
that -- then a) it's really not that inconvenient for the user of your
library to handle the invoking, and b) it's more appropriate for him to do
so anyway.
Any ideas left ?
You can look at the target of the delegate passed to you, see if it
implements ISynchronizeInvoke, and call Invoke() on that interface if it
does.

But really, that's something I'd only do if you specifically intend your
class to be used with GUI targets. Most threaded classes are more general
purpose than that, and are better off leaving the cross-thread management
to the code that knows the most about the situation (i.e. the GUI class
itself).

Pete
Jun 27 '08 #6
On 24 Jun, 21:55, <bwrote:
Hi Göran,

Thanks for your reply
Only the GUI thread can update the controls, so you need to make the code
run in the GUI thread somehow.
Instead of checking if invoke is required, you can just make the
background thread make the invoke directly. You know that the invoke is
always required from the background thread.
An alternative to invoking a method, is to put the data in some
(synchronised or volatile) variable that both threads have access to, and
have a timer in the GUI that checks the variable for a value to put in the
label.

Could you elaborate on this because I am not sure what you mean here.

Thanks again,

Bart
Opera 9.5 seems to have broken google groups again (or vice versa) so
I'll have to be brief as I can only see two lines. Take a look at
SynchronizationContext. There are quite a few good articles about it.
Jun 27 '08 #7
On Tue, 24 Jun 2008 14:39:27 -0700, DeveloperX
<ia************@googlemail.comwrote:
[...]
An alternative to invoking a method, is to put the data in some
(synchronised or volatile) variable that both threads have access to,
and
have a timer in the GUI that checks the variable for a value to put
in the
label.

Could you elaborate on this because I am not sure what you mean here.

Opera 9.5 seems to have broken google groups again (or vice versa) so
I'll have to be brief as I can only see two lines. Take a look at
SynchronizationContext. There are quite a few good articles about it.
While SynchronizationContext is in fact yet another way to approach the
situation, a) it's probably superfluous since he's already dealing with a
class that supports cross-thread invocations, and b) it's definitely not
what Göran was writing about.

As far as Göran's actual comment goes, he's talking about polling some
variable that contains the data of interest. When he writes "synchronized
or volatile", he's not talking about SynchronizationContext. He's just
talking about either using a synchronization technique (such as lock(),
the Monitor class, the Interlocked class, etc.), or simply marking a
variable as volatile (which would work fine for a string or other
reference, or any atomically-written value for that matter).

I personally don't condone polling, and especially not for a situation
like this where there are perfectly reasonable alternatives. But it's
certainly a viable alternative if the OP really wants to go that way.

But it still doesn't have anything to do with SynchronizationContext.

Pete
Jun 27 '08 #8
On 24 Jun, 22:48, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Tue, 24 Jun 2008 14:39:27 -0700, DeveloperX *

<ianathomeag...@googlemail.comwrote:
[...]
An alternative to invoking a method, is to put the data in some
(synchronised or volatile) variable that both threads have access to, *
and
have a timer in the GUI that checks the variable for a value to put *
in the
label.
Could you elaborate on this because I am not sure what you mean here.
Opera 9.5 seems to have broken google groups again (or vice versa) so
I'll have to be brief as I can only see two lines. Take a look at
SynchronizationContext. There are quite a few good articles about it.

While SynchronizationContext is in fact yet another way to approach the *
situation, a) it's probably superfluous since he's already dealing with a*
class that supports cross-thread invocations, and b) it's definitely not *
what Göran was writing about.

As far as Göran's actual comment goes, he's talking about polling some *
variable that contains the data of interest. *When he writes "synchronized *
or volatile", he's not talking about SynchronizationContext. *He's just*
talking about either using a synchronization technique (such as lock(), *
the Monitor class, the Interlocked class, etc.), or simply marking a *
variable as volatile (which would work fine for a string or other *
reference, or any atomically-written value for that matter).

I personally don't condone polling, and especially not for a situation *
like this where there are perfectly reasonable alternatives. *But it's *
certainly a viable alternative if the OP really wants to go that way.

But it still doesn't have anything to do with SynchronizationContext.

Pete
I replied to the wrong post. Seriously I'm giving up with google
groups at this point.I can't see anything. My point was, and I'm not
proof reading this, syncctx can move much of the worry about running
something on the right thread out of the gui and somewhere more
sensible. I'm also with you on polling. It's not the way forward.
Dear Google, fix this. Dear Opera, fix this.
Jun 27 '08 #9
On Tue, 24 Jun 2008 15:14:15 -0700, DeveloperX
<ia************@googlemail.comwrote:
I replied to the wrong post. Seriously I'm giving up with google
groups at this point. [...]
Dear Google, fix this. Dear Opera, fix this.
For what it's worth, Opera has a servicable, if not great, newsreader
built into its messaging features. In fact, that's what I'm using right
now. Rather than using the web UI on Google Groups, just hook into a real
NNTP server. Your ISP probably includes access to one, and if it doesn't,
my understanding is that there are several free-access ones out there.
For sure, _this_ newsgroup is available, since you can get at it and every
other "microsoft..." newsgroup via Microsoft's own NNTP server (off the
top of my head, I think it's called "msnews.microsoft.com", but I not
completely sure).

Pete
Jun 27 '08 #10
I don't understand the question. My proposed change is still in your Form
subclass. If you want to change to a different control, just do that.
The worker thread doesn't need to know anything about the form at all,
just as in the code you posted it doesn't.

There's really no difference between the code I posted and the code you
posted, except mine is more concise. They both do exactly the same thing,
and have exactly the same amount of information or lack thereof with
respect to the classes involved.

Sorry was a little bit tired yesterday. I didn't read your code
well.....sorry for that

I think I will stick to your proposal.

Thanks again,

Bart

Jun 27 '08 #11
On 25 Jun, 02:20, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Tue, 24 Jun 2008 15:14:15 -0700, DeveloperX *

<ianathomeag...@googlemail.comwrote:
I replied to the wrong post. Seriously I'm giving up with google
groups at this point. [...]
Dear Google, fix this. Dear Opera, fix this.

For what it's worth, Opera has a servicable, if not great, newsreader *
built into its messaging features. *In fact, that's what I'm using right *
now. *Rather than using the web UI on Google Groups, just hook into a real *
NNTP server. *Your ISP probably includes access to one, and if it doesn't, *
my understanding is that there are several free-access ones out there. *
For sure, _this_ newsgroup is available, since you can get at it and every *
other "microsoft..." newsgroup via Microsoft's own NNTP server (off the *
top of my head, I think it's called "msnews.microsoft.com", but I not *
completely sure).

Pete
Good grief, I've been using Opera for years and never knew! I mainly
use the web browser interface to get through work firewalls, but I'll
give this a go now I'm home :)

Thanks
Jun 27 '08 #12
Peter Duniho wrote:
On Tue, 24 Jun 2008 15:14:15 -0700, DeveloperX
<ia************@googlemail.comwrote:
>I replied to the wrong post. Seriously I'm giving up with google
groups at this point. [...]
Dear Google, fix this. Dear Opera, fix this.

For what it's worth, Opera has a servicable, if not great, newsreader
built into its messaging features. In fact, that's what I'm using right
now. Rather than using the web UI on Google Groups, just hook into a
real NNTP server. Your ISP probably includes access to one, and if it
doesn't, my understanding is that there are several free-access ones out
there. For sure, _this_ newsgroup is available, since you can get at it
and every other "microsoft..." newsgroup via Microsoft's own NNTP server
(off the top of my head, I think it's called "msnews.microsoft.com", but
I not completely sure).

Pete
I use aioe.cjb.net as my newserver. It's free, carry's a LOT of groups,
and it just works.

Todd
Jun 27 '08 #13

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

Similar topics

11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
11
by: DW | last post by:
I've gotten this question a couple of times in interviews and I don't know what they are looking for: How do you update a control's property, such as a textbox.text property, from a thread, in...
8
by: GTi | last post by:
I have a UI Class with StatusBarPanels Then I have another Class that is a thread. I want this class thread to update the StatusBarPanels in the UI class. But I get the error: error CS0120: An...
4
by: Frank | last post by:
I am loading data through a OleDbDatareader. The load is started from a form by the user, but the actual load is done in a separate class, which is accessed from my form. Now, I want the user to...
0
by: Andy Roxburgh | last post by:
Hi all, I'm trying to program a multithreaded application - it currently works, but I have two problems. I'm relatively new to VB.NET (C and VB6 are my things) so I'm a bit lost here. The code...
2
by: Kejpa | last post by:
Hi, I've got a number of objects that each have it's own thread. When the value changes I raise an event. Now, I want to handle the event in a form and show the value in a listview. With my first...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
6
by: Derek Hart | last post by:
I am using a status bar to show progress for a long database update. With the single threading, the GUI often gets stuck and does not show updating, especially when the user clicks to another...
0
by: Need2CSharp | last post by:
Hi All, Following is a quote from an article on MSDN. Article Title: Safe, Simple Multithreading in Windows Forms URL:...
2
by: lizii | last post by:
At the moment i am trying to write into a richtextbox using the Rtf call from another richtextbox. However realising that richtextbox do not have multithreading - i know i need to invoke this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.