| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Concurrent;
- using System.Threading;
- namespace ET
- {
- public class ThreadSynchronizationContext : SynchronizationContext
- {
- public static ThreadSynchronizationContext Instance { get; } = new ThreadSynchronizationContext(Thread.CurrentThread.ManagedThreadId);
- private readonly int threadId;
- // 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
- private readonly ConcurrentQueue<Action> queue = new ConcurrentQueue<Action>();
- private Action a;
- public ThreadSynchronizationContext(int threadId)
- {
- this.threadId = threadId;
- }
- public void Update()
- {
- while (true)
- {
- if (!this.queue.TryDequeue(out a))
- {
- return;
- }
- try
- {
- a();
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- }
- }
- public override void Post(SendOrPostCallback callback, object state)
- {
- this.Post(() => callback(state));
- }
-
- public void Post(Action action)
- {
- if (Thread.CurrentThread.ManagedThreadId == this.threadId)
- {
- try
- {
- action();
- }
- catch (Exception e)
- {
- Log.Error(e);
- }
- return;
- }
- this.queue.Enqueue(action);
- }
-
- public void PostNext(Action action)
- {
- this.queue.Enqueue(action);
- }
- }
- }
|