• 异步操作样本1


    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(); 
    } 
    
  • 相关阅读:
    最不要勉强的,是感情.
    Nginx+phpfastcgi下flush 一下子全部输出问题
    php excel 读取日期问题
    JavaScript年月日和时间戳互转
    mysql 查找除id外其他重复的字段数据
    Thinkphp 下 MySQL group by 接count 获得条数方法
    MySQL性能管理及架构设计 --- 理论篇
    MySQL 导入.sql文件
    Apache ab 压力并发测试工具
    工作生活随笔
  • 原文地址:https://www.cnblogs.com/kevinzhwl/p/3878904.html
Copyright © 2020-2023  润新知