events
-
Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?
-
Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?
If you are writing a GUI application, the main GUI thread is event driven and will handle an event and execute all code for that handler before returning to the code that raised the event. If you are writing a multithreaded application, the background thread works independently of the main thread and would not be interrupted unless its listening to events as well.
-
Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?
1. your threads do whatever you make them do, and will continue to do so. And events get handled by either the main aka GUI thread, or one of the threadpool threads. See here[^]. 2. If you have long winding code that executes on the main thread (e.g. in a button click handler), then other GUI events (and Windows.Forms.Timer tick events, and more) will be stalled. That basically is why long winding operations should NOT be executed by the main thread. 3. If other threads need to access WinForm GUI components (Controls), you need to use Invoke. See here[^]. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
-
If you are writing a GUI application, the main GUI thread is event driven and will handle an event and execute all code for that handler before returning to the code that raised the event. If you are writing a multithreaded application, the background thread works independently of the main thread and would not be interrupted unless its listening to events as well.
AFAICT your suggestion has two major drawbacks, memory-wise: 1. it repeats the leading symbols over and over; 2. for each new chunk of license plates it costs a pointer. I'd consider an N-tier approach; for N=2 that would be some data structure for the rightmost say 3 symbols, and then a second layer of structure to hold all of those, representing the remaining symbols. The first one could be a run-length as you described, or a linked list, or switch from one to the other scheme depending on the population. The second one could be a full array, or maybe a dictionary. Maybe a 3-tier approach would be optimal: an array for 2 symbols (36^2 elements), pointing to an array for 2 symbols, pointing to an array for 2 symbols, holding a 36-bit bitmask for the last symbol. (Yes it would work better if there were only 32 symbols in the alphabet used). Note: the "pointers" above don't have to be real pointers, they could be indexes into a huge array, requiring say 4B each rather than 8B. And the simplest approach that might be good enough would be a Dictionary<string,long> where the key holds the first N-1 symbols, and the value is a bitmask covering the last symbol. :) PS: this is what I was going to reply to your vanishing question in Algos
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
-
AFAICT your suggestion has two major drawbacks, memory-wise: 1. it repeats the leading symbols over and over; 2. for each new chunk of license plates it costs a pointer. I'd consider an N-tier approach; for N=2 that would be some data structure for the rightmost say 3 symbols, and then a second layer of structure to hold all of those, representing the remaining symbols. The first one could be a run-length as you described, or a linked list, or switch from one to the other scheme depending on the population. The second one could be a full array, or maybe a dictionary. Maybe a 3-tier approach would be optimal: an array for 2 symbols (36^2 elements), pointing to an array for 2 symbols, pointing to an array for 2 symbols, holding a 36-bit bitmask for the last symbol. (Yes it would work better if there were only 32 symbols in the alphabet used). Note: the "pointers" above don't have to be real pointers, they could be indexes into a huge array, requiring say 4B each rather than 8B. And the simplest approach that might be good enough would be a Dictionary<string,long> where the key holds the first N-1 symbols, and the value is a bitmask covering the last symbol. :) PS: this is what I was going to reply to your vanishing question in Algos
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
Lol... saw that forum was dead, so I moved it over here :).
-
Lol... saw that forum was dead, so I moved it over here :).
That is exactly how forums die. :doh:
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
-
Hello, If i my application handles an event in the middle of other code running, does it return to previously running code after the event was handled? if not, how can i do it?
Beware of
Application.DoEvents()
! Using such a terrible line of code can cause chaos, e.g. a second click on the same button can "overtake" the first click somewhere in the middle of execution. While you can avoid those calls in your code, you'll never be safe when you have to use ThirdParty (often closed-source) code. -
That is exactly how forums die. :doh:
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
Luc Pattyn wrote:
That is exactly how forums die.
"Not with a ! (bang), but by forum transmigration," T.S. Elliot did not write in "Hollow Men."
"Our life is a faint tracing on the surface of mystery, like the idle, curved tunnels of leaf miners on the surface of a leaf. We must somehow take a wider view, look at the whole landscape, really see it, and describe what's going on here. Then we can at least wail the right question into the swaddling band of darkness, or, if it comes to that, choir the proper praise." Annie Dillard
-
Beware of
Application.DoEvents()
! Using such a terrible line of code can cause chaos, e.g. a second click on the same button can "overtake" the first click somewhere in the middle of execution. While you can avoid those calls in your code, you'll never be safe when you have to use ThirdParty (often closed-source) code.ok. can i raise event in a thread (Task) and handle this event in the main thread? so task will keep running when event is handled?
-
ok. can i raise event in a thread (Task) and handle this event in the main thread? so task will keep running when event is handled?
Not exactly (event handlers are always called in the same thread that called them) but if you're talking about a GUI application you can have event handlers that marshal the call to the UI thread if they're called on a different one:
void MyEventHandler(object sender, EventArgs ea){
if(InvokeRequired) {
BeginInvoke(new EventHandler(MyEventHandler), new object[] { sender, ea });
return;
}// ... normal, in-UI-thread code here
}Using BeginInvoke (instead of Invoke) causes the call to be asynchronous, i.e. the calling thread continues executing while the event handler runs. This is what you requested, but be aware of synchronisation issues with any form of parallel execution. Because you usually don't care about the result of a delegate call, you can ignore the return value and not bother calling EndInvoke at any point.
-
Not exactly (event handlers are always called in the same thread that called them) but if you're talking about a GUI application you can have event handlers that marshal the call to the UI thread if they're called on a different one:
void MyEventHandler(object sender, EventArgs ea){
if(InvokeRequired) {
BeginInvoke(new EventHandler(MyEventHandler), new object[] { sender, ea });
return;
}// ... normal, in-UI-thread code here
}Using BeginInvoke (instead of Invoke) causes the call to be asynchronous, i.e. the calling thread continues executing while the event handler runs. This is what you requested, but be aware of synchronisation issues with any form of parallel execution. Because you usually don't care about the result of a delegate call, you can ignore the return value and not bother calling EndInvoke at any point.
OK. So what will happen if i call an event that is handled in the main thread (not UI thread)?