用C# 调用winAPI的 settimer()后,不执行回调函数!! 具体程序如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace APITimerForm
{
class timer
{
}
class TimerCallbackTest
{
private delegate void TimerProc(IntPtr hWnd, uint nMsg, int nIDEvent, int dwTime);
[DllImport("user32 ")]
private static extern int SetTimer(IntPtr hwnd, int nIDEvent, int uElapse, TimerProc CB);
[DllImport("user32 ")]
private static extern int KillTimer(IntPtr hwnd, int nIDEvent);
private int ID_CB_Timer = 0;
private int Cnt_CB_Timer = 0;
private TimerProc Cb = null;
private string _s = string.Empty;
public void startTimer(string s)
{
Cb = new TimerProc(this.OnTickTimer);
_s=_s + s + "/";
ID_CB_Timer = SetTimer(IntPtr.Zero, 0, 10, Cb);
}
public void stopTimer(ref string s)
{
s = _s;
KillTimer(IntPtr.Zero, this.ID_CB_Timer);
}
private void OnTickTimer(IntPtr hWnd, uint nMsg, int nIDEvent, int dwTime)
{
++Cnt_CB_Timer;
_s =_s+"/"+Cnt_CB_Timer+"over";
}
}
}
调用程序 另一个窗体中
private void btmOK_Click(object sender, EventArgs e)
{
TimerCallbackTest t = new TimerCallbackTest();
string s = richTextBox1.Text;
t.startTimer(s);
Thread.Sleep(200);
t.stopTimer(ref s);
richTextBox1.Text = s;
}
希望大家试试!