-1

I have two thread which start running When user select play button. But when use select Pause or Resume button my UI hangs because of Thread.Join().

Below is code, I am looking for some alternative to overcome this. I already tried with Invoke but it's not working even with this approach my UI freeze the moment I call Thread methods.

ThreadStart m_executeThreadStart;
        Thread m_executeThread;
        //user Selected Start Button
        private void Start()
        {
            m_executeThreadStart = new ThreadStart(method1);
            m_executeThread = new Thread(m_executeThreadStart);
            m_executeThread.Name = "ExecuteTestSession";
            m_executeThread.IsBackground = true;
            m_executeThread.Start();

            // Start the asynchronous operation.
            // InitializeBackgroundWorker();
            // backgroundWorker1.RunWorkerAsync();

            //Creating result sync thread
            ThreadStart m_resultSyncThreadStart = new ThreadStart(method2);
            Thread m_resultSyncThread = new Thread(m_resultSyncThreadStart);
            m_resultSyncThread.Name = "SyncResultDatabase";
            m_resultSyncThread.Start();
        }
        private void method1()
        {
            //do some work
            //read data from OPC sever (device)
        }
        private void method2()
        {
            //do some work
            //updated database accordingly method 1 data
        }

        //user Press Pause button
        public void Suspend()
        {
            //do work
            m_executeThread.Join();
            //do work
        }

        //user Press Resume button
        public void Resume()
        {
            //do work
            m_executeThread.Join();
            //do work
        }
VARUN NAYAK
  • 656
  • 5
  • 15
  • 36
  • It's unclear why you're performing the `Join` at all. Are you in some way trying to shut down the thread from `Suspend`/`Pause` before `Join`? If so, you've completely removed the details of *how* you're doing this from the code you've posted. If not, then why do you expect the thread to have exited when you hit those points? – Damien_The_Unbeliever Jun 07 '17 at 10:39
  • @Damien_The_Unbeliever i am trying to complete database updating process when user click on pause for amount of data which read from method 1. for example if i have 1000 data and after reading 50 data user click on pause i want to update for 50 data. – VARUN NAYAK Jun 07 '17 at 11:06

2 Answers2

-1

You can add an event handler to your background worker, then you don't need Thread.Join(). Your event handler can then perform the necessary actions when the long running process has completed:

backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     // code to run after long running process has completed
}

The async / await pattern was designed to stop the UI from freezing (when implemented correctly) you could use this as an alternative approach - https://learn.microsoft.com/en-us/dotnet/csharp/async

Also see: https://learn.microsoft.com/en-us/windows/uwp/debug-test-perf/keep-the-ui-thread-responsive

J45
  • 374
  • 3
  • 14
-1

i founded solution , below is link for answer.

How to pause/suspend a thread then continue it?

ManualResetEvent mrse = new ManualResetEvent(true);    

 ThreadStart m_executeThreadStart;
        Thread m_executeThread;
        //user Selected Start Button
        private void Start()
        {
            m_executeThreadStart = new ThreadStart(method1);
            m_executeThread = new Thread(m_executeThreadStart);
            m_executeThread.Name = "ExecuteTestSession";
            m_executeThread.IsBackground = true;
            m_executeThread.Start();

            // Start the asynchronous operation.
            // InitializeBackgroundWorker();
            // backgroundWorker1.RunWorkerAsync();

            //Creating result sync thread
            ThreadStart m_resultSyncThreadStart = new ThreadStart(method2);
            Thread m_resultSyncThread = new Thread(m_resultSyncThreadStart);
            m_resultSyncThread.Name = "SyncResultDatabase";
            m_resultSyncThread.Start();
        }
        private void method1()
        {
            //do some work
            //read data from OPC sever (device)
              mrse.WaitOne();
        }
        private void method2()
        {
            //do some work
            //updated database accordingly method 1 data
              mrse.WaitOne();
        }

        //user Press Pause button
        public void Suspend()
        {
            //do work
            mrse.Reset();
            //do work
        }

        //user Press Resume button
        public void Resume()
        {
            //do work
             mrse.Set();
            //do work
        }

Appreciated your help.

VARUN NAYAK
  • 656
  • 5
  • 15
  • 36