C# 我必须中止System.Threading.Thread吗? 我想知道在我完成它之后,是否必须停止一个(UI)线程(从一个本地C++项目调用)。或者我可以让它过去,让ASP.NET为我做这项工作吗

C# 我必须中止System.Threading.Thread吗? 我想知道在我完成它之后,是否必须停止一个(UI)线程(从一个本地C++项目调用)。或者我可以让它过去,让ASP.NET为我做这项工作吗,c#,wpf,multithreading,C#,Wpf,Multithreading,我这样调用并创建System.Threading.Thread Thread viewerThread = new Thread(delegate () { Window window = new MyCustomWPFWindow(); window.Show(); System.Windows.Threading.Dispatcher.Run(); }); this.UIThread = viewerThread; viewerThread.SetApartmen

我这样调用并创建
System.Threading.Thread

Thread viewerThread = new Thread(delegate ()
{
    Window window = new MyCustomWPFWindow();
    window.Show();
    System.Windows.Threading.Dispatcher.Run();
});

this.UIThread = viewerThread;

viewerThread.SetApartmentState(ApartmentState.STA);
viewerThread.Start();

我知道有一个
Thread.Abort()
方法,但是我真的必须中止它吗,或者有更好的方法吗,或者我可以放手让.NET来处理这个问题。

你应该避免
Thread.Abort()
,只有当第三方组件阻塞线程时才使用这个方法。即便如此,还是要弄清楚它为什么会挂起。(
Thread.Abort()
==Evil)您应该始终在不使用
Abort()的情况下设计线程


如果要停止线程,应该关闭调度程序。下面是一个关于如何实现它的示例

// a field to store the guiDispatcher.
Dispatcher guiDispatcher;

// wait event.
ManualResetEvent dispatcherInitialized = new ManualResetEvent(false);

Thread viewerThread = new Thread(delegate ()
{
    Window window = GetDialog(configuration);
    window.Show();
    // get a/the dispatcher of this thread.
    guiDispatcher = Dispatcher.CurrentDispatcher;

    // dispatcher initialized. Set wait event.
    dispatcherInitialized.Set();

    // run dispatcher.
    System.Windows.Threading.Dispatcher.Run();
});

viewerThread.SetApartmentState(ApartmentState.STA);
viewerThread.Start();
dispatcherInitialized.WaitOne();

// ......

// when you want to terminate the thread, just shutdown the dispatcher.
guiDispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
// you might want to wait until the thread is terminated.
viewerThread.Join();


如果调度程序自行停止,线程将被清理。如果调度程序没有停止(这会阻止应用程序终止),则可以添加
viewerThread.IsBackground=true

您应该避免
线程。中止()
,仅当第三方组件阻止线程时才使用此选项。即便如此,还是要弄清楚它为什么会挂起。(
Thread.Abort()
==Evil)您应该始终在不使用
Abort()的情况下设计线程


如果要停止线程,应该关闭调度程序。下面是一个关于如何实现它的示例

// a field to store the guiDispatcher.
Dispatcher guiDispatcher;

// wait event.
ManualResetEvent dispatcherInitialized = new ManualResetEvent(false);

Thread viewerThread = new Thread(delegate ()
{
    Window window = GetDialog(configuration);
    window.Show();
    // get a/the dispatcher of this thread.
    guiDispatcher = Dispatcher.CurrentDispatcher;

    // dispatcher initialized. Set wait event.
    dispatcherInitialized.Set();

    // run dispatcher.
    System.Windows.Threading.Dispatcher.Run();
});

viewerThread.SetApartmentState(ApartmentState.STA);
viewerThread.Start();
dispatcherInitialized.WaitOne();

// ......

// when you want to terminate the thread, just shutdown the dispatcher.
guiDispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
// you might want to wait until the thread is terminated.
viewerThread.Join();


如果调度程序自行停止,线程将被清理。如果调度程序没有停止(这会阻止应用程序终止),则可以添加
viewerThread.IsBackground=true

当一个线程完成它正在运行的例程时,它会在
ASP.NET
中自动处理它自己的UI线程?更奇怪的是,ASP中的哪个窗口哪个GetDialog哪个DIspatcher?你是说WPF吗?哦。。或者,可能是您偶然从asp web应用程序中创建并显示了一些窗口?当线程完成其正在运行的例程时,它将自动在asp.NET中处置其自己的UI线程。
asp.NET
?更奇怪的是,asp中的哪个窗口什么GetDialog什么DIspatcher?你是说WPF吗?哦。。或者可能是您偶然从asp web应用程序中创建并显示了一些窗口?在您的示例中是
窗口。show
阻止主UI线程?@toddmo nope,
窗口。show()
不会阻止线程。它不是显示的模型(比如
ShowDialog()
System.Windows.Threading.Dispatcher.Run()
将阻止线程(它是一个messageloop),并将由
BeginInvokeShutdown()
终止。在您的示例中是
窗口。Show
阻止主UI线程?@toddmo nope,
窗口。Show()
不会阻止线程。它不是显示的模型(比如
ShowDialog()
System.Windows.Threading.Dispatcher.Run()将阻止线程(它是一个messageloop),并将由
BeginInvokeShutdown()