我有一个旧的Windows Forms应用程序,在许多地方对数据库进行一些搜索。有时候它需要很长时间,所以我决定在wpf中创建一个加载屏幕,以向用户显示在单独的线程中正在加载某些内容。基本上只是一个带有加载指示器(一个旋转的圆圈)的完全透明的窗口。在我的主机计算机和虚拟机上一切正常,但当我尝试部署到我们的演示环境时,就像它开始加载,指示器出现,几秒钟后它消失了,应用程序停止响应,好像永远无法恢复。我最初的想法是它与GPU加速有关,它无法处理透明度,但它会显示几秒钟,所以这不可能是问题。所以很可能是我做错了什么。下面您可以看到我的代码,您是否注意到任何可能出错/导致死锁或其他问题的地方?
public class LoadingManager
{
public LoadingManager()
{ }
public LoadingManager(string LoadingText)
{
loadingText = LoadingText;
}
private string loadingText = "Please wait ..";
private Thread thread;
private bool ThreadReadyToAbort = false;
private BusyIndicatorView loadingWindow;
public void BeginLoading()
{
this.thread = new Thread(this.RunThread);
this.thread.IsBackground = true;
this.thread.SetApartmentState(ApartmentState.STA);
this.thread.Start();
}
public void EndLoading()
{
if (this.loadingWindow != null)
{
this.loadingWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{ this.loadingWindow.Close(); }));
while (!this.ThreadReadyToAbort) { }; // I also tried to remove this while but it didn't help
}
this.thread.Abort();
}
public void RunThread()
{
this.loadingWindow = new BusyIndicatorView();
loadingWindow.tbLoadingCaption.Text = loadingText;
this.loadingWindow.Closing += new System.ComponentModel.CancelEventHandler(waitingWindow_Closed);
this.loadingWindow.ShowDialog();
}
void waitingWindow_Closed(object sender, System.ComponentModel.CancelEventArgs e)
{
Dispatcher.CurrentDispatcher.InvokeShutdown();
this.ThreadReadyToAbort = true;
}
编辑.
我注意到在这些机器上,通常(有时候第一次点击也会失败)只要我第一次点击搜索,它就可以正常工作。如果我再次点击,它会显示一秒钟然后消失,应用程序停止响应。因此,似乎线程没有被关闭,调度程序关闭失败了?但是没有抛出任何异常...