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

Events and GUI question:

Hi,

I'm trying to update my GUI when an event is raised in my one of my
dlls.

my example code is:
-------------------
In GUI:
someLable.Text = " ";
---
---
someObj.UpdateEvent += new UpdateEventDelegate(UpdateGui);
someObj.UpdateCompleted += new UpdateCompletedDelegate(UpdateCompl);
---
---
UpdateGui()
{
someLable.Text = "Updated";
}
UpdateCompl()
{
someLable.Text = "Completed";
}
----------------------------------

In my dll:
i = 0;
while( i < 5 )
{
...
...
j = 0;
while( j < 2 )
{
....
....
j ++;
}
UpdateEvent();
i ++;
}
UpdateCompleted();
------------------------

Even though UpdatedEvent() is being raised, the changes are not
being reflected in the GUI. I mean, the changes made by the
UpdateEvent handler are not being reflected in the GUI.
But, GUI is reflecting the changes made by
UpdateCompleted event handler.

I can't understand why. What could be the problem.

Kindly let me know,

Cheers,

Naveen.

Nov 16 '05 #1
3 3660
If you are calling your DLL directly from the GUI, then you have only
one thread, the UI thread, doing all of the work.

Since a single thread can do only one thing at a time, your
UpdateEvent() is being raised, and the event handler is being called,
but simply setting someLabel.Text to something doesn't cause the GUI to
be redrawn immediately.

In fact, what happens is that the someLabel.Text = "Updated"; line
changes a property of the label (its text), and then sets the label as
"stale" in the GUI, so that the next time the UI thread gets around to
looking to see what needs repainting, it will find the "stale" label
and redraw it.

However, since the UI thread is busy inside your DLL doing processing
work, it doesn't get back to its redrawing the GUI until later... in
fact, not until after it finishes what it's doing in your DLL and
returns to your main form and then out... back to what's call the
"event loop". Only then does it notice that the label is stale and
needs to be redrawn.

One alternative is to call Application.DoEvents() after you set the
label text. You can read about Application.DoEvents() here:

http://msdn.microsoft.com/library/de...ventstopic.asp

However, this can have unforseen consequences: you can't tell
DoEvents() what GUI events to handle. It does them all before returning
to you. So, if your use has clicked another button while you're doing
your processing work, the UI thread will call the event handler for
that button, even while you're "in the middle of" your update, so you
can end up doing tasks within tasks. Could get ugly.

The other choice (the one recommended by Microsoft) is to use a
background thread for your processing work, leaving the UI thread free
to repaint the screen. You can still raise events and change the screen
from the background thread... you just need to use Invoke, because a
background thread can't mess with controls on the screen.

However, having worked in multi-threaded environments before, I can
tell you that you really don't want to get into background threads
unless you have plenty of time to experiment and learn. Although it is
the "right solution," it's even more complicated than calling
DoEvents().

Anyway, that's the long-winded answer: why doesn't the UI thread
repaint your label while you're doing your update? Because it's busy
doing the update.

Nov 16 '05 #2
Hi Bruce,

Thank you very much for taking time to explaining.
It is very specific and good explanation, much appriciated.

I tried Application.DoEvents(), it worked.

Thanks once again.

Cheers,

Naveen.

"Bruce Wood" wrote:
If you are calling your DLL directly from the GUI, then you have only
one thread, the UI thread, doing all of the work.

Since a single thread can do only one thing at a time, your
UpdateEvent() is being raised, and the event handler is being called,
but simply setting someLabel.Text to something doesn't cause the GUI to
be redrawn immediately.

In fact, what happens is that the someLabel.Text = "Updated"; line
changes a property of the label (its text), and then sets the label as
"stale" in the GUI, so that the next time the UI thread gets around to
looking to see what needs repainting, it will find the "stale" label
and redraw it.

However, since the UI thread is busy inside your DLL doing processing
work, it doesn't get back to its redrawing the GUI until later... in
fact, not until after it finishes what it's doing in your DLL and
returns to your main form and then out... back to what's call the
"event loop". Only then does it notice that the label is stale and
needs to be redrawn.

One alternative is to call Application.DoEvents() after you set the
label text. You can read about Application.DoEvents() here:

http://msdn.microsoft.com/library/de...ventstopic.asp

However, this can have unforseen consequences: you can't tell
DoEvents() what GUI events to handle. It does them all before returning
to you. So, if your use has clicked another button while you're doing
your processing work, the UI thread will call the event handler for
that button, even while you're "in the middle of" your update, so you
can end up doing tasks within tasks. Could get ugly.

The other choice (the one recommended by Microsoft) is to use a
background thread for your processing work, leaving the UI thread free
to repaint the screen. You can still raise events and change the screen
from the background thread... you just need to use Invoke, because a
background thread can't mess with controls on the screen.

However, having worked in multi-threaded environments before, I can
tell you that you really don't want to get into background threads
unless you have plenty of time to experiment and learn. Although it is
the "right solution," it's even more complicated than calling
DoEvents().

Anyway, that's the long-winded answer: why doesn't the UI thread
repaint your label while you're doing your update? Because it's busy
doing the update.

Nov 16 '05 #3
Bruce Wood <br*******@canada.com> wrote:

<snip>
However, having worked in multi-threaded environments before, I can
tell you that you really don't want to get into background threads
unless you have plenty of time to experiment and learn. Although it is
the "right solution," it's even more complicated than calling
DoEvents().


It's certainly more complicated than calling DoEvents if DoEvents is
going to work - however, I think that judging when DoEvents is going to
work involves learning nearly as much as learning how to do it
properly. Threading is one of those things that most people are going
to need to learn how to do properly at some stage or other - I think
it's worth avoiding the "band-aid" of DoEvents (which can cause really
hard to diagnose bugs - re-entrancy is harder to spot than deadlocks,
IMO) in the first place. Just MHO, obviously.

To the OP - if you want to know more about multi-threading, see
http://www.pobox.com/~skeet/csharp/threads

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

6
by: Saso Zagoranski | last post by:
Hi! How can I unregister all the events registered to a control? I have seen a piece of code in another thread, which gets all the registered handlers for a specific event. Let's say I have a...
5
by: ECVerify.com | last post by:
This should be a basic question. In VB.NET in the two drop downs over the source code for a form you can get a list of the events and overrides for that form. In VC++ in the properties window...
3
by: Jacob | last post by:
I'm working on a class that needs to be called from a windows form, do it's work, and then, show progress back to the main form. I'm well aware that worker threads need to call Invoke for updates...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
13
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
1
by: Tina | last post by:
I'm a VB.Net developer learning C# so this is another newbie question.... (using 1.1) I posted a question about how to paste definitions for events in C# because in VB we use the vs.net left...
6
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and...
5
by: Sin Jeong-hun | last post by:
class Manager { public event ItemEventHandler ItHappened; public Manager { Item i; i.ItHappend+=new ItemEventHandler(OnItHappened); } void OnItHappened(...) {
5
by: puzzlecracker | last post by:
I took a closer look at delegates and events, and it became apparent that events don't offer any extra functionalities over delegates. I don't even see it as syntactical sugar over "overtly...
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...
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.