473,566 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.UpdateE vent += new UpdateEventDele gate(UpdateGui) ;
someObj.UpdateC ompleted += new UpdateCompleted Delegate(Update Compl);
---
---
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 3673
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.DoE vents() after you set the
label text. You can read about Application.DoE vents() 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.DoE vents(), 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.DoE vents() after you set the
label text. You can read about Application.DoE vents() 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*******@cana da.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.co m>
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
13234
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 CustomTextBox : TextBox, which has quite a lot of events... Do I put unregistering code for all the possible events in the Dispose method? Or is...
5
1689
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 you can get the events and overrides. But for some reason I can not seem to find the list of events and overrides for a C# form, if I do this. the...
3
506
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 to the main thread to be threadsafe. I want to make this worker class I'm writing a self contained assembly so that other's can drop it into their...
4
22862
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 when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc....
13
1930
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 indicates that the forms timer does not operate on its own thread and will not cause a query to be sent to use the single SqlConnection while another is...
30
3616
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 it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the...
1
1526
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 dropdown for the object (Page, btnpush, etc) and the right dropdown for the event we want. Simple! I was told that in C# the left dropdown has...
6
4796
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 ugly. Here's what I have so far 1. Implement via the 'event' keyword (with associated delegate, etc) 2. Expose a private delegate via a public...
5
4392
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
1420
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 obfuscated" delegate syntax. Moreover, you can accomplish everything with delegates that you can accomplish with events, such as combining instances, etc....
0
7584
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...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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...
0
7951
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 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...
1
5484
isladogs
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.