我的程序如何获得我将要启动的进程是否已经启动了呢?

heyoulin 2006-01-23 06:33:19
我现在想启动一个java程序,但是,该程序在这之前就已经在运行了.
因此,我现在如果还启动它的话,就需要给出提示:该进程已经启动.
我如何才能知道该进程已经启动了呢?
...全文
93 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zouxinfox 2006-01-23
  • 打赏
  • 举报
回复
不错,顶
cenlmmx 2006-01-23
  • 打赏
  • 举报
回复
1. 用API函数,那就要用到JNI,java来调用C++ dll.
C++代码:
BOOL GetProcessList ()
{
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};

// Take a snapshot of all processes in the system.

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE)
return (FALSE);

// Fill in the size of the structure before using it.

pe32.dwSize = sizeof(PROCESSENTRY32);

// Walk the snapshot of the processes, and for each process,
// display information.

if (Process32First(hProcessSnap, &pe32))
{
DWORD dwPriorityClass;
BOOL bGotModule = FALSE;
MODULEENTRY32 me32 = {0};

do
{
bGotModule = GetProcessModule(pe32.th32ProcessID,
pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));

if (bGotModule)
{
HANDLE hProcess;

// Get the actual priority class.
hProcess = OpenProcess (PROCESS_ALL_ACCESS,
FALSE, pe32.th32ProcessID);
dwPriorityClass = GetPriorityClass (hProcess);
CloseHandle (hProcess);

// Print the process's information.
printf( "\nPriority Class Base\t%d\n",
pe32.pcPriClassBase);
printf( "PID\t\t\t%d\n", pe32.th32ProcessID);
printf( "Thread Count\t\t%d\n", pe32.cntThreads);
printf( "Module Name\t\t%s\n", me32.szModule);
printf( "Full Path\t\t%s\n\n", me32.szExePath);
}
}
while (Process32Next(hProcessSnap, &pe32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of processes

// Do not forget to clean up the snapshot object.

CloseHandle (hProcessSnap);
return (bRet);
}

2. 笨办法,启动一个进程就写标志到一个本地资源(如文件),下一个进程启动先去读这个资源来判断,标志为false(已经有相同进程在运行),启动,为true,放弃.
jlbqd 2006-01-23
  • 打赏
  • 举报
回复
关注
VB中我会实现的
java就不会了
vickcy 2006-01-23
  • 打赏
  • 举报
回复
关注中~~~~~~~~~~~~~

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧