判断进程是否已启动的问题 请问如何判断一个进程是否已经启动 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我一般实现的多线程是用backworker组件来实现的 他有个属性叫做isbusy属性是判断它是否正在执行的 给你个参考 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,放弃. 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();}} 调用API FindWindowEx(HWND hWndParent, HWND hWndNext, /*in*/LPCTSTRszClassName, /*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.STRINGBUFFERsLimitedLengthWindowTitle; NativeWIN32.GetWindowText(hNext, outsLimitedLengthWindowTitle, 256); String sWindowTitle =sLimitedLengthWindowTitle.szText; if (sWindowTitle.Length>0){ // find this caption in the list of banned captionsforeach (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 externIntPtr FindWindowEx(IntPtr parent /*HWND*/,IntPtr next /*HWND*/, stringsClassName, IntPtrsWindowTitle); } 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); C#窗体关闭 急求正则表达式,解析启动参数 treeview节点修改时获取值的问题 C# socket编程为什么打开侦听的时候,程序容易卡死,且程序收不到数据 怎么样实现局域网中文件夹互相传送 关于完整的文章的在数据库中的存储问题 能否用标准C++写数据处理,用C#做界面 C# Web怎样调用串口 如何使得同类的窗口最多只能打开一个? 数据库字段调整后,以前自动生成的数据集不能用了。求救! 请教:如果得到异步调用后出现的异常 SharpDevelop用VC#.NET 2005打开的问题
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,放弃.
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();
}
}
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); }
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);