How to implement single instance per machine application?

前端 未结 7 2124
無奈伤痛
無奈伤痛 2020-12-06 17:42

I have to restrict my .net 4 WPF application so that it can be run only once per machine. Note that I said per machine, not per session.
I implemented single instance ap

7条回答
  •  孤街浪徒
    2020-12-06 18:11

    I think what you need to do is use a system sempahore to track the instances of your application.

    If you create a Semaphore object using a constructor that accepts a name, it is associated with an operating-system semaphore of that name.

    Named system semaphores are visible throughout the operating system, and can be used to synchronize the activities of processes.

    EDIT: Note that I am not aware if this approach works across multiple windows sessions on a machine. I think it should as its an OS level construct but I cant say for sure as i havent tested it that way.

    EDIT 2: I did not know this but after reading Stevo2000's answer, i did some looking up as well and I think that the "Global\" prefixing to make the the object applicable to the global namespace would apply to semaphores as well and semaphore, if created this way, should work.

提交回复
热议问题