【发布时间】:2016-01-17 23:37:29
【问题描述】:
我有这个自定义消息框类:
public class AutoCloseMsb
{
readonly System.Threading.Timer _timeoutTimer;
readonly string _caption;
private AutoCloseMsb(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout)
{
new AutoCloseMsb(text, caption, timeout);
}
private void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow("#32770", _caption);
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WmClose, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
private const int WmClose = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
}
我在几个地方一个接一个地称呼它:
AutoCloseMsb.Show("Bot 1 Turn", "Turns", ThinkTime);
AutoCloseMsb.Show("Bot 2 Turn", "Turns", ThinkTime);
AutoCloseMsb.Show("Bot 3 Turn", "Turns", ThinkTime);
AutoCloseMsb.Show("Bot 4 Turn", "Turns", ThinkTime);
AutoCloseMsb.Show("Bot 5 Turn", "Turns", ThinkTime);
变量 ThinkTime 正在从我实际更改它的资源中获取价值。但是,例如,如果我将 3000 毫秒作为显示时间,它将显示第一个 3 秒,而其他在此期间不会显示,它们将在 100-200 毫秒内关闭(它们只是显示并立即关闭)为什么这发生了吗?我应该在显示每个消息框后重置变量的值吗?
【问题讨论】:
-
每次调用
ThinkTime的值是多少? -
一直是 3000 我的课有什么不对吗?
标签: c# winforms timer messagebox