예제 #1
0
		public void Get(ManagementOperationObserver watcher)
		{
			this.Initialize(false);
			if (this.path == null || this.path.Path.Length == 0)
			{
				throw new InvalidOperationException();
			}
			else
			{
				if (watcher != null)
				{
					IWbemServices wbemServices = this.scope.GetIWbemServices();
					ObjectGetOptions objectGetOption = ObjectGetOptions._Clone(this.options);
					WmiGetEventSink newGetSink = watcher.GetNewGetSink(this.scope, objectGetOption.Context, this);
					if (watcher.HaveListenersForProgress)
					{
						objectGetOption.SendStatus = true;
					}
					SecurityHandler securityHandler = this.scope.GetSecurityHandler();
					int objectAsync_ = this.scope.GetSecuredIWbemServicesHandler(wbemServices).GetObjectAsync_(this.path.RelativePath, objectGetOption.Flags, objectGetOption.GetContext(), newGetSink.Stub);
					if (securityHandler != null)
					{
						securityHandler.Reset();
					}
					if (objectAsync_ < 0)
					{
						watcher.RemoveSink(newGetSink);
						if (((long)objectAsync_ & (long)-4096) != (long)-2147217408)
						{
							Marshal.ThrowExceptionForHR(objectAsync_);
						}
						else
						{
							ManagementException.ThrowWithExtendedInfo((ManagementStatus)objectAsync_);
							return;
						}
					}
					return;
				}
				else
				{
					throw new ArgumentNullException("watcher");
				}
			}
		}
        //******************************************************
        //Get
        //******************************************************
        /// <summary>
        ///    <para> Binds to the management object asynchronously.</para>
        /// </summary>
        /// <param name='watcher'>The object to receive the results of the operation as events.</param>
        /// <remarks>
        ///    <para>The method will issue the request to get the object
        ///       and then will immediately return. The results of the operation will then be
        ///       delivered through events being fired on the watcher object provided.</para>
        /// </remarks>
        /// <example>
        ///    <code lang='C#'>ManagementObject o = new ManagementObject("MyClass.Name='abc'");
        /// 
        /// //Set up handlers for asynchronous get
        /// MyHandler h = new MyHandler();
        /// ManagementOperationObserver ob = new ManagementOperationObserver();
        /// ob.Completed += new CompletedEventHandler(h.Done);
        /// 
        /// //Get the object asynchronously
        /// o.Get(ob);
        /// 
        /// //Wait until operation is completed
        /// while (!h.Completed)
        ///     System.Threading.Thread.Sleep (1000);
        /// 
        /// //Here we can use the object
        /// Console.WriteLine(o["SomeProperty"]);
        /// 
        /// public class MyHandler
        /// {
        ///     private bool completed = false;
        /// 
        ///     public void Done(object sender, CompletedEventArgs e) {
        ///         Console.WriteLine("async Get completed !");
        ///         completed = true;
        ///     }
        ///     
        ///     public bool Completed { 
        ///         get {
        ///             return completed;
        ///         }
        ///     }
        /// }
        ///    </code>
        ///    <code lang='VB'>Dim o As New ManagementObject("MyClass.Name=""abc""")
        /// 
        /// 'Set up handlers for asynchronous get
        /// Dim h As New MyHandler()
        /// Dim ob As New ManagementOperationObserver()
        /// ob.Completed += New CompletedEventHandler(h.Done)
        /// 
        /// 'Get the object asynchronously
        /// o.Get(ob)
        /// 
        /// 'Wait until operation is completed
        /// While Not h.Completed
        ///     System.Threading.Thread.Sleep(1000)
        /// End While
        ///     
        /// 'Here we can use the object
        /// Console.WriteLine(o("SomeProperty"))
        /// 
        /// Public Class MyHandler
        ///     Private _completed As Boolean = false;
        /// 
        ///     Public Sub Done(sender As Object, e As EventArrivedEventArgs)
        ///         Console.WriteLine("async Get completed !")
        ///         _completed = True
        ///     End Sub    
        /// 
        ///     Public ReadOnly Property Completed() As Boolean
        ///        Get
        ///            Return _completed
        ///        End Get
        ///     End Property
        /// End Class
        ///    </code>
        /// </example>
        public void Get(ManagementOperationObserver watcher)
        {
            Initialize ( false ) ;

            if ((null == path) || (path.Path.Length==0))
                throw new InvalidOperationException();
            else if (null == watcher)
                throw new ArgumentNullException("watcher");
            else
            {
                IWbemServices wbemServices = scope.GetIWbemServices();

                ObjectGetOptions o = ObjectGetOptions._Clone(options);

                WmiGetEventSink sink = watcher.GetNewGetSink(
                    scope,
                    o.Context, 
                    this);

                // If someone has registered for progress, make sure we flag it
                if (watcher.HaveListenersForProgress)
                    o.SendStatus = true;

                SecurityHandler securityHandler = null;
                int status                        = (int)ManagementStatus.NoError;

                securityHandler = scope.GetSecurityHandler();

                status = scope.GetSecuredIWbemServicesHandler(wbemServices).GetObjectAsync_(path.RelativePath,
                                            o.Flags,
                                            o.GetContext(),
                                            sink.Stub);

                
                if (securityHandler != null)
                    securityHandler.Reset();

                if (status < 0)
                {
                    watcher.RemoveSink(sink);
                    if ((status & 0xfffff000) == 0x80041000)
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                    else
                        Marshal.ThrowExceptionForHR(status);
                }
            }
        }