在线等............不能多次打开同一窗体.
不能多次打开同一个exe文件,请给出代码. 问题点数:50、回复次数:7Top
1 楼xjliang007(痛并快乐着)回复于 2005-01-28 10:17:17 得分 0
利用一个公共属性,如果其值为False则不让其打开,如果为TRUE则可以打开,打开时将其值改为TRUE,关闭时将其值改为FALSE。Top
2 楼landlordh(work wonders)回复于 2005-01-28 10:20:39 得分 0
限制程序只运行一次:
记得工程启动对象要设为sub main
Public Function RunningInstance() As Process
Dim current As Process = System.Diagnostics.Process.GetCurrentProcess()
Dim processes As Process() = System.Diagnostics.Process.GetProcessesByName(current.ProcessName)
Dim process As Process
For Each process In processes
'Ignore the current process
If process.Id <> current.Id Then
'Make sure that the process is running from the exe file.
If System.Reflection.[Assembly].GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then
'Return the other process instance.
Return process
End If
End If
Next process
'如果没有,返回为空
Return Nothing
End Function
Public Sub Main()
Dim objMutex As New System.Threading.Mutex(False, Windows.Forms.Application.ProductName)
If Not objMutex.WaitOne(0, False) Then
objMutex.Close()
objMutex = Nothing
'防止连续两次过快的双击
End
End If
Dim instance As Process = RunningInstance()
If Not (instance Is Nothing) Then
MsgBox("此程序已经在运行!", MsgBoxStyle.Information, "提示信息")
End
End If
'如果没有运行,就继续下面。
...
Dim main As New mainfrm
Application.Run(main)
End SubTop
3 楼fjshirley()回复于 2005-01-28 10:28:55 得分 0
有什么问题可以去www.zhihuigu.com上去问,那有很多一流大虾.......高手如云..专门给人解决问题
有问必答.....各位有时间都可以去看看!!!!!!!!!!!!!!!
Top
4 楼chenjinaban(cj)回复于 2005-01-28 10:53:18 得分 50
//判断是否已经存在一个exe
bool createdNew;
Mutex m = new Mutex(true, "yourexe", out createdNew);
if (! createdNew)
{
MessageBox.Show("Only one exe is allowed at a time.");
return;
}
Application.Run(new Start());
GC.KeepAlive(m);Top
5 楼chenjinaban(cj)回复于 2005-01-28 10:54:31 得分 0
上面的要添加using System.Threading;
Mutex class是用于进程同步的Top
6 楼chenjinaban(cj)回复于 2005-01-28 10:55:56 得分 0
对了上面的是要写在main函数里面的
[STAThread]
static void Main()
{
}Top
7 楼cendrillon(珞珞)回复于 2005-01-30 21:52:13 得分 0
等星期一我写测试过,马上给分.Top




