如何显示先前的实例,如果应用程序已在运行. [英] How to show the previous instance, If application is already run.
问题描述
当用户最小化程序时,我使用NotifyIcon控件隐藏表单并在托盘中显示图标.
如果用户双击图标,则在屏幕上显示该程序.它按顺序工作.
私有 子 NotifyIcon1_MouseDoubleClick( ByVal 发件人 As 系统.对象, ByVal e As System.Windows.Forms.MouseEventArgs)句柄 NotifyIcon1.MouseDoubleClick
NotifyIcon1.Visible = 错误
我 .Show()
我 .WindowState = FormWindowState.Normal
结束 子
私有 子 frmGenerateProformaWeeklyLifting_Resize( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 MyBase .
如果 我 .WindowState = FormWindowState.最小化然后
我 .Hide()
NotifyIcon1.Visible = 真
结束 如果
结束 子
我的问题是程序已经运行并且在托盘中(图标正在显示,程序处于隐藏状态),如果用户再次运行该程序(意味着用户再次运行exe)
那么我想显示以前的实例,而不是允许程序运行多个实例.
通过调用以下代码,我可以知道程序是否已经运行,但是我不知道如何显示前一个实例.
私有 Sub CheckInstanceOfApp()
Dim appProc() As 流程
Dim strModName,strProcName As 字符串
strModName = Process.GetCurrentProcess.MainModule.ModuleName
strProcName = System.IO.Path.GetFileNameWithoutExtension(strModName)
appProc = Process.GetProcessesByName(strProcName)
如果 appProc.Length> 1 然后
MessageBox.Show(" )
其他
MessageBox.Show(" )
结束 如果
结束 子
非常感谢您的帮助.
谢谢和最好的问候
通过使用命名的Mutex,您可以做到这一点.首先创建一个名为Mutex的文件,并在启动应用程序时进行检查.这是一个可以帮助您单实例C#应用程序-.NET 2.0的链接 [ ^ ]
I找到了解决方法
这些是我的做法
1.制作单个实例
在应用程序"选项卡上转到项目属性",然后选中制作单实例应用程序".
2.显示以前的实例
在"Application.Designer.vb"中选择"StartupNextInstance"事件,添加"e.BringToForeground = True"
例如.私有 Sub MyApplication_StartupNextInstance( As ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs)句柄 真 frmGenerateProformaWeeklyLifting.NotifyIcon1.Visible = 错误 我 .MainForm.Show() 我 .MainForm.WindowState = FormWindowState.Normal 结束 子
它确实适合我的情况.
Laxmikant_Yadav,谢谢您的回答.您能帮我吗,我在VB中使用Visual Studio 2010 Ultimate.我只是不太明白这是子还是什么.
I use NotifyIcon control to hide the form and show icon in tray when user minimize the program.
If user Double click on icon then show the program on screen. It works in order.
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
NotifyIcon1.Visible = False
Me.Show()
Me.WindowState = FormWindowState.Normal
End Sub
Private Sub frmGenerateProformaWeeklyLifting_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.Hide()
NotifyIcon1.Visible = True
End If
End Sub
My problem is while the program is already run and it''s in tray(icon is showing and program is hiding status), if user run the program again(means user run exe again)
then I want to show previous instance instead of allow program running multiple instance.
By calling the following code I can know if program is already run, but I don''t know how to show the previous instance.
Private Sub CheckInstanceOfApp()
Dim appProc() As Process
Dim strModName, strProcName As String
strModName = Process.GetCurrentProcess.MainModule.ModuleName
strProcName = System.IO.Path.GetFileNameWithoutExtension(strModName)
appProc = Process.GetProcessesByName(strProcName)
If appProc.Length > 1 Then
MessageBox.Show("There is an instance of this application running.")
Else
MessageBox.Show("There are no other instances running.")
End If
End Sub
Would appreciate your help.
Thanks and Best Regards
By Using Named Mutex you can do this. First create a named Mutex and check for it while starting your application. Here is one link which might help you Single-Instance C# Application - for .NET 2.0[^]
I found the solution
These are how I did
1. To make single instance
Got to Project Properties at "Application" Tab, then checked "Make Single instance application".
2. To show previous instance
In "Application.Designer.vb" Select "StartupNextInstance" event add "e.BringToForeground = True"
eg.Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance e.BringToForeground = True frmGenerateProformaWeeklyLifting.NotifyIcon1.Visible = False Me.MainForm.Show() Me.MainForm.WindowState = FormWindowState.Normal End Sub
It did work for my case.
Laxmikant_Yadav, thank you for your answer.
Hello, I had that same problem, still cant figure out what im doing wrong. Can you please help me, I am using visual studio 2010 ultimate in VB. I just don''t quite understand if this is a sub or what.
这篇关于如何显示先前的实例,如果应用程序已在运行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!