zoukankan      html  css  js  c++  java
  • 异步操作样本

    Sample C code calling it:

    OVERLAPPED overlapped;   
    overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);   
    fns.NotifyStateChange(&overlapped);   
    WaitForSingleObject(overlapped.hEvent, INFINITE);   
    // ...query for the new state or whatever... 
    

    This is how I approached it in C#:

    [DllImport("myfuncs.dll")] 
    unsafe public static extern int NotifyStateChange(NativeOverlapped* lpOverlapped); 
     
    static private ManualResetEvent m_stateChangeEvent = new ManualResetEvent(false); 
     
    public static DeviceState WaitForStateChange() 
    { 
        unsafe 
        { 
            Overlapped overlapped = new Overlapped(0, 0,  
                m_stateChangeEvent.SafeWaitHandle.DangerousGetHandle(), null); 
            IOCompletionCallback callback = StateChangeCallback; 
            byte[] userData = new byte[100]; 
            NativeOverlapped* nativeOverlapped = overlapped.Pack(callback, userData); 
            NotifyStateChange(nativeOverlapped); 
            m_stateChangeEvent.WaitOne(); 
            Overlapped.Unpack(nativeOverlapped); 
            Overlapped.Free(nativeOverlapped); 
        } 
        return GetCurrentState(); 
    } 
     
    [ComVisibleAttribute(true)] 
    unsafe static public void StateChangeCallback ( 
        uint errorCode,  
        uint bytesTransferred,  
        NativeOverlapped* overlapped) 
    { 
        m_stateChangeEvent.Set(); 
    } 
    
  • 相关阅读:
    特性和属性
    装箱和拆箱的问题(NET1.1+)
    poj 1013 Counterfeit Dollar(模拟)
    poj 3686 The Windy's( KM算法 )
    二分匹配(匈牙利算法)
    poj 3273 Monthly Expense(二分)
    poj 2115 C Looooops(扩展欧几里德)
    poj 2400 Supervisor, Supervisee
    poj 2195 Going Home (KM算法)
    poj 2513 Colored Sticks(trie树 + 并查集 + 欧拉图)
  • 原文地址:https://www.cnblogs.com/kevinzhwl/p/3878903.html
Copyright © 2011-2022 走看看