【发布时间】:2016-01-30 23:35:42
【问题描述】:
我运行这个算法,当我在我的 GUI 上按下 Stop 时,它挂起,然后我在 VS 上按下暂停,只是为了查看我的代码光标在哪里,它指向或或多或少地击中 thread.join()。并且内部异常是线程正在等待或睡眠..这发生了大约 30 秒,GUI 稍后可以正常工作..
如何使 gui 不挂起,而是让代码线程运行,并且一旦完成更改应反映在 GUI 上
namespace ILS.PLMOptimization
{
public class PLMOptimizationLoop : Disposable
{
private Thread PLMOptimizerThread;
private AutoResetEvent stopEvent;
public IOptimizer Optimizer { get; private set; }
private static PLMOptimizationLoop instance;
//Singleton Pattern Used
public static PLMOptimizationLoop Instance
{
get { return (instance ?? (instance = new PLMOptimizationLoop())); }
}
protected override void Dispose(bool disposing)
{
SafeDispose(ref stopEvent);
base.Dispose(disposing);
}
private void OptimizationThread()
{
if (Optimizer == null)
throw new NullReferenceException("Optimizer");
Optimizer.Initialize();
while (true)
{
// if stopped externally
if (this.stopEvent.WaitOne(1000))
break;
// execute optimizer, continue to call execute only if the optimizer requires it
if (this.Optimizer.Execute())
continue;
// set thread to null only if exiting on its own
// in case stop was called the caller will wait for this thread to exit
// and then make the thread object null
this.PLMOptimizerThread = null;
break;
}
Optimizer.Shutdown();
}
private PLMOptimizationLoop()
{
this.stopEvent = new AutoResetEvent(false);
this.PLMOptimizerThread = null;
}
int a = 0;
public void Start(IOptimizer optimizer)
{
a = 1;
if (optimizer == null)
throw new ArgumentNullException("optimizer");
this.Optimizer = optimizer;
this.PLMOptimizerThread = new Thread(OptimizationThread);
this.PLMOptimizerThread.Start();
}
public void Stop()
{
if (this.PLMOptimizerThread == null)
return;
this.stopEvent.Set();
this.PLMOptimizerThread.Join();// **********the problem seems to be here ****//
this.PLMOptimizerThread = null;
}
}
}
这是我上面代码试图运行的算法:
namespace ILS.PLMOptimization.Algorithm
{
public class PLMOptimizationAlgorithm : IOptimizer
{
public void Initialize()
{
//somecode here
}
public bool Execute()
{
//somecode here
}
public void Shutdown()
{
//somecode here
}
public int ResetLuminaire_To_DefaultStateonSTop()
{
//somecode here
}
public PLMOptimizationAlgorithm()
{
//somecode here
}
}
}
【问题讨论】:
-
Thread.Join 的意思是“嘿,当前线程,请等待其他线程完成”。在您的情况下,当前线程是调度程序线程,因此 UI 挂起。
-
感谢您的回复先生,我可以改进的代码样式的修改是什么
-
在线程中执行 Stop() 函数
标签: wpf multithreading