How to invoke a function on parent thread in .NET?

后端 未结 6 1111
野的像风
野的像风 2020-12-24 07:19

I have a .NET class library containing a class with a method that performs some lengthy operation. When a client calls this method it should perform the lengthy operation on

6条回答
  •  粉色の甜心
    2020-12-24 07:46

    A thread cannot just execute stuff on another thread. The closest you can get is to put a delegate on a queue for the other thread to execute, but that assumes that the other thread is cooperating about this.

    In a WinForms application, the main loop looks for such queued messages on each loop iteration.

    If you just need to communicate the finishing of the worker thread, you can use e.g. a flag variabe. If the main thread should be able to wait for job termination, use a semaphore or condition variable (monitor).

提交回复
热议问题