Is there a good method in C# for throwing an exception on a given thread

前端 未结 8 1487
刺人心
刺人心 2020-12-16 01:22

The code that I want to write is like this:

void MethodOnThreadA()
{
    for (;;)
    {
        // Do stuff
        if (ErrorConditionMet)
            ThrowO         


        
8条回答
  •  抹茶落季
    2020-12-16 02:26

    Like the others, I'm not sure that's such a good idea, but if you really want to do it, then you can create a subclass of SynchronizationContext that allows posting and sending delegates to the target thread (if it's a WinForms thread the work is done for you as such a subclass already exists). The target thread will have to implement some sort of a message pump equivalent though, to receive the delegates.

提交回复
热议问题