Esempio n. 1
0
            IntPtr _work;                           //CreateThreadpoolWork. Not used with simple callbacks.

            /// <exception cref="Win32Exception"/>
            /// <exception cref="InvalidOperationException">(only when completionCallback is used) This thread has a synchronization context other than WindowsFormsSynchronizationContext or null; or it is null and thread's GetApartmentState is not STA.</exception>
            internal Work(object state, WorkCallback workCallback, SendOrPostCallback completionCallback, bool createWork)
            {
                _state        = state;
                _workCallback = workCallback;
                if (completionCallback != null)
                {
                    _completionCallback = completionCallback;
                    //we need WindowsFormsSynchronizationContext to call _completionCallback in this thread
                    _context = EnsureWindowsFormsSynchronizationContext_.EnsurePermanently();
                    //SHOULDDO: loads Forms dll. Try to avoid it. Also, test with WPF.
                }

                _gc = GCHandle.Alloc(this);
                Debug.Assert(sizeof(GCHandle) == IntPtr.Size);                 //we declare API IntPtr parameters as GCHandle

                if (createWork)
                {
                    _work = CreateThreadpoolWork(_workCallbackDelegate, _gc, ref _env);
                }
                else
                {
                    bool ok = TrySubmitThreadpoolCallback(_simpleCallbackDelegate, _gc, ref _env);
                    //Debug.Assert(ok);
                    if (!ok)
                    {
                        throw new Win32Exception();
                    }
                }
            }
Esempio n. 2
0
 public Task(TaskControl control, WorkCallback workproc, WaitCallback oncomplete, object state)
 {
     this.workproc   = workproc;
     this.oncomplete = oncomplete;
     this.control    = control;
     this.state      = state;
 }
Esempio n. 3
0
        private void mBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] parameters = (object[])e.Argument;

            int currentStepNumber = (int)parameters[0];
            var shapeValues       = (List <object>)parameters[1];

            Thread.CurrentThread.CurrentCulture   = (CultureInfo)parameters[2];
            Thread.CurrentThread.CurrentUICulture = (CultureInfo)parameters[3];

            e.Result = WorkCallback?.Invoke(currentStepNumber, shapeValues);
        }
Esempio n. 4
0
        /// <summary>
        ///     Breaks/Stops current work.
        /// </summary>
        internal void InternalBreakWork()
        {
            if (!WorkActive)
            {
                return;
            }

            // reset last work
            WorkActive    = false;
            WorkSessionId = 0;
            WorkCallback?.Invoke();
            WorkCallback = null;
        }
Esempio n. 5
0
    TaskControl AddUserTaskToQueue(WorkCallback workproc, WaitCallback oncomplete, object state)
    {
        TaskControl control = new TaskControl();

        if (thread == null)
        {
            thread = new WorkingThread();
        }

        thread.AddTask(new Task(control, workproc, oncomplete, state));

        return(control);
    }
Esempio n. 6
0
        public void Start(WorkCallback work, object state)
        {
            lock (this)
            {
                if (m_thread != null)
                {
                    throw new Exception("work is already started!");
                }

                m_state               = state;
                m_workCallback        = work;
                m_thread              = new Thread(new ThreadStart(WorkThread));
                m_thread.IsBackground = true;
                m_thread.Name         = "WorkThread";
                m_thread.Start();
            }
        }
Esempio n. 7
0
        public IAsyncResult BeginInvoke(object arg, AsyncCallback asyncCallback, object state, int timeout)
        {
            wrapper = delegate(object argv)
            {
                AutoResetEvent e = new AutoResetEvent(false);
                try
                {
                    TimeoutState waitOrTimeoutState = new TimeoutState(Thread.CurrentThread, state);
                    ThreadPool.RegisterWaitForSingleObject(e, WaitOrTimeout, waitOrTimeoutState, timeout, true);
                    return(workCallback(argv));
                }
                finally
                {
                    e.Set();
                }
            };
            IAsyncResult asyncResult = wrapper.BeginInvoke(arg, asyncCallback, state);

            return(asyncResult);
        }
        /// <summary>
        /// 开始调用委托
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="asyncCallback"></param>
        /// <param name="state"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public IAsyncResult BeginInvoke(object arg, AsyncCallback asyncCallback, object state, int timeout)
        {
            wrapper = delegate(object argv)
            {
                AutoResetEvent e = new AutoResetEvent(false);

                try
                {
                    TimeoutState waitOrTimeoutState = new TimeoutState(Thread.CurrentThread, state);

                    ThreadPool.RegisterWaitForSingleObject(e, WaitOrTimeout, waitOrTimeoutState, timeout, true);

                    return workCallback(argv);
                }
                finally
                {
                    e.Set();
                }
            };

            IAsyncResult asyncResult = wrapper.BeginInvoke(arg, asyncCallback, state);

            return asyncResult;
        }
        public void Start(WorkCallback work, object state)
        {
            lock (this)
            {
                if (m_thread != null)
                    throw new Exception("work is already started!");

                m_state = state;
                m_workCallback = work;
                m_thread = new Thread(new ThreadStart(WorkThread));
                m_thread.IsBackground = true;
                m_thread.Name = "WorkThread";
                m_thread.Start();
            }
        }
Esempio n. 10
0
		public Task(TaskControl	control, WorkCallback workproc, WaitCallback oncomplete, object state)
		{
			this.workproc = workproc;
			this.oncomplete = oncomplete;
			this.control = control;
			this.state = state;
		}
Esempio n. 11
0
 /// <summary>
 /// Creates a <see cref="Work"/> object that can be used when need more options than <see cref="SubmitCallback"/> has.
 /// </summary>
 /// <param name="state">Something to pass to the callback functions.</param>
 /// <param name="workCallback">Callback function to call in a thread pool thread.</param>
 /// <param name="completionCallback">Optional callback function to call in this thread after workCallback.</param>
 /// <remarks>
 /// Call Dispose() to avoid memory leaks. If not called, the object and related OS object remain in memory until this process ends.
 /// </remarks>
 /// <exception cref="Win32Exception"/>
 /// <exception cref="InvalidOperationException">(only when completionCallback is used) This thread has a synchronization context other than WindowsFormsSynchronizationContext or null; or it is null and thread's GetApartmentState is not STA.</exception>
 public static Work CreateWork(object state, WorkCallback workCallback, SendOrPostCallback completionCallback = null)
 {
     return(new Work(state, workCallback, completionCallback, true));
 }
Esempio n. 12
0
 /// <summary>
 /// Requests that a thread pool thread call the callback function.
 /// </summary>
 /// <param name="state">Something to pass to the callback functions.</param>
 /// <param name="workCallback">Callback function to call in a thread pool thread.</param>
 /// <param name="completionCallback">Optional callback function to call in this thread after workCallback.</param>
 /// <exception cref="Win32Exception"/>
 /// <exception cref="InvalidOperationException">(only when completionCallback is used) This thread has a synchronization context other than WindowsFormsSynchronizationContext or null; or it is null and thread's GetApartmentState is not STA.</exception>
 public static void SubmitCallback(object state, WorkCallback workCallback, SendOrPostCallback completionCallback = null)
 {
     new Work(state, workCallback, completionCallback, false);
 }
Esempio n. 13
0
	TaskControl AddUserTaskToQueue( WorkCallback workproc, WaitCallback oncomplete, object state )
	{
		TaskControl control = new TaskControl();
		
		if( thread == null )
			thread = new WorkingThread();

		thread.AddTask(new Task(control,workproc,oncomplete,state));
		
		return control;
	}
Esempio n. 14
0
 /// <summary>
 /// 实例化CancellableTask
 /// </summary>
 /// <param name="workCallback"></param>
 /// <param name="cancelCallback"></param>
 public CancellableTask(WorkCallback workCallback, CancelCallback cancelCallback)
 {
     this.workCallback   = workCallback;
     this.cancelCallback = cancelCallback;
 }
Esempio n. 15
0
 /// <summary>
 /// 实例化CancellableTask
 /// </summary>
 /// <param name="workCallback"></param>
 public CancellableTask(WorkCallback workCallback)
 {
     this.workCallback = workCallback;
 }
Esempio n. 16
0
 public ThreadPool(int maxThreads,
                   int maxQueuedItems,
                   WorkCallback /* ! */ workCallback)
Esempio n. 17
0
	public static TaskControl QueueUserWorkItem( WorkCallback workproc, WaitCallback oncomplete, object state )
	{
		return ThreadQueue.Instance.AddUserTaskToQueue(workproc, oncomplete, state);
	}
 /// <summary>
 /// 实例化CancellableTask
 /// </summary>
 /// <param name="workCallback"></param>
 /// <param name="cancelCallback"></param>
 public CancellableTask(WorkCallback workCallback, CancelCallback cancelCallback)
 {
     this.workCallback = workCallback;
     this.cancelCallback = cancelCallback;
 }
 /// <summary>
 /// 实例化CancellableTask
 /// </summary>
 /// <param name="workCallback"></param>
 public CancellableTask(WorkCallback workCallback)
 {
     this.workCallback = workCallback;
 }
Esempio n. 20
0
 public static TaskControl QueueUserWorkItem(WorkCallback workproc, WaitCallback oncomplete, object state)
 {
     return(ThreadQueue.Instance.AddUserTaskToQueue(workproc, oncomplete, state));
 }
Esempio n. 21
0
 public WorkItem(WorkCallback callback, object userObject)
 {
     this.callback   = callback;
     this.userObject = userObject;
 }