Aganstrong
Aganstrong
2021-01-16 19:03

线程间操作无效: 从不是创建控件“TheadWaitForm”的线程访问它。

  • c#
  • asp.net

执行Close(),就报错,怎么办?

//ThreadManager
//关键代码1
private static Action<Action, bool> _onUiExec = delegate (Action action, bool sync)
{
    action();
};

//关键代码2
public static void OnUiThread(Action action, bool isSync = true)
{
    _onUiExec(action, isSync);
}

//关键代码3
ThreadManager.OnUiThread(delegate
{
    using (TheadWaitForm wait = new TheadWaitForm()) 
    {
        wait.ThreadAction = delegate {};
        wait.ShowDialog();
    }
}, false);

//关键代码4  TheadWaitForm 
Thread _thread = new Thread((ThreadStart)delegate
{
    if (ThreadAction())
    {
        ThreadManager.OnUiThread(delegate
        {
            if (!base.IsDisposed)
            {
                base.DialogResult = DialogResult.OK;
                Close();
            }
        });
    }
}

 

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐

换一换