c# - 在 Windows 窗体应用程序中 Hook 消息循环?

标签 c# winforms

我需要 Hook Windows 窗体应用程序中的消息循环来执行一些与滴答计数相关的工作。

我正在使用异步方法(async/await)开发一个 tcp 服务器,我避免在多线程进程中处理竞争条件——这基本上是我使用 async/await 而不是其他模型的原因.由于此限制,我无法创建后台线程来执行滴答计数,因为这样我将以多线程代码告终。

我认为的一个选择是以某种方式连接 Windows 窗体中的消息循环,这样我就可以使用我的 Windows 窗体对象的同一线程轻松地完成与滴答计数相关的事情,尽管我没有找到任何合适的方法或 Windows.Forms.Application 类中的事件。

有谁知道我需要的方法吗?

谢谢。

最佳答案

我认为您最好的方法可能是使用计时器,它会在定时间隔内执行您的代码:

var aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000;
aTimer.Enabled = true;



// What you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    // Do something
}

关于c# - 在 Windows 窗体应用程序中 Hook 消息循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824929/

相关文章:

c# - GC.AddMemoryPressure() 不足以按时触发 Finalizer 队列执行

c# - CaSTLe 项目每个 session 生活方式与 ASP.NET MVC

c# - Json.NET 不区分大小写的属性反序列化

C# - 使用网络带宽计算下载/上传时间

c# - 对于 35 张图像,如何在获取 1 个特定像素的颜色时获得最佳性能?

c# - 如何从 DataGridView 接收 "scroll box"类型的滚动事件?

c# - 如何修复 "The application is improperly formatted"错误?

c# - 为什么 GotFocus 事件不断从 Designer 中删除自身?

c# - 如何在 c#.net 中制作来电显示

.NET/WinForms - 清除 DataGridView 上的排序