Synchronizing events from different threads in con

2019-05-07 12:01发布

问题:

I am feeling like a total noob asking this, but anyway, here it goes:

I was wondering what the easiest way of synchronizing events from different threads is.

Some sample code:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("# started on:" + Thread.CurrentThread.ManagedThreadId);
        tt t = new tt();

        t.First += new EventHandler(t_First);
        t.Second += new EventHandler(t_Second);

        Task task = new Task(new Action(t.Test));
        task.Start();

        while (true)
        {
            Console.ReadKey();
            Console.WriteLine("# waiting on:" + Thread.CurrentThread.ManagedThreadId);
        }
    }

    static void t_Second(object sender, EventArgs e)
    {
        Console.WriteLine("- second callback on:" + Thread.CurrentThread.ManagedThreadId);
    }

    static void t_First(object sender, EventArgs e)
    {
        Console.WriteLine("- first callback on:" + Thread.CurrentThread.ManagedThreadId);
    }

    class tt
    {
        public tt()
        {
        }

        public event EventHandler First;
        public event EventHandler Second;

        public void Test()
        {
            Thread.Sleep(1000);

            Console.WriteLine("invoked on:" + Thread.CurrentThread.ManagedThreadId);

            First(this, null);
            Thread.Sleep(1000);
            Second(this, null);
        }
    }
}

As you can probably guess, only the first writeline is executed on the main thread, the other calls are all executed on the new thread created by the task.

I'd like to synchronize the call to "Second" back on the main thread (it should never get called, because i am blocking in the while loop). However i'd like to know the way of doing this or if its even possible?

回答1:

You could try a BlockingCollection

BlockingCollection<Action> actions = new BlockingCollection<Action>();

void main() {
   // start your tasks

   while (true) {
       var action = actions.Take();

       action();
   }
}

static void t_First(object sender, EventArgs e) {
    string message = "- first callback on:" + Thread.CurrentThread.ManagedThreadId;
    actions.Add(_ => Console.WriteLine(message));
}


回答2:

If I understand you right, you are asking about how to execute code running on thread into different thread. i.e: you have two threads, while you are executing the second thread code you want to execute it in the first thread.

You can achieve this by using SynchronizationContext, for example if you want to execute code from another thread into main thread, you should use current synchronization context:

private readonly System.Threading.SynchronizationContext _currentContext = System.Threading.SynchronizationContext.Current;

private readonly object _invokeLocker = new object();

public object Invoke(Delegate method, object[] args)
{
    if (method == null)
    {
        throw new ArgumentNullException("method");
    }

    lock (_invokeLocker)
    {
        object objectToGet = null;

        SendOrPostCallback invoker = new SendOrPostCallback(
        delegate(object data)
        {
            objectToGet = method.DynamicInvoke(args);
        });

        _currentContext.Send(new SendOrPostCallback(invoker), method.Target);

        return objectToGet;
     }
}

While you are in the second thread, using this method will execute code on the main thread.

you can implement ISynchronizeInvoke "System.ComponentModel.ISynchronizeInvoke". check this example.

Edit: Because of you are running a Console application and so can't use SynchronizationContext.Current. then you probably need to design your own SynchronizationContext, this example might help.