如何在C#timer事件处理程序中显示消息? [英] How to show a message in C# timer event handler?

查看:161
本文介绍了如何在C#timer事件处理程序中显示消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VC#创建了一个计时器,工作似乎很好。问题是我必须在调用timer事件处理程序时显示消息。我发现MessageBox.Show无法正常工作。下面是我修改过的代码片段,其中我简化了事件处理程序,只显示了一条消息。

我的窗口是全屏和无边框。似乎显示的消息是由窗口的窗体隐藏的。

为什么MessageBox.Show()不能在timer事件处理程序中工作?如果我想在计时器事件发生时提示某事,我该怎么办?



I created a timer using VC# which working seems fine. The problem is that I must show message at the time when timer event handler is called. I found the MessageBox.Show not working. Below is my modified code snippet, in which I simplified the event handler as just showing a message.
My window is a full screen and borderless. It seems the message shown is hiden by window's form.
Why can't MessageBox.Show() doesn't work at timer event handler? How can I do if I would like to prompt something when timer event occurres?

void OnGridTmEvt(object source, ElapsedEventArgs e)
{
    MessageBox.Show("Test end");
}
void createGridTmer()
{
    gridFlashTmer = new System.Timers.Timer();
    gridFlashTmer.Elapsed += new ElapsedEventHandler(OnGridTmEvt);
    gridFlashTmer.Interval = Constants.FLASH_MS_COUNT;
    gridFlashTmer.Enabled = true;
}

推荐答案

查看以下链接,这不仅会为您提供有关问题的详细信息,还会提供解决方案(链接虽然讨论控制台应用程序,但你也可以在你的应用程序中做同样的事情)



控制台应用程序中所有窗口前面的消息框? [ ^ ]
Check below link, this will not only give you detail info about problem but also suggest solutions as well (The link though discuss console application but you may do the same in your application as well)

Message box in front of all windows in console application?[^]


请参阅此处: TopMost MessageBox [ ^ ]

有效;但我仍然想知道更简单的解决方案。在我看来,在所有窗口的顶部或至少当前窗口显示消息必须是Windows编程中非常流行的选项。为什么需要这么多代码来做呢?它应该只需要一个参数或一个或两个语句来设置它。
Refer here:TopMost MessageBox[^]
It works; but I still wonder the simpler solotion. In my opinion, showing message at top of all windows or at least the current window must be very popular option in Windows programming. How come it needs so lots of codes to do it? It should need only one parameter or one or two statement to set it.


这篇关于如何在C#timer事件处理程序中显示消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆