Click here to Skip to main content
15,893,381 members
Home / Discussions / C#
   

C#

 
AnswerLoop while mouse or key is down Pin
DerecL9-Feb-12 12:13
DerecL9-Feb-12 12:13 
GeneralRe: Loop while mouse or key is down Pin
Richard Andrew x649-Feb-12 12:17
professionalRichard Andrew x649-Feb-12 12:17 
GeneralRe: Loop while mouse or key is down Pin
BillWoodruff9-Feb-12 19:41
professionalBillWoodruff9-Feb-12 19:41 
GeneralRe: Loop while mouse or key is down Pin
Pete O'Hanlon9-Feb-12 12:18
mvePete O'Hanlon9-Feb-12 12:18 
GeneralRe: Loop while mouse or key is down Pin
raven198529-Feb-12 13:04
raven198529-Feb-12 13:04 
GeneralRe: Loop while mouse or key is down Pin
BillWoodruff9-Feb-12 19:42
professionalBillWoodruff9-Feb-12 19:42 
GeneralRe: Loop while mouse or key is down Pin
V.9-Feb-12 22:52
professionalV.9-Feb-12 22:52 
GeneralRe: Loop while mouse or key is down Pin
BillWoodruff9-Feb-12 20:09
professionalBillWoodruff9-Feb-12 20:09 
For example: a while loop running in a MouseDown EventHandler ... with no terminating condition ... is going to run "forever." The MouseUp EventHandler is never going to be called: and you lose control of your Application.

There are three strategies that could work here to avoid this "pitfall."

By the way, the same issues described here would apply in the case of using a specific key press to trigger a non-halting loop.

The technical reason for the "pitfall" is that an unterminated loop launched in a MouseDown EventHandler is going to block the thread ... in this case the main thread, which happens to be your application.

1. Raven has mentioned the threading option already: hope he'll flesh it out with some code. It will work !

2. Quick and nasty: create a WinForm project: note: this example will undoubtedly spawn a series of responses/sermons on the "utter evil" of using "Application.DoEvents," but I already hang garlic on my computer to avoid being attacked by its meme, myself Smile | :)

a. add a Button named "button1" : hook up the MouseDown and the MouseUp EventHandlers to the Button as shown below:

b. add this code:
private bool IsMouseDown = false;

private int tick = 0;

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    IsMouseDown = true;

    while (IsMouseDown)
    {
        tick++;
        // comment the line below out and observe what happens when you run the app
        Application.DoEvents(); // this is the nasty part, in case you don't know
    }
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    IsMouseDown = false;

    Console.WriteLine(String.Format("ticks = {0}", tick));
}
3. Use a Timer : activate the Timer in the MouseDown EventHandler; de-activate in the Mouse-Up EventHandler. Put the code to be done in the loop (assuming it has no built-in halt checking) inside the Timer's Tick EventHandler.
"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman


modified 10-Feb-12 2:43am.

GeneralRe: Loop while mouse or key is down Pin
Luc Pattyn9-Feb-12 23:32
sitebuilderLuc Pattyn9-Feb-12 23:32 
GeneralRe: Loop while mouse or key is down Pin
BobJanova10-Feb-12 0:22
BobJanova10-Feb-12 0:22 
GeneralRe: Loop while mouse or key is down Pin
Marcus_211-Feb-12 8:52
Marcus_211-Feb-12 8:52 
GeneralRe: Loop while mouse or key is down Pin
BobJanova9-Feb-12 22:06
BobJanova9-Feb-12 22:06 
QuestionTaskbar Icon (not tray icon) picturebox replace? Pin
Thomas von Smolinski9-Feb-12 10:26
Thomas von Smolinski9-Feb-12 10:26 
AnswerRe: Taskbar Icon (not tray icon) picturebox replace? Pin
Luc Pattyn9-Feb-12 10:57
sitebuilderLuc Pattyn9-Feb-12 10:57 
AnswerRe: Taskbar Icon (not tray icon) picturebox replace? Pin
DaveyM699-Feb-12 11:28
professionalDaveyM699-Feb-12 11:28 
AnswerRe: Taskbar Icon (not tray icon) picturebox replace? Pin
BobJanova10-Feb-12 4:21
BobJanova10-Feb-12 4:21 
GeneralRe: Taskbar Icon (not tray icon) picturebox replace? Pin
Thomas von Smolinski10-Feb-12 15:07
Thomas von Smolinski10-Feb-12 15:07 
QuestionDeleting the Palette Chunk from png stream Pin
Shailesh H9-Feb-12 7:59
Shailesh H9-Feb-12 7:59 
GeneralRe: Deleting the Palette Chunk from png stream Pin
harold aptroot9-Feb-12 8:35
harold aptroot9-Feb-12 8:35 
GeneralRe: Deleting the Palette Chunk from png stream Pin
Shailesh H9-Feb-12 20:59
Shailesh H9-Feb-12 20:59 
GeneralRe: Deleting the Palette Chunk from png stream Pin
harold aptroot9-Feb-12 23:45
harold aptroot9-Feb-12 23:45 
AnswerRe: Deleting the Palette Chunk from png stream Pin
BobJanova9-Feb-12 22:07
BobJanova9-Feb-12 22:07 
GeneralRe: Deleting the Palette Chunk from png stream Pin
Shailesh H9-Feb-12 22:58
Shailesh H9-Feb-12 22:58 
GeneralRe: Deleting the Palette Chunk from png stream Pin
BobJanova10-Feb-12 4:23
BobJanova10-Feb-12 4:23 
QuestionWhich approach is better for this problem? Pin
AmbiguousName9-Feb-12 4:23
AmbiguousName9-Feb-12 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.