當前位置: 首頁>>代碼示例>>C#>>正文


C# CompletedEventArgs類代碼示例

本文整理匯總了C#中System.Management.CompletedEventArgs的典型用法代碼示例。如果您正苦於以下問題:C# CompletedEventArgs類的具體用法?C# CompletedEventArgs怎麽用?C# CompletedEventArgs使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


CompletedEventArgs類屬於System.Management命名空間,在下文中一共展示了CompletedEventArgs類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: InvokeMethodAsync

//引入命名空間
using System;
using System.Management;

public class InvokeMethodAsync 
{
    private bool isComplete = false;
    private ManagementBaseObject returnObject;

    public InvokeMethodAsync()
    {
        // Get the object on which the method 
        // will be invoked
        ManagementClass processClass = 
            new ManagementClass("Win32_Process");

        // Create a results and completion handler
        ManagementOperationObserver handler = 
            new ManagementOperationObserver();
        handler.Completed += 
            new CompletedEventHandler(this.Completed);

        // Invoke method asynchronously
        ManagementBaseObject inParams =
            processClass.GetMethodParameters("Create");
        inParams["CommandLine"] = "calc.exe";
        processClass.InvokeMethod(
            handler, "Create", inParams, null);
      
        // Do something while method is executing
        while(!this.IsComplete) 
        {
            System.Threading.Thread.Sleep(1000);
        }
    }

    // Property allows accessing the result
    // object in the main function
    private ManagementBaseObject ReturnObject 
    {
        get 
        {
            return returnObject;
        }
    }

    // Delegate called when the method completes
    // and results are available
    private void NewObject(object sender,
        ObjectReadyEventArgs e) 
    {
        Console.WriteLine("New Object arrived!");
        returnObject = e.NewObject;
    }

    // Used to determine whether the method
    // execution has completed
    private bool IsComplete 
    {
        get 
        {
            return isComplete;
        }
    }

    private void Completed(object sender,
        CompletedEventArgs e)
    {
        isComplete = true;
        Console.WriteLine("Method invoked.");
    }

    public static void Main()
    {
        InvokeMethodAsync invokeMethod = new InvokeMethodAsync();

        return;
    }
}
開發者ID:.NET開發者,項目名稱:System.Management,代碼行數:79,代碼來源:CompletedEventArgs


注:本文中的System.Management.CompletedEventArgs類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。