Communication between c# child thread and main thread

Communication between c# child thread and main thread

Communication between c# sub-thread and main thread_HonorDuan's Blog-CSDN Blog_c# Inter-Thread Communication

HonorDuan

Released at 2017-10-18 11:28:45

11275Favorites22CategoriesColumn : c#Article tags
 :
Thread Communicationc#
Copyright

The c#
column contains this content
2 articles 0 Subscribe Subscribe to the
column
How to communicate between the c# sub-thread and the main thread
A new thread is created when the message is sent. This leads to the fact that the message processing is not performed on the main thread, but some of the steps can only be operated through the main thread. This leads to how does a sub-thread notify the main thread what to do?
In order to solve the above problem, I found a lot of good information, and many of them were solved by entrusting. Then I looked at my project and solved it through this method of beginInvoker, which is not very useful. Does not solve the current problem, at this time I found the SynchronizationContext object. The purpose of this object is to record the context of a thread and after the sub-thread is processed, you can post or send an event to solve it when you need to use the main program to operate, which is very convenient. The code is as follows:
1
2
                / /This is back to the main thread
            //do something
        }

        private void DoWork()//This is executed by the workThread thread
        {             //Do something here (connection or something...)

            //this is done

            mainThreadSynContext.Post(new SendOrPostCallback(OnConnected), null);//Notify the main thread
        }
    }

  class TestClient
    {
        private Thread workThread;
        private SynchronizationContext mainThreadSynContext;

        public TestClient()//The constructor is of course executed by the main thread
        {             mainThreadSynContext = SynchronizationContext.Current; //Record the context of the main thread here             workThread = new Thread(new ThreadStart(DoWork));//Create a new thread         }


        private void OnConnected(object state)//Since it is called by the synchronization object Post of the main thread, this is executed in the main thread {//
        Here             we are back to the main thread             //Do some things         }


        private void DoWork()//This is executed by the workThread thread
        {             //Do something here (connection or something...)

            //this is done

            mainThreadSynContext.Post(new SendOrPostCallback(OnConnected), null);//Notify the main thread
        }
    }
———————————————————
Copyright Notice: This article is the original article of CSDN blogger "HonorDuan" , following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/qq_23127851/article/details/78270988

I hereby record the above code so that I can check it in the future, and I hope it can help people who have the same problem as me.

– The more you know, the more you don't know, so learn more!
 

Guess you like

Origin blog.csdn.net/u014090257/article/details/124730269