简体   繁体   中英

Can Events be Inter-Process?

I have created an event in one process and to test, sent the event handle via a pipe to a totally separate process (not a child thread)

When I fire the event in the first, WaitForSingleObject does not detect the event so I am guessing the answer is no unless I missed some trick in the SECURITY_ATTRIBUTES structure?

Or perhaps I need to use a named event and call OpenEvent()?

In this case I cannot use window messages because I am trying to signal a windows service. I could use the pipe, but there will be many of these applications, and I would like to find a "low cost" solution if possible.

Other options like Memory mapped files have even more overhead than the pipe?

How would you do this?

You need to create a named event and open it in both processes. If you have multiple processes listening, you may consider using a semaphore .

Yes this works:

  #COMPILE EXE "NamedEvent.exe"

  #INCLUDE "win32api.inc" 

  %EVENT_ALL_ACCESS = &h0001F0003

  FUNCTION PBMAIN() AS LONG  

    LOCAL lRet AS LONG, lError AS LONG, lEventName AS ASCIIZ * %MAX_PATH
    lEventName = "TestEvent"
    lRet   = CreateEvent (BYVAL %NULL, %False, %False, lEventName)
    lError = GetLastError ()
    IF ISFALSE lRet THEN
      MSGBOX "Unable to create Event, error:" + STR$(lError),,"CreateEvent error"
    ELSE
      IF lError = %ERROR_ALREADY_EXISTS THEN
        lRet = OpenEvent(BYVAL %EVENT_ALL_ACCESS, %False, lEventName)
        lError = GetLastError()
        IF lRet THEN
          MSGBOX "Opened existing Event, handle:" + STR$(lRet),,"OpenEvent:"
        ELSE
          MSGBOX "Unable to open Event, error:" + STR$(lError),,"OpenEvent error" : EXIT FUNCTION
        END IF
      ELSE
        MSGBOX "Created new Event, handle:" + STR$(lRet),,"CreateEvent:"
      END IF
    END IF    

  END FUNCTION

In general, what has a lower overhead:

Pipes (assuming small size specified)

MemMapFiles

Events

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM