0

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
        }
    }
}
Szabolcs Dézsi
  • 8,743
  • 21
  • 29
anilG
  • 19
  • 8

1 Answers1

0

If the "Execute" Method takes long time, the thread will not receive the stop event and the GUI-thread blocks in the join-Method. If you signal the Cancelation request through stopEvent, you have to evalute this event more often, that means also in the long running Execute-Method. A CancellationToken (https://msdn.microsoft.com/de-de/library/dd997364%28v=vs.110%29.aspx) sometimes is more usefull for this purpose, you have to pass it to every long-running function, in this case Execute. Many build in-function support cancellation through this token.

In case you want to update the WPF-GUI in the Optimizer thread use the Dispatcher to pass the request to the GUI-Thread: Using the C# Dispatcher

Community
  • 1
  • 1
APesa
  • 1
  • 1
  • thank u sir. i have could upon the link.. could u tell me i can implemnt it in my code of faster efficiency – anilG Jan 30 '16 at 16:02