vc計時器數字重疊 VC計時器的乙個使用心得

2021-10-13 00:27:12 字數 2207 閱讀 8877

最近使用vc的計時器。計時器一般是先設定乙個timer,然後響應wm_timer訊息,然後銷毀計時器。但是我發現在**設定計時器和銷毀計時器是有講究的。

開始我的**是這樣的:

cmainframe::cmainframe()

// todo: add member initialization code here

settimer(1, 5000, 0);

// wm_timer訊息響應函式

void cmainframe::ontimer(uint nidevent)

// todo: add your message handler code here and/or call default

afxmessagebox("hello world!");

cframewnd::ontimer(nidevent);

cmainframe::~cmainframe()

killtimer(1);

編譯結果是,debug模式編譯下出現assertion fauled,

if (!processshellcommand(cmdinfo))

return false;

發生的。

但是在release模式編譯成功。想來是因為release模式忽略assert巨集的緣故,但是執行程式卻沒有hello world!的對話方塊彈出。

後來我想莫非是settimer(1, 5000, 0);函式放置的地方不對的緣故。後來我把它

框架類的oncreate函式。

int cmainframe::oncreate(lpcreatestruct lpcreatestruct)

if (cframewnd::oncreate(lpcreatestruct) == -1)

return -1;

if (!m_wnd*******.createex(this, tbstyle_flat, ws_child | ws_visible | cbrs_top

| cbrs_gripper | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic) ||

!m_wnd*******.load*******(idr_mainframe))

trace0("failed to create *******/n");

return -1; // fail to create

if (!m_wndstatusbar.create(this) ||

!m_wndstatusbar.setindicators(indicators,

sizeof(indicators)/sizeof(uint)))

trace0("failed to create status bar/n");

return -1; // fail to create

// todo: delete these three lines if you don't want the ******* to

// be dockable

m_wnd*******.enabledocking(cbrs_align_any);

enabledocking(cbrs_align_any);

dockcontrolbar(&m_wnd*******);

settimer(1, 5000, 0); // 在這裡設定計時器

return 0;

這樣編譯debugm通過, hello world!的對話方塊也出來了。但是在程式退出時檢測到有記憶體洩露:detected memory leaks!

我估計在銷毀計時器的**有問題。於是我把放到響應wm_destroy訊息的函式裡。

void cmainframe::ondestroy()

cframewnd::ondestroy();

// todo: add your message handler code here

killtimer(1);

為什麼設定計時起不能放在視窗類建構函式,銷毀計時器不能放在視窗類的析構函式裡?讓我們看看mfc的原始碼:

cwnd::settimer(uint nidevent, uint nelapse,

void (callback* lpfntimer)(hwnd, uint, uint, dword))

果然有乙個assert巨集。而且這個巨集很明確地告訴我們必須在視窗控制代碼有效的時候才能設定計時器。同樣道理,在銷毀計時器時也必須確保視窗控制代碼是有效的。

python計時器 python 計時器

import sysclassshowtime qwidget def init self super init self.istimestart false 標記時間是否開始計時 self.setwindowtitle qlable 顯示計時時間 self.lable time val qlabe...

python計時器單位 python計時器類

import time as t class mytimer def init self self.unit 年 月 日 時 分 秒 self.prompt 未開始計時 self.lasted self.start 0 self.stop 0 def str self return self.pro...

伺服器計時器與 Windows 計時器

說明 此文大部分內容摘自msdn,本人只是做了一點整理。如果有什麼異議,一切以msdn為準。一 引言 在 visual studio net 中有兩種計時器控制項 基於伺服器的計時器和標準的基於 windows 的計時器。基於 windows 的計時器為在 windows 窗體應用程式中使用而進行了...