View Full Version : 在C#中如何防止程序被多次运行?


David Zha0
04-05-2007, 10:36 AM
我想知道如何在C#中防止创建一个程序的多个实例?谢谢!!!

绉﹂鎰忓姩
04-05-2007, 11:00 AM
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

public class OneInstnace
{
[STAThread]
public static void Main()
{
//寰楀埌姝e湪杩愯鐨勪緥绋
Process instance = RunningInstance();
if (instance == null)
{
//濡傛灉娌℃湁鍏跺畠渚嬬▼锛屽氨鏂板缓涓涓獥浣
Application.Run (new Form());
}
else
{
//澶勭悊鍙戠幇鐨勪緥绋
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);

//閬嶅巻姝e湪鏈夌浉鍚屽悕瀛楄繍琛岀殑渚嬬▼
foreach (Process process in processes)
{
//蹇界暐鐜版湁鐨勪緥绋
if (process.Id != current.Id)
{
//纭繚渚嬬▼浠嶦XE鏂囦欢杩愯
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//杩斿洖鍙︿竴涓緥绋嬪疄渚
return process;
}
}
}

//娌℃湁鍏跺畠鐨勪緥绋嬶紝杩斿洖Null
return null;
}

public static void HandleRunningInstance(Process instance)
{
//纭繚绐楀彛娌℃湁琚渶灏忓寲鎴栨渶澶у寲
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//璁剧疆鐪熷疄渚嬬▼涓篺oreground window
SetForegroundWindow (instance.MainWindowHandle);
}

[DllImport("User32.dll")]

private static extern bool ShowWindowAsync(
IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")] private static extern bool
SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
}


鈥淒avid Zha0鈥濈紪鍐欙細

> 鎴戞兂鐭ラ亾濡備綍鍦–#涓槻姝㈠垱寤轰竴涓▼搴忕殑澶氫釜瀹炰緥锛熻阿璋紒锛侊紒
>
>
>

David Zha0
04-08-2007, 04:00 AM
谢谢您的回答!
我记得VB中好像有个名为Instance的变量,可以用它判断是否已经有一个实例在运行,C#
中有这样的变量或方法吗?只能用这样的方法吗?
谢谢!

"秦风意动" <@discussions.microsoft.com> 写入消息新闻:4B1C0FF5-2818-4DB8-99B1-745064E5554E@microsoft.com...
> using System;
> using System.Runtime.InteropServices;
> using System.Windows.Forms;
> using System.Diagnostics;
> using System.Reflection;
>
> public class OneInstnace
> {
> [STAThread]
> public static void Main()
> {
> //得到正在运行的例程
> Process instance = RunningInstance();
> if (instance == null)
> {
> //如果没有其它例程,就新建一个窗体
> Application.Run (new Form());
> }
> else
> {
> //处理发现的例程
> HandleRunningInstance(instance);
> }
> }
> public static Process RunningInstance()
> {
> Process current = Process.GetCurrentProcess();
> Process[] processes = Process.GetProcessesByName (current.ProcessName);
>
> //遍历正在有相同名字运行的例程
> foreach (Process process in processes)
> {
> //忽略现有的例程
> if (process.Id != current.Id)
> {
> //确保例程从EXE文件运行
> if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
> current.MainModule.FileName)
> {
> //返回另一个例程实例
> return process;
> }
> }
> }
>
> //没有其它的例程,返回Null
> return null;
> }
>
> public static void HandleRunningInstance(Process instance)
> {
> //确保窗口没有被最小化或最大化
> ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
>
> //设置真实例程为foreground window
> SetForegroundWindow (instance.MainWindowHandle);
> }
>
> [DllImport("User32.dll")]
>
> private static extern bool ShowWindowAsync(
> IntPtr hWnd, int cmdShow);
> [DllImport("User32.dll")] private static extern bool
> SetForegroundWindow(IntPtr hWnd);
> private const int WS_SHOWNORMAL = 1;
> }
>
>
> “David Zha0”编写:
>
>> 我想知道如何在C#中防止创建一个程序的多个实例?谢谢!!!
>>
>>
>>

gshzheng
04-09-2007, 09:39 AM
那就是处理你这个问题的另一种方法.

你查一下mutex.


"David Zha0" <zhplive@163.com> 写入消息新闻:uixNcLneHHA.1216@TK2MSFTNGP03.phx.gbl...
> 谢谢您的回答!
> 我记得VB中好像有个名为Instance的变量,可以用它判断是否已经有一个实例在运行,C# 中有这样的变量或方法吗?只能用这样的方法吗?
> 谢谢!
>
> "秦风意动" <@discussions.microsoft.com> 写入消息新闻:4B1C0FF5-2818-4DB8-99B1-745064E5554E@microsoft.com...
>> using System;
>> using System.Runtime.InteropServices;
>> using System.Windows.Forms;
>> using System.Diagnostics;
>> using System.Reflection;
>>
>> public class OneInstnace
>> {
>> [STAThread]
>> public static void Main()
>> {
>> //得到正在运行的例程
>> Process instance = RunningInstance();
>> if (instance == null)
>> {
>> //如果没有其它例程,就新建一个窗体
>> Application.Run (new Form());
>> }
>> else
>> {
>> //处理发现的例程
>> HandleRunningInstance(instance);
>> }
>> }
>> public static Process RunningInstance()
>> {
>> Process current = Process.GetCurrentProcess();
>> Process[] processes = Process.GetProcessesByName
>> (current.ProcessName);
>>
>> //遍历正在有相同名字运行的例程
>> foreach (Process process in processes)
>> {
>> //忽略现有的例程
>> if (process.Id != current.Id)
>> {
>> //确保例程从EXE文件运行
>> if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
>> current.MainModule.FileName)
>> {
>> //返回另一个例程实例
>> return process;
>> }
>> }
>> }
>>
>> //没有其它的例程,返回Null
>> return null;
>> }
>>
>> public static void HandleRunningInstance(Process instance)
>> {
>> //确保窗口没有被最小化或最大化
>> ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
>>
>> //设置真实例程为foreground window
>> SetForegroundWindow (instance.MainWindowHandle);
>> }
>>
>> [DllImport("User32.dll")]
>>
>> private static extern bool ShowWindowAsync(
>> IntPtr hWnd, int cmdShow);
>> [DllImport("User32.dll")] private static extern bool
>> SetForegroundWindow(IntPtr hWnd);
>> private const int WS_SHOWNORMAL = 1;
>> }
>>
>>
>> “David Zha0”编写:
>>
>>> 我想知道如何在C#中防止创建一个程序的多个实例?谢谢!!!
>>>
>>>
>>>
>
>
>