Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当GUI需要一段时间时,等待对话框_C#_Winforms_Multithreading_Thread Abort - Fatal编程技术网

C# 当GUI需要一段时间时,等待对话框

C# 当GUI需要一段时间时,等待对话框,c#,winforms,multithreading,thread-abort,C#,Winforms,Multithreading,Thread Abort,我有一个应用程序,可能需要很长时间来执行一些GUI更新。我不能在后台线程中运行它,因为长时间的处理与更新GUI组件相关,而更新GUI组件只能在主线程中完成 因此,我创建了一个helper类,它将在后台线程中创建和显示WaitDialog表单,直到GUI完成更新 我的助手类如下所示: public class ProgressWaitDialogHelper : IDisposable { private Thread _thread = null; public void Sh

我有一个应用程序,可能需要很长时间来执行一些GUI更新。我不能在后台线程中运行它,因为长时间的处理与更新GUI组件相关,而更新GUI组件只能在主线程中完成

因此,我创建了一个helper类,它将在后台线程中创建和显示WaitDialog表单,直到GUI完成更新

我的助手类如下所示:

public class ProgressWaitDialogHelper : IDisposable
{
    private Thread _thread = null;

    public void ShowDialog()
    {
        ThreadStart threadStart = new ThreadStart(ShowDialogAsync);
        _thread = new Thread(threadStart);
        _thread.SetApartmentState(ApartmentState.STA);
        _thread.Start();
    }

    public void Dispose()
    {
        if ((_thread != null) &&
            (_thread.IsAlive))
        {
            _thread.Abort();
        }
    }

    private void ShowDialogAsync()
    {
        ProgressWaitDialog waitDialog = new ProgressWaitDialog();
        waitDialog.ShowDialog();
    }
}
using (ProgressWaitDialogHelper waitDialog = new ProgressWaitDialogHelper())
{
    waitDialog.ShowDialog();

    // Do long running GUI task on main thread here.
}
在我的主GUI窗口中调用helper类的代码如下所示:

public class ProgressWaitDialogHelper : IDisposable
{
    private Thread _thread = null;

    public void ShowDialog()
    {
        ThreadStart threadStart = new ThreadStart(ShowDialogAsync);
        _thread = new Thread(threadStart);
        _thread.SetApartmentState(ApartmentState.STA);
        _thread.Start();
    }

    public void Dispose()
    {
        if ((_thread != null) &&
            (_thread.IsAlive))
        {
            _thread.Abort();
        }
    }

    private void ShowDialogAsync()
    {
        ProgressWaitDialog waitDialog = new ProgressWaitDialog();
        waitDialog.ShowDialog();
    }
}
using (ProgressWaitDialogHelper waitDialog = new ProgressWaitDialogHelper())
{
    waitDialog.ShowDialog();

    // Do long running GUI task on main thread here.
}
这似乎工作得很好,看起来完全像我想要的GUI。WaitDialog表单是模态的,它会阻止对主GUI表单的访问,直到它完成更新。一旦长时间运行的GUI任务完成,它将退出Using块,从而调用helper类上的Dispose方法,而helper类又将调用线程上的Abort


我的问题是,有没有一种更优雅的方式来终止线程,还是一种更好的方式来实现相同的行为?

我建议在工作线程上进行实际工作,并在GUI线程上显示waitdialog。然后,您可以在工作完成时向主线程发送信号,工作完成后,线程可以正常终止,您可以像终止任何其他对话框一样终止waitdialog。

这将不起作用,因为长时间运行的任务与在主线程上创建的GUI组件有关。无法在工作线程上更新GUI组件。由于主线程正忙于更新GUI,因此无法创建模式waitdialog,因为这样GUI控件将无法更新。据我所知,waitdialog需要在worker线程中创建。@用户在你的问题中没有这样说,我怎么知道,但如果是这样的话,我想知道这是在GUI组件上做的什么工作?你应该知道这一点,因为我在开篇文章的前两行中提到了这一点“我有一个应用程序,执行一些GUI更新可能需要很长时间。我不能在后台线程中运行它,因为长时间的处理与更新GUI组件相关,而更新GUI组件只能在主线程中完成。“我真的不明白我怎么能比这更清楚。关于提供实际GUI组件工作的详细信息,我将更详细地介绍,但我认为这实际上并不重要,也无助于提供解决方案。