Exemplo n.º 1
0
		/// <summary>
		/// Executes the delegate on the main thread that this DelegateQueue executes on.
		/// </summary>
		/// <param name="method">
		/// A Delegate that contains a method to call, in the context of the thread for the DelegateQueue.
		/// </param>
		/// <param name="args">
		/// An array of type Object that represents the arguments to pass to the given method.
		/// </param>
		/// <returns>
		/// An Object that represents the return value from the delegate being invoked, or a 
		/// null reference (Nothing in Visual Basic) if the delegate has no return value.
		/// </returns>
		/// <remarks>
		/// <para>Unlike BeginInvoke, this method operates synchronously, that is, it waits until 
		/// the process completes before returning. Exceptions raised during the call are propagated 
		/// back to the caller.</para>
		/// <para>Use this method when calling a method from a different thread to marshal the call 
		/// to the proper thread.</para>
		/// </remarks>
		public object Invoke(Delegate method, params object[] args)
		{
			if (method == null)
			{
				throw new ArgumentNullException("method");
			}

			if (InvokeRequired)
			{
				DelegateQueueAsyncResult result = new DelegateQueueAsyncResult(this, method, args);
				lock (lockObject)
				{
					if (disposed)
					{
						throw new ObjectDisposedException(string.Empty, OBJECT_DISPOSED_EXCEPTION);
					}

					delegateQueue.Enqueue(result);
					Monitor.Pulse(lockObject);
				}
				return EndInvoke(result);
			}
			// Invoke the method here rather than placing it in the queue.
			return method.DynamicInvoke(args);
		}
Exemplo n.º 2
0
		/// <summary>
		/// Executes the delegate on the main thread that this DelegateQueue executes on.
		/// </summary>
		/// <param name="method">
		/// A Delegate to a method that takes parameters of the same number and type that 
		/// are contained in args. 
		/// </param>
		/// <param name="args">
		/// An array of type Object to pass as arguments to the given method. This can be 
		/// a null reference (Nothing in Visual Basic) if no arguments are needed. 
		/// </param>
		/// <returns>
		/// An IAsyncResult interface that represents the asynchronous operation started 
		/// by calling this method.
		/// </returns>
		/// <remarks>
		/// <para>The delegate is called asynchronously, and this method returns immediately. 
		/// You can call this method from any thread. If you need the return value from a process 
		/// started with this method, call EndInvoke to get the value.</para>
		/// <para>If you need to call the delegate synchronously, use the Invoke method instead.</para>
		/// </remarks>
		public IAsyncResult BeginInvoke(Delegate method, params object[] args)
		{
			if (method == null)
			{
				throw new ArgumentNullException("method", OBJECT_DISPOSED_EXCEPTION);
			}

			DelegateQueueAsyncResult result = new DelegateQueueAsyncResult(this, method, args);
			lock (lockObject)
			{
				if (disposed)
				{
					throw new ObjectDisposedException(string.Empty, OBJECT_DISPOSED_EXCEPTION);
				}

				delegateQueue.Enqueue(result);
				Monitor.Pulse(lockObject);
			}
			return result;
		}