English 中文(简体)
窗口.net 交叉过程同步
原标题:windows .net cross process synchronization

我需要做一些跨过程的同步。

Process B should do some actions only when Process A is running. I tried using Mutex and EventWaitHandles like this :

进程A的主要部分:

EventWaitHandle eventWaitHandle = new EventWaitHandle(true, EventResetMode.ManualReset, "SOMEGUID");

进程B:

private static bool IsProcessARunning(string mutexStr)
{
    try
    {
        EventWaitHandle.OpenExisting(mutexStr);
        return true;
    }
    catch (WaitHandleCannotBeOpenedException e)
    {
        return false;
    }
}

But after process A starts once the IsProcessARunning method will always return true even if process A is closed. Any idea what am I doing wrong here ?

问题回答

EventWaitHandle.OpenExisting(mutexStr); with EventWaitHandle.OpenExisting(mutexStr).Wait One(-1,false);,以便你可以等待活动。

EDIT:

更改:<代码>EventWaitHandle WaitHandle = 新的事件WaitHandle (true, MattResetMode.ManualReset, “SOMEGUID”);

纽约总部

WaitHandle = 新活动WaitHandle(false, FinalResetMode.ManualReset, “SOMEGUID”);

You must call eventWaitHandle.Set() when starting and eventWaitHandle.Reset() when s纽约总部ping in ProcessA

EDIT:

Because processA might have crashed you should impement System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(procAName); and check if it is running. If I knew how you will be using this exactly then I could possibly suggest a more appropriate solution. In this case you don t need mutexes.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签