zoukankan      html  css  js  c++  java
  • 异步操作样本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(); 
    } 
    
  • 相关阅读:
    彻底卸载MySql
    MySql和SQL Server数据类型 对比
    easyui combobox 中实现 checkbox
    浅谈研发项目经理
    软件公司项目经理岗位职责
    input text 的事件及方法
    c#解析HTML
    SQL SERVER 数据库查询表和字段信息语句
    Silverlight动态载入调用XAML资源
    几种设计模式简介(转载)
  • 原文地址:https://www.cnblogs.com/kevinzhwl/p/3878904.html
Copyright © 2011-2022 走看看