Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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.
VB
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
Posted

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.
VB
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.
 
Share this answer
 
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[^]
 
Share this 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900