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(); 
    } 
    
  • 相关阅读:
    elementUI中的loading
    element消息提示封装
    scroll滚动条掩藏
    elment-UI中表头和内容错位
    SpringBoot集成AD域实现统一用户认证
    SpringBoot集成JWT验证方式
    OAuth2.0协议专区-深入介绍
    Alibaba-技术专区-开源项目之Druid数据库监控平台
    SpringBoot-技术专区-Mybatisplus多数据源
    MySQL-技术专区-性能优化速记
  • 原文地址:https://www.cnblogs.com/kevinzhwl/p/3878903.html
Copyright © 2011-2022 走看看