コード例 #1
0
        private bool AttachProcessor(OperationProcessor newProcessor)
        {
            if (this.thread != Thread.CurrentThread.ManagedThreadId)
            {
                Log <OperationProxy> .Logger.FatalFormat("Thread Unsafe code AttachProcessor : [{0} != {1} ({2})]", this.thread, Thread.CurrentThread.ManagedThreadId, this.Processor);
            }
            if (this.processor != null)
            {
                Log <OperationProxy> .Logger.ErrorFormat("duplicate processor : [{0} on {1}]", newProcessor, this.processor);

                this.DetachProcessor();
                return(false);
            }
            this.processor = newProcessor;
            if (this.processor == null)
            {
                return(true);
            }
            Log <OperationProxy> .Logger.DebugFormat("Attach {0}({1})", newProcessor.GetType().Name, newProcessor.Operation.GetType().Name);

            JobProcessor loop = JobProcessor.Current;

            this.processor.OnSend += delegate(object msg)
            {
                if (msg is ISynchronizable)
                {
                    ISynchronizable synchronizable = (ISynchronizable)msg;
                    synchronizable.OnFinished += this.OnSynchronizableFinished;
                    synchronizable.OnSync();
                    return;
                }
                if (this.Sending != null)
                {
                    this.Sending(msg);
                }
            };
            if (OperationProxy.timeReportEnabled)
            {
                this.timer = Stopwatch.StartNew();
            }
            if (this.processor.Operation.TimeOut != 0)
            {
                this.timeOutSchedule = Scheduler.Schedule(loop, Job.Create <OperationProcessor>(new Action <OperationProcessor>(this.OnTimeOut), this.processor), this.processor.Operation.TimeOut);
            }
            if (this.ProcessorAttached != null)
            {
                try
                {
                    this.ProcessorAttached(this, this.processor);
                }
                catch (Exception ex)
                {
                    Log <OperationProxy> .Logger.Error("ProcessorAttached", ex);

                    this.DetachProcessor();
                    return(false);
                }
            }
            this.processor.Start();
            return(true);
        }
コード例 #2
0
 public static void Sync(this ISynchronizable sync, Action <ISynchronizable> callback)
 {
     sync.OnFinished += callback;
     sync.OnSync();
 }