防止程序重复运行,试了很多法子都不行,WinCE和VS2005C#环境,急啊!!!求救

sxxajian   2008-5-17 14:01 楼主
正在用VS2005的C#进行PDA开发,操作系统用的是WinCE5.0,现在遇到麻烦,不能防止程序重复运行。在论坛里查了许多帖子,基本都是使用Process和Mutex,可是在VS2005 C#环境下的PDA编程,没有FindWindow函数,不支持使用名称的Mutex构造,无法建立起系统级的Mutex对象,也没有CreateMutex()函数,而Process也不支持GetProcess()方法,不能获得Process列表,该怎么办啊?就要交活了,急死,请各位达人不吝赐教,先谢了!!!

回复评论 (14)


那可以改变思路:
比如 用一个文件来标识。进程启动时检查文件是否存在。若文件存在,说明程序已运行,当前进程退出;若否,创建文件,进程照常运行,进程结束时,应删除文件
另外还有开辟TCP端口之类的,道理是一样的
点赞  2008-5-17 19:56
如果程序异常退出的话,可能会造成程序无法运行啊,另外这个PDA是不带网络功能的。怎么办呢?晕
点赞  2008-5-17 22:16
CreateMutex应该有啊,是不是没加头文件Winbase.h.
点赞  2008-5-19 11:01
确实没有,不是头文件的问题,CE环境还是有些特殊的
点赞  2008-5-23 14:41
  WinCE 可以用这些函数的  我这不是用的好好的

我的平台 WinCE5.0  Intel pxa270 用的EVC开发工具

难道C#不一样?
点赞  2008-5-23 15:59
按一楼的方法,再外加一个守护进程。如果程序异常退出,则由守护进程删除标识文件。
点赞  2008-5-23 16:19
1. 创建一个可以跨进程访问的东西,如Mutex等(带名字的Mutex)

2. 如果是后台程序,我当初在2000系统中也做运行只运行一个exe实例的问题,注意2000是个多用户系统,为了多个用户也只能同时运行一个exe实例,我把该exe做成了“服务”。因为“服务”程序是在Windows用户登陆之前就已经运行的,而多个用户登陆时,都是从登陆界面开始的,所以可以解决。我不知道WCE下有没有服务的概念。

3. 是否可以通过注册表来实现(我是感觉这样不大好……)
点赞  2008-5-23 20:54
WINCE下肯定支持Mutex的。

WINCE下也可以有服务(Service)程序,将自己的程序注册到HLM\Service下就行。
点赞  2008-5-24 12:35
鉴于你的实际情况,一楼的方法建立配置文件来判断是最好的方法。
点赞  2008-5-27 10:06
信号量?
点赞  2008-5-27 11:35
CreateMutex的确是能用的,VS2005的帮助文档就可以查到

This function creates a named or unnamed mutex object.

Syntax

HANDLE CreateMutex(
  LPSECURITY_ATTRIBUTES lpMutexAttributes,
  BOOL bInitialOwner,
  LPCTSTR lpName
);


Parameters
lpMutexAttributes
[in] Ignored. Must be NULL.

bInitialOwner
[in] Boolean that specifies the initial owner of the mutex object. If this value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.

lpName
[in] Long pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.

If lpName is NULL, the mutex object is created without a name.

If lpName matches the name of an existing named mutex object, the bInitialOwner parameter is ignored because it has already been set by the creation process.

Each object type, such as memory maps, semaphores, events, message queues, mutexes, and watchdog timers, has its own separate namespace. Empty strings, "", are handled as named objects. On Windows desktop-based platforms, synchronization objects all share the same namespace.

Return Value
A handle to the mutex object indicates success. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex. NULL indicates failure. To get extended error information, call GetLastError.

Remarks
The handle returned by CreateMutex has MUTEX_ALL_ACCESS access to the new mutex object and can be used in any function that requires a handle to a mutex object.

Any thread of the calling process can specify the mutex-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.

The state of a mutex object is signaled when no thread owns it. The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutex's state is signaled, one waiting thread is granted ownership, the mutex's state changes to nonsignaled, and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership.

The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, the thread would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving you of the responsibility of ensuring that the creating process is started first. When using this technique, set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.

Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization. To provide object sharing, a process can specify the name of a mutex object in a call to the CreateMutex function.

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.

Requirements
OS Versions: Windows CE 1.0 1 and later.

Header: Winbase.h.

Link Library: Coredll.lib, Nk.lib.

See Also
Reference
CreateProcess
ReleaseMutex

Other Resources
CloseHandle
点赞  2008-5-27 14:58
明确的说,是支持的。
我们的程序都是只运行一次的
点赞  2008-5-27 18:16
引用: 引用 1 楼 prstat 的回复:

那可以改变思路:
比如 用一个文件来标识。进程启动时检查文件是否存在。若文件存在,说明程序已运行,当前进程退出;若否,创建文件,进程照常运行,进程结束时,应删除文件
另外还有开辟TCP端口之类的,道理是一样的


还有请教 6楼 91program

为什么是“创建文件”?而不是写注册表值呢?
点赞  2008-5-27 22:10
如果从纯技术上说,写注册表当然也可以的。

LZ自己选择。
点赞  2008-5-28 09:06
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复