Re: Need explaination of BeginInvoke timing

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



sippyuconn wrote:
Found some code that works for me to allow me to highlight text when I re-enter a textbox.

That code seems pretty suspicious to me.

I was curious if someone could explain how BeginInvoke is working???

Basically, it just queues the delegate invocation on the regular message queue for the control's owning thread. As the thread processes the messages, when that invocation comes to the front of the queue, it's executed.

There is no specific ordering mechanism involved, though of course if you have knowledge of what events have already been placed in the queue, you know that the invocation will be executed after those events.

I know it causes execution in a separate thread but how is the timing set to ensure that the bool is reset after after all the window events {enter, mouseup} that are working on the original bool value are complete ????

Actually, while you don't post enough code to be certain, it appears that the event handlers are just handlers for the regular Control events Enter, MouseUp, and MouseDown. If so, then none of the code gets executed "in a separate thread". All of the code you posted is being executed in the control's owning thread.

So basically, what the code is doing is using the thread's message queue as a sort of signaling mechanism. It is making the assumption that the Enter event will be raised before the MouseDown event corresponding to the mouse action that caused the control to receive focus, and that the MouseDown event is already in the message queue. The latter assumption seems reasonable; I'm less sure about the former.

The code is setting the flag, queuing a delegate that resets the flag which will be executed after all of the messages that are already in the queue (which in this case is assumed to include the mouse down event), and then the remaining events are processed, including the MouseDown event and the delegate invocation that was queued via BeginInvoke. The flag is automatically reset once all events that were in the queue at the time the invocation was queued have been processed.

Now, this all may work but I have a couple of issues with it. The first is that I don't see anywhere that documents the order of the events. I think it's safe to assume that the MouseDown event is going to happen before the delegate invocation; after all, it was the underlying mouse action that presumably caused the Enter event to be raised in the first plcae. However, I don't see any documentation that guarantees that the Enter event will be raised before the MouseDown event. Since these both are a consequence of the same underlying mouse action, it is conceivable to me that the order could be different in the future.

The second issue is that it seems overly complicated for whatever the actual goal is. The code appears to be trying to ensure that if you click on a text control to set the focus, that the text will be completely highlighted. But it's very fragile (subject to assumptions regarding the order of events being raised _and_ how the user interacts with the control), and as you've found it's less-than-intuitive as far as how it works goes. It may be the minimal amount of code required to implement the functionality, but it's hardly the most obvious.

I actually have a bit of a third issue with it, and it's the same issue I have when I run across any code that interferes with the normal UI response in a standard control: it is a very bad thing to change the UI response in a standard control. We have standardized UI for a reason, and that is so that the UI can follow the very important "rule of least surprise". The edit control already has a well-defined and well-known way to fully select the text when the user clicks in it (double-click), and the code you posted will, when it works, prevent another common user interaction with the control, namely doing a drag-select on the text when the control isn't already the one with focus.

I'm not sure what's scarier: that even one person would want to implement this UI, or that enough people have implemented this UI that it was possible to dig up sample code for the change. :)

Pete
.



Relevant Pages

  • Re: Call Center application in Visual Basic
    ... Just to clear things out for me because I am new to the field of TAPI. ... Can any 3rd party model TSP for the specific PBX work with? ... > Please note that you need additional hardware (optiset E data or control ... >> of statistics on a queue, such the number of calls currently queued. ...
    (microsoft.public.win32.programmer.tapi)
  • Re: Controling Event Sequencing...
    ... event queue and have it execute AFTER the OnClick in completely done ... ... control exposes this event to the programmer to allow him / her to capture ... current system event has completed... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Call Center application in Visual Basic
    ... I don't know of any other except its ancestor "Hicom CTI TAPI". ... Please note that you need additional hardware (optiset E data or control ... No it doesn't support lineGet/SetAgentXxx functions. ... > of statistics on a queue, such the number of calls currently queued. ...
    (microsoft.public.win32.programmer.tapi)
  • Re: ipc/msg.c "cleanup" breaks fakeroot on Alpha
    ... avoid taking a lock on a message queue that just got freed? ... queue after a grace period. ... "volatile" shouln't be necessary. ... or the target thread was destroyed before wake_up_process completed. ...
    (Linux-Kernel)
  • Re: Multithreading in .NET CF
    ... BackgroundWorker class for the .NET CF that works with an input queue. ... I don't know people with the information I need, because either don't use .NET or because they have not learnt any multithreading basics. ... SPECIFICATIONS: ... It's because I'm doing a custom control that loads images in background when a path is specified. ...
    (microsoft.public.dotnet.framework.compactframework)