社区
C#
帖子详情
判断进程是否已启动的问题
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
打赏
举报
回复
枚举进程
linux
判断
进程
是否
存在并
启动
,LINUX
判断
进程
是否
存在并
启动
该
进程
服务器上运行了一些小工具属于非服务类的,程序
进程
本身不是很稳定总是异常关闭写个脚本监视它,
判断
进程
是否
存在并
启动
该
进程
。Linux
判断
进程
是否
存在并
启动
该
进程
#!/bin/bash#
判断
进程
是否
存在,如果不存在就
启动
...
Linux
判断
进程
是否
存在并
启动
该
进程
Linux
判断
进程
是否
存在并
启动
该
进程
判断
某个
进程
是否
已经
启动
判断
某个
进程
是否
已经
启动
shell
判断
进程
是否
存在,不存在则
启动
shell
判断
进程
是否
存在,不存在则
启动
C# 检查
进程
是否
已经
启动
string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; //获取欲
启动
进程
名 CommonData.VersionNumber = ...//检查
进程
是否
已经
启动
,已经
启动
则提示信息退出程序 i
C#
110,538
社区成员
642,577
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章