5

I have 'Applications.PrevInstance' in VB 6 code that I am trying to upgrade to .NET using VS 2008. Apparently this code is no longer valid. Does anyone have any ideas about upgraded solution? TIA

MarkJ
  • 30,070
  • 5
  • 68
  • 111
Daniel Grindstaff
  • 105
  • 1
  • 2
  • 8

2 Answers2

9

See here:

http://www.knowdotnet.com/articles/previnstance.html

Public Sub Main()
   If PrevInstance() Then Exit Sub

   ' continue with your application
   UserName = Environ("UserName")
   ComputerName = Environ("COMPUTERNAME")

End Sub

Function PrevInstance() As Boolean
  If UBound(Diagnostics.Process.GetProcessesByName _
     (Diagnostics.Process.GetCurrentProcess.ProcessName)) _
     > 0 Then
     Return True
  Else
     Return False
  End If
End Function
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0
Function PrevInstance() As Boolean
    If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
        PrevInstance = True
    Else
        UserName = Environ("UserName")
        Computername = Environ("COMPUTERNAME")
        PrevInstance = False
    End If
    Dim i, n As Integer, RepForm As String
    For i = My.Application.OpenForms.Count - 1 To 1 Step -1
        RepForm = My.Application.OpenForms.Item(i).Name
        For n = My.Application.OpenForms.Count - 1 To 1 Step -1
            If My.Application.OpenForms.Item(n).Name = My.Application.OpenForms.Item(i).Name And n > i Then
                My.Application.OpenForms(i).Close()
                PrevInstance = True
                Exit Function
            End If
        Next n
    Next i
End Function
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
  • in the Function above you have two functions: the first for the App.exe itself and the second for the forms in the app.exe. So you need to choose which you need and separate them in your app. Ricky – user7114634 Nov 04 '16 at 11:00