473,725 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event handling precedence?

I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a
database through a timer. So the timer is setup and from it I call the
procedure that loads the latest records from the database. This works fine.

However, I attempt to notify the user when data accesses occur. The way I
attempt to accomplish this is by changing the background color of a label on
the form the user is looking at. I use red for when the database is opened,
yellow for data transfers and green for a successful completion of the
operation - and then I set the color back to its original gray background
after the operation.

If this is put inline in my code - for most (not all) accesses the notify
happens so fast you never see the color changes - so I want the change to
last for a second or so. So I set up another timer that essentially does:

if color = gray then set color red
if color = red then set color yellow
if color = yellow then set color green
if color = green then set color gray

This timer works perfectly in any piece of standalone code. However when I
set it from the routine that access the database, somehow the calls are
stacked and I do not see them occur until the end of the database access.

I initially suspected that the database I/O calls were occurring prior to
the timer event and that the I/O was blocking the event. However if I place
any type of debug statement in the code, the thing works. So now I am
wondering if it is caused by calling one timer event from a routine that was
launched from another timer event.

Is there anyway to force the order of event handling (on time - and without
putting message boxes in the code) so that it would work as you would
suspect based on code flow?

Thanks for any and all help/suggestions,
Paul Orman
pa********@comc ast.net

Nov 21 '05 #1
2 2134
Its a bit unclear from your code exactly how you are handling these
events. You must first recognize that the Event Handling precedence and
architecture varies drastically from that of the VB6 model. When a
Timer raises and event to the form - the handling function is executed
by the Timer thread - NOT - by the form. Thus, the Timer is executing
the function in parallel with the form. This is highly problematic when
it comes to updating the Form Interface without first alerting the
Forms main GUI Thread that an update is about to occur. This is because
the resources used by the GUI thread are not thread safe, and therefore
should NEVER be updated by a seperate thread.

Also you need to make sure that 2 seperate threads are not attempting
to update the same variable at the same time. This is handled throw
using a Mutex.

Approprate Event Handling for a GUI interface with extra threads is
handled in the following model. Declare a variable for the timer event
delegate. And an assocated function that will handle the actual update
to the interface (which is the AddressOf for the delegate). This
function Doesnt directly handle the Elepsed event. Add another function
which handled the elapsed event from the timer. Remember - the Timer
Thread is executing this function *NOT* the main gui thread. So what
you want to do is tell the Main Gui Thread to execute the function that
actually updates the interface. This is done by the mybase.Invoke (or
begin invoke if you dont want the Timer thread to wait for the Main Gui
thread to handle the update).

Private _d_onEventElaps ed as New
System.Timers.E lapsedEventHand ler(AddressOf onTimerElapsed)
Private Sub __TimerElapsed( ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles t.Elapsed
Try
mybase.Invoke(_ d_onEventElapse d, New Object() {sender, e})
Catch ex As Exception
#If DEBUG Then
Stop
#End If
End Try
End Sub
Private Sub onTimerElapsed( ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs )
Try
'UPDATE THE INTERFACE HERE - IT IS NOW EXECUTED BY THE MAIN
GUI THREAD
Catch ex As Exception
#If DEBUG Then
Stop
#End If
End Try
End Sub

Let me know if this helps, I can provide an example of a Threaded Data
object that the main gui requests the data to load, and the object
raises events when the query is complete. But, this example should get
you started down the correct path.

Kevin M. Schreiner
VP Software Architecture
Business Intelligence Force, Inc.

Nov 21 '05 #2
Thank you very much, after some testing this adequately explains what is
happening. I did not (and do not) understand the dotnet architecture (I
have some reading to do :-). I am assuming at this point based upon your
explanation and the behavior I am seeing that there is a single thread that
handles the timer events. To clarify the point I was unclear on ... I have
my main body of code (Form1) that has a timer. On the form load I start a
timer (Timer1). In the Handles Timer1.Tick function I call a sub
LoadFromDataBas e(). This routine starts another timer (Timer2) that then
sets the label colors in the Handles Timer2.Tick function. So apparently I
have a Timer thread nested inside of another Timer thread (that is already
running). I believe what I am seeing happen is the second timer thread
being serviced *after* completion of the first (and original) timer event.
Based upon your explanation and what I am seeing occur, I do not believe
this is going to work for me (as I had intended) and I will need to rework
this with an altered solution. Thank you once again for your explanation.

Paul Orman
pa********@comc ast.net

"kevin" <ke************ *@gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Its a bit unclear from your code exactly how you are handling these
events. You must first recognize that the Event Handling precedence and
architecture varies drastically from that of the VB6 model. When a
Timer raises and event to the form - the handling function is executed
by the Timer thread - NOT - by the form. Thus, the Timer is executing
the function in parallel with the form. This is highly problematic when
it comes to updating the Form Interface without first alerting the
Forms main GUI Thread that an update is about to occur. This is because
the resources used by the GUI thread are not thread safe, and therefore
should NEVER be updated by a seperate thread.

Also you need to make sure that 2 seperate threads are not attempting
to update the same variable at the same time. This is handled throw
using a Mutex.

Approprate Event Handling for a GUI interface with extra threads is
handled in the following model. Declare a variable for the timer event
delegate. And an assocated function that will handle the actual update
to the interface (which is the AddressOf for the delegate). This
function Doesnt directly handle the Elepsed event. Add another function
which handled the elapsed event from the timer. Remember - the Timer
Thread is executing this function *NOT* the main gui thread. So what
you want to do is tell the Main Gui Thread to execute the function that
actually updates the interface. This is done by the mybase.Invoke (or
begin invoke if you dont want the Timer thread to wait for the Main Gui
thread to handle the update).

Private _d_onEventElaps ed as New
System.Timers.E lapsedEventHand ler(AddressOf onTimerElapsed)
Private Sub __TimerElapsed( ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles t.Elapsed
Try
mybase.Invoke(_ d_onEventElapse d, New Object() {sender, e})
Catch ex As Exception
#If DEBUG Then
Stop
#End If
End Try
End Sub
Private Sub onTimerElapsed( ByVal sender As Object, ByVal e As
System.Timers.E lapsedEventArgs )
Try
'UPDATE THE INTERFACE HERE - IT IS NOW EXECUTED BY THE MAIN
GUI THREAD
Catch ex As Exception
#If DEBUG Then
Stop
#End If
End Try
End Sub

Let me know if this helps, I can provide an example of a Threaded Data
object that the main gui requests the data to load, and the object
raises events when the query is complete. But, this example should get
you started down the correct path.

Kevin M. Schreiner
VP Software Architecture
Business Intelligence Force, Inc.

Nov 21 '05 #3

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

Similar topics

1
3435
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the database. This works fine. However, I attempt to notify the user when data accesses occur. The way I attempt to accomplish this is by changing the background color of a label on the form the user is looking at. I use red for when the database is...
7
49580
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
1
20698
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function init() {
4
12576
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
18
2886
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
8
6085
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
21
3036
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as opposed to what is mentioned in precedence table? Also, with comma operator. consider,
4
9695
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person writing the JavaScript doesn't have to pass the index in the "onChange" event name. I thought that one might be able to use "this.value" or compare this as
0
8872
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9392
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9246
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8069
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6694
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5997
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2150
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.