C# WinForm 线程中调用按钮事件及程序优雅关闭最佳实践

xingyun86 2022-4-29 703

C# WinForm 线程中调用按钮事件及程序优雅关闭最佳实践

Action<Form> m_FormActionClose = (sender) => { ((Form)sender).Dispose(); };
Action<Button> m_ButtonActionClick = (sender) => { ((Button)sender).PerformClick(); };
Action<HttpViewToolForm> m_FormActionDoneCheck = (sender) => { ((HttpViewToolForm)sender).DoneCheck(); };
m_taskThread = new Thread(() =>
{
    m_bWorkDone = false;
    while(m_bWorkDone == false)
    {
        // Do what you want to do
        ...
        Invoke(m_ButtonActionClick, buttonGet2);
        //m_ButtonActionClick.Invoke(buttonGet2);
        Invoke(m_FormActionDoneCheck, this);
        //m_FormActionDoneCheck.Invoke(this);
        Thread.Sleep(Convert.ToInt32(TimeSpan.TicksPerSecond / TimeSpan.TicksPerMillisecond));
    }
    if (m_bWorkDone == true)
    {
        Invoke(m_FormActionClose, this);
        //m_FormActionClose.Invoke(this);
    }
});
m_taskThread.Start();
this.FormClosing += (s, e) =>
{
    if (m_taskThread.IsAlive == true)
    {
        e.Cancel = true;
    }
    m_bWorkDone = true;
};


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回