I running this algorithm where when I Press Stop on my GUI it hangs and then I pressed pause on my VS just to see where my code cursor was and it was pointing or more or less struck on thread.join(). and the inner exception was thread is waiting or sleep.. this happens for some 30 sec and the GUI works normal later..
how do i make the gui to not hang instead let code thread runs and once done changes should be reflected back on 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;
}
}
}
Here is my algo which the above code is trying to run:
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
}
}
}