Example #1
0
        public object Invoke(Delegate d, params object[] args)
        {
            // Schedule synchronous task

            AutoResetEvent taskDone = new AutoResetEvent(false);

            lock (_taskDoneEvents) {
                _taskDoneEvents.Add(taskDone);
            }

            NikonTask task = new NikonTask(d, args, taskDone);

            EnqueueTask(task);

            taskDone.WaitOne();

            lock (_taskDoneEvents) {
                _taskDoneEvents.Remove(taskDone);
            }

            // If an exception occurred during execution of the task (on the worker thread), re-throw
            // it here, on the waiting thread.
            if (task.Exception != null)
            {
                throw task.Exception;
            }

            return(task.Result);
        }
Example #2
0
        public void Run()
        {
            // Enter the run loop
            while (!_shuttingDown)
            {
                // Get next task in queue
                NikonTask task = null;

                lock (_tasks) {
                    if (_tasks.Count > 0)
                    {
                        task = _tasks.Dequeue();
                    }
                }

                // If we have a task, execute it. Otherwise wait for a new task to arrive
                if (task != null)
                {
                    task.Execute();

                    // Store first exceptions caused by an asynchronous task
                    if (task.Exception != null && !task.IsSynchronous && _asyncException == null)
                    {
                        Interlocked.Exchange <Exception>(ref _asyncException, task.Exception);
                    }
                }
                else
                {
                    _haveTask.WaitOne();
                }
            }
        }
Example #3
0
        public void BeginInvoke(Delegate d, params object[] args)
        {
            // Schedule asynchronous task

            NikonTask task = new NikonTask(d, args);

            EnqueueTask(task);
        }
Example #4
0
        private void EnqueueTask(NikonTask task)
        {
            // Add task to queue
            lock (_tasks) {
                _tasks.Enqueue(task);
            }

            // Signal the run loop that we have a pending task
            _haveTask.Set();
        }
        void EnqueueTask(NikonTask task)
        {
            // Add task to queue
            lock (_tasks)
            {
                _tasks.Enqueue(task);
            }

            // Signal the run loop that we have a pending task
            _haveTask.Set();
        }
        public object Invoke(Delegate d, params object[] args)
        {
            // Schedule synchronous task

            AutoResetEvent taskDone = new AutoResetEvent(false);

            lock (_taskDoneEvents)
            {
                _taskDoneEvents.Add(taskDone);
            }

            NikonTask task = new NikonTask(d, args, taskDone);

            EnqueueTask(task);

            taskDone.WaitOne();

            lock (_taskDoneEvents)
            {
                _taskDoneEvents.Remove(taskDone);
            }

            // If an exception occurred during execution of the
            // task (on the worker thread), re-throw it here, on
            // the waiting thread.
            if (task.Exception != null)
            {
                throw task.Exception;
            }

            return task.Result;
        }
        public void BeginInvoke(Delegate d, params object[] args)
        {
            // Schedule asynchronous task

            NikonTask task = new NikonTask(d, args);

            EnqueueTask(task);
        }