如何让程序只运行一次实例

toball 2004-11-22 12:39:29
如何让程序只运行一次实例,如果运行了程序的一个实例,再次运行程序的时候激活原来运行的程序,使他的窗口显示出来
...全文
429 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
keiven 2005-03-24
  • 打赏
  • 举报
回复
mark一下
yys_tt 2004-12-07
  • 打赏
  • 举报
回复
UP
yys_tt 2004-12-07
  • 打赏
  • 举报
回复
不好意思,打扰您了,再问一个问题:如果一个多线程的程序A启动时,按一个按钮启动一个程序B,我用了你的这段程序,那么按第二此就不会再运行B,但是我这个时候又启动了一个程序A,再按这个按钮,这个时候我想启动B,那么应该怎么办,求救...
yys_tt 2004-12-07
  • 打赏
  • 举报
回复
请问李先生,我有个小问题,为什么运行上面的程序时,电脑明显的发生一个很大的读写的声音(1秒左右)?我的电脑刚买的,开别的程序也没那么大声音啊.没有加这段代码没声音的.如何解决这个问题?
lgg06 2004-12-07
  • 打赏
  • 举报
回复
声明一个私有的构造函数应该可以吧
yys_tt 2004-12-07
  • 打赏
  • 举报
回复
我有个小问题,为什么运行上面的程序时,电脑明显的发生一个读写的很大的声音?我的电脑刚买的,开别的程序也没那么大声音啊.是不是代码不够温柔?
wolma 2004-12-07
  • 打赏
  • 举报
回复
我也是才学C#,又偷学一招儿。
bihoo88 2004-12-07
  • 打赏
  • 举报
回复
这是VB.NET的程序
bihoo88 2004-12-07
  • 打赏
  • 举报
回复
下面两句可实现:程序是否打开,如打开就不会再打开,但不会激活已打开的程序,我也不知道。

If Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName).Length > 1 Then

End

End If
husiwei 2004-11-22
  • 打赏
  • 举报
回复
up
lihonggen0 2004-11-22
  • 打赏
  • 举报
回复
呵呵,最近事比较多,睡得比较晚
amitabha 2004-11-22
  • 打赏
  • 举报
回复
xx
cathylang 2004-11-22
  • 打赏
  • 举报
回复
赞成
hnhl 2004-11-22
  • 打赏
  • 举报
回复
static void Main()
{
//判断之前是否已经打开了相同的进程
//new Bestzone.MIS.Report.F1BookContainer();
bool alreadyExist = false;
try
{
System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in ps)
{
if(p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id)
{
alreadyExist = true;
}
}
}
catch(System.PlatformNotSupportedException ex){ex.ToString();}
catch(System.InvalidOperationException ex){ex.ToString();}

//如果已经存在,则放弃本进程
if(alreadyExist)
{
return;
}

Application.Run(new MainForm());
}
CSDNATM 2004-11-22
  • 打赏
  • 举报
回复
楼上的正确

顺便问一下,李先生你不用睡觉的吗
biliboy 2004-11-22
  • 打赏
  • 举报
回复
up
哈哈007哈 2004-11-22
  • 打赏
  • 举报
回复
一楼的根据进程名字是不好的,因为我exe文件的名称可以改的,人家要是改了你的可执行文件的文件名你就没有办法了
lihonggen0 2004-11-22
  • 打赏
  • 举报
回复
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);

//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}

//No other instance was found, return null.
return null;
}


public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//Set the real intance to 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;
}

toball 2004-11-22
  • 打赏
  • 举报
回复
如果当前的窗口已经被我Hide()了,那该怎么让他再显示出来

110,561

社区成员

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

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

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