gpt4 book ai didi

c# - 是否有 ExecuteWhenThisThreadIsIdle 方法?

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:42 24 4
gpt4 key购买 nike

我遇到过好几次了。我的应用程序收到一些事件(例如 WndProc)并需要快速返回。需要运行的代码需要一些时间,并且不需要在从事件处理程序返回之前执行。

我目前的解决方案是启动一个短时间的计时器,并在 Tick 事件中运行该代码。

但对于这种情况,这似乎是错误的工具,并且容易出现一些错误(例如多次运行代码)。

那么,有没有ExecuteWhenThisThreadIsIdle方案呢?

编辑

最好是 C#/.NET 解决方案,但也欢迎使用特定于框架的解决方案。 主要是 Winforms。(还有 WPF、UWP、Xamarin.Forms,...)

代码需要与事件处理程序在同一个线程上运行。 (通常是 UI 线程。)

最佳答案

在 WinForms 中,您可以订阅 Application.Idle

例如……

private bool _wndProcEventHooked = false;

protected override void WndProc(ref Message m) {
// Run your code here
}

private void Application_Idle(Object sender, EventArgs e) {
if (!this._wndProcEventHooked) {
// Hook your wndProc event here.
this._wndProcEventHooked = true;
}
}

关于c# - 是否有 ExecuteWhenThisThreadIsIdle 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com