gpt4 book ai didi

c# - 只要窗体处于事件状态,如何运行后台线程?

转载 作者:行者123 更新时间:2023-12-03 13:20:50 24 4
gpt4 key购买 nike

在我的program.cs文件中,代码如下:

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmWizard frm = new frmWizard();
Application.Run(frm);

Thread th = new Thread(frm.ThreadSetCom);
th.Start();
}

ThreadSetCom是一种以无限循环方式运行以检查某些内容的方法。我注意到ThreadSetCom仅在WinForm出现之前和关闭窗体之后才执行。当窗体可见时,它不会执行。谁能解决这个问题吗?

最佳答案

Application.Run等到参数中传递的表单关闭。您可能需要在frmWizard的create事件中对start进行threadload编码。

private void frmWizard_Load(object sender, System.EventArgs e)
{
Thread th = new Thread(ThreadSetCom);
th.Start();
}

关于c# - 只要窗体处于事件状态,如何运行后台线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774555/

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