bool gFlag = true;
Event sets flag to false, and causes thread to fall out of loop!
Simple but effective!
// while (true){
while ( gFlag ){
Don't sleep your thread so long.
If you need 1000ms timing then
int nSnooze = 1000;
while ( gFlag )
{
Sleep(25);
nSnooze -= 25;
if (nSnooze > 0)
{
continue;
}
nSnooze = 1000;
DoSomething();
}
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
gFlag is global.
In your event handler for mouse, set gFlag=false;
Thread falls out of while loop upon gFlag being false then exits.
There will be micro-delay between the flag being set and the thread reading the flag and falling out of its loop.
Just looked at your code again. Mouse event spawns the new thread?
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
Where does come from? If its never cleared then the false logic won't be executed!
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
I'm not sure that this is the issue, but just so you know, the while loop condition is only going to be checked after the method call that is inside the while loop completes. You probably knew that. Other than that, I'm not sure I understand what problem you're having. What you need to do is have a different thread (probably the main thread) detect the mouse event, then set a condition that lets the other thread know it's time to stop execution. The other thread should periodically check to see if it is time to stop execution.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
"I can't interact with the GUI and trigger mouse events that should interrupt the WorkThread, even if it goes through multiple iterations of the while loop."
So you're saying that once the WorkThread starts running, your program will not trigger mouse events? Or that your program triggers mouse events, but these mouse events do not interrupt the WorkThread like they're supposed to?
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
"I had my event handler print a message every time it detected a mouse event and it only prints once"
Like I said, you should have at least two threads: one would be your WorkThread, and the other would be your main thread, which presumably would run your code on the Event Dispatch Thread. The main thread would be the one responsible for the event handling. You should make sure that your code is running on the Event Dispatch Thread. . you can do so by calling SwingUtilities.isEventDispatchThread() from within the thread. Your "WorkThread" should return false when you call that method, and your main thread should return true when you call that method. So put that line of code into both your main thread code and your WorkThread code and tell me what it returns for each of them.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Btw, the reason I'm having you do that is because if your main code is running on the Event Dispatch Thread, like it should be, and if your "WorkThread" is also, which it should not be, then it could be blocking the thread, preventing any of your mouse clicks from registering. So I'm trying to make sure your Threads are actually set up correctly as separate Threads and that the one is running on the EDT. Thanks for being so patient.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
WorkThread should not be running on the EDT. This indicates that WorkThread is not running in a separate thread - so the new Thread was not set up properly. Look up how to create a separate Thread on google. You should also look into Swing's WorkerThread class.
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
This whole loop-with-a- sleep-in-it polling approach is a poor way to go, even if you get it to work. You want your thread to go into a wait state and wait passively until notified to exit the wait, which it should do without further dalay. Have a look at wait() and notify() - you'll find loads of info via Google.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
aThread.run();
is your problem. This calls the run method on the current thread.
You should use aThread.start(); twhich calls the run() method on a new thread
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Glad you found a solution. FYI, the easiest thing would have been to leave all your code as was (extending Thread), and simply call
new WorkThread.start();
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073