[C#]
using System;
using System.Diagnostics;
using System.ComponentModel;namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
public class MyProcess
{
public void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();
// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");
// Get all instances of Notepad running on the specifiec
// computer.
// 1. Using the computer alias (do not precede with "\\").
Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");
// 2. Using an IP address to specify the machineName parameter.
Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
// Get all processes running on the local computer.
Process [] localAll = Process.GetProcesses();
// Get all processes running on the remote computer.
Process [] remoteAll = Process.GetProcesses("myComputer");
// Get a process on the local computer, using the process id.
Process localById = Process.GetProcessById(1234);
// Get a process on a remote computer, using the process id.
Process remoteById = Process.GetProcessById(2345, "myComputer");
}
public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.BindToRunningProcesses(); }
}
}
using System;
using System.Diagnostics;
using System.ComponentModel;namespace MyProcessSample
{
/// <summary>
/// Shell for the sample.
/// </summary>
public class MyProcess
{
public void BindToRunningProcesses()
{
// Get the current process.
Process currentProcess = Process.GetCurrentProcess();
// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");
// Get all instances of Notepad running on the specifiec
// computer.
// 1. Using the computer alias (do not precede with "\\").
Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");
// 2. Using an IP address to specify the machineName parameter.
Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
// Get all processes running on the local computer.
Process [] localAll = Process.GetProcesses();
// Get all processes running on the remote computer.
Process [] remoteAll = Process.GetProcesses("myComputer");
// Get a process on the local computer, using the process id.
Process localById = Process.GetProcessById(1234);
// Get a process on a remote computer, using the process id.
Process remoteById = Process.GetProcessById(2345, "myComputer");
}
public static void Main()
{
MyProcess myProcess = new MyProcess();
myProcess.BindToRunningProcesses(); }
}
}
public static extern int FindWindow(string lpClassName, string lpWindowName) 如果返回值不等于0,那么就是有运行了。
Dim thisproc As System.Diagnostics.Process
For Each thisproc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
If thisproc.CloseMainWindow() = False Then
thisproc.Kill()
MsgBox("kill the EXCEL successfully!")
Else
MsgBox("Close the Excel Process successfully!")
End If
Next
using System.Runtime.InteropServices;[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd); private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
static void Main()
{
#region 关闭重复线程
string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
{
MessageBox.Show ("程序已经启动!!",Constants.MESSAGEBOX_TITLE );
Process p=Process.GetCurrentProcess();
int n=0;
if (processes[0].Id==p.Id)
{
n=1;
}
IntPtr hWnd=processes[n].MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}
SetForegroundWindow(hWnd);
return;
}
#endregion
}
using System.Runtime.InteropServices;
然后在Main()函数里面加如下代码:
bool requestInitialOwnership = true;
bool mutexWasCreated; Mutex m = new Mutex(requestInitialOwnership, "jiliang",out mutexWasCreated);
if (!(requestInitialOwnership && mutexWasCreated))
{
MessageBox.Show("本程序已经打开。");
return;
}
就可以解决问题了.
忘了结帖,现在给分!