判断进程是否已启动的问题

wuhengqiang 2007-04-03 03:53:56
请问如何判断一个进程是否已经启动
...全文
598 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sz709 2007-04-03
  • 打赏
  • 举报
回复
学习..
GXY2005 2007-04-03
  • 打赏
  • 举报
回复
枚举进程
最简单实用
zhangliu_521 2007-04-03
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;

其他代码省略。仅仅从一个form启动讲。每个form都有个main入口。
static void Main()
{ Process instance = RunningInstance(); //获取运行中的实例
if (instance==null)
Application.Run(new Form1());
else
{
HandleRunningInstance(instance);//处理得到的进程。
}
}
private static Process RunningInstance()
{Process current = Process.GetCurrentProcess(); //得到当前form1的进程
Process[] processes = Process.GetProcessesByName(current.ProcessName);// 得到所有同名进程。
foreach(Process MyProcess in processes)
{
if(MyProcess.Id!= current.Id) //不同的进程id,也就是肯定不是同一进程
{
if(Assembly.GetExecutingAssembly().Location.Replace("/","\\")==current.MainModule.FileName) //进程名不同而id不同 的时候,比较程序启动的路径
return MyProcess;

}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("程序已经运行!");
ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。


}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
wuhengqiang 2007-04-03
  • 打赏
  • 举报
回复
试试先
zhangliu_521 2007-04-03
  • 打赏
  • 举报
回复
调用API FindWindowEx(HWND hWndParent, HWND hWndNext, /*in*/LPCTSTR
szClassName, /*in*/LPCTSTR szWindowTitle)方法.
比较有用的是这句,我们可以使用registered window class name来找到所要窗口.
如: IE窗口(IEFrame是所有打开的IE的标识).

protected void FindPopup()
{
IntPtr hParent = IntPtr.Zero;
IntPtr hNext = IntPtr.Zero;
String sClassNameFilter = "IEFrame"; // 所有IE窗口的

do
{
hNext =
NativeWIN32.FindWindowEx(hParent,hNext,sClassNameFilter,IntPtr.Zero);
// we've got a hwnd to play with if ( !hNext.Equals(IntPtr.Zero) )
{ // get window caption NativeWIN32.STRINGBUFFER
sLimitedLengthWindowTitle; NativeWIN32.GetWindowText(hNext, out
sLimitedLengthWindowTitle, 256); String sWindowTitle =
sLimitedLengthWindowTitle.szText; if (sWindowTitle.Length>0)
{ // find this caption in the list of banned captions
foreach (ListViewItem item in listView1.Items)
{ if ( sWindowTitle.StartsWith(item.Text) )
NativeWIN32.SendMessage(hNext, NativeWIN32.WM_SYSCOMMAND,
NativeWIN32.SC_CLOSE,
IntPtr.Zero); // try soft kill } } } }
while (!hNext.Equals(IntPtr.Zero)); } public class NativeWIN32{
[DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern
IntPtr FindWindowEx(IntPtr parent /*HWND*/,
IntPtr next /*HWND*/, string
sClassName, IntPtr
sWindowTitle); }
zhangliu_521 2007-04-03
  • 打赏
  • 举报
回复
string prfun(){
bool i=false;
process pName=Process.Start("ex.exe");
foreach (Process otherProc in
Process.GetProcessByName(pName.ProcessName))
{
if (pName.ProcessName==otherProc .ProcessName)
i=true;
if(i)
otherProc.Kill();
}
}
zhangliu_521 2007-04-03
  • 打赏
  • 举报
回复
1. 用API函数,调用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,放弃.
louise135 2007-04-03
  • 打赏
  • 举报
回复
我一般实现的多线程是用backworker组件来实现的 他有个属性叫做isbusy属性是判断它是否正在执行的 给你个参考
Red_angelX 2007-04-03
  • 打赏
  • 举报
回复
枚举进程

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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