Home » U++ Library support » U++ Callbacks and Timers » how to use timer id? and how to kill a timer
Re: how to use timer id? [message #27533 is a reply to message #27525] |
Thu, 22 July 2010 10:58   |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
I just cannot understand what it is you are trying to do 
You are correct that my previous example wasn't threadsafe. Here is a threadsafe version. As far as I can tell ALL timers will executed in the GUI thread.
#include "CtrlLib/CtrlLib.h"
using namespace Upp;
class MyWindow : public TopWindow
{
typedef MyWindow CLASSNAME;
int count;
public:
MyWindow() : count(0) {}
virtual void Paint(Draw& w) { TopWindow::Paint(w); w.DrawText(4, 4, AsString(count)); }
void Count() { ASSERT(Thread::IsMain()); ++count; Refresh(); }
};
void MyThread(Callback cb)
{
TimeCallback timer;
for (int i = 0; i < 50; ++i) {
if (Thread::IsShutdownThreads())
return;
Sleep(100);
}
timer.Set(-1000, cb);
for (int i = 0; i < 50; ++i) {
if (Thread::IsShutdownThreads())
return;
Sleep(100);
}
timer.Kill();
}
GUI_APP_MAIN
{
MyWindow wnd;
Thread thrd;
wnd.SetRect(RectC(0, 0, 200, 200));
wnd.CenterScreen();
thrd.Run(callback1(MyThread, callback(&wnd, &MyWindow::Count)));
wnd.Run();
Thread::ShutdownThreads();
}
Quote: | The problem is, I do not know whether the timer is already envoked when I kill it
|
Of course. You can be sure that the timer has been created, but not whether it has been executed. If you need guaranteed excution than you should call the function directly (using GuiLock if it needs GUI access)
Quote: | Plus, why did you test Thread::IsShutdownThreads()?
|
Because I call Thread::ShutDownThreads before closing. This ensures that all threads are finished, otherwise I get heap leaks from dangling threads. I check IsShutdownThreads so that I can terminate the thread prematurely, if you take them out you'd have to wait for the thread to finished naturally.
|
|
|
 |
|
how to use timer id? and how to kill a timer
By: bonami on Tue, 20 July 2010 10:03
|
 |
|
Re: how to use timer id?
By: mrjt on Wed, 21 July 2010 10:46
|
 |
|
Re: how to use timer id?
By: bonami on Wed, 21 July 2010 11:28
|
 |
|
Re: how to use timer id?
By: mrjt on Wed, 21 July 2010 12:54
|
 |
|
Re: how to use timer id?
By: bonami on Thu, 22 July 2010 04:09
|
 |
|
Re: how to use timer id?
By: mrjt on Thu, 22 July 2010 10:58
|
 |
|
Re: how to use timer id?
By: bonami on Thu, 22 July 2010 11:32
|
 |
|
Re: how to use timer id?
By: mrjt on Thu, 22 July 2010 12:10
|
 |
|
Re: how to use timer id?
By: bonami on Fri, 23 July 2010 04:28
|
 |
|
Re: how to use timer id?
By: mrjt on Fri, 23 July 2010 11:32
|
 |
|
Re: how to use timer id?
By: bonami on Mon, 26 July 2010 04:16
|
Goto Forum:
Current Time: Fri Jul 18 03:17:07 CEST 2025
Total time taken to generate the page: 0.04278 seconds
|