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
Asked
Active
Viewed 1.4k times
2 Answers
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
-
Thank you! works perfectly! Wondering if you know of a similar work around for dealing with Unload? – Daniel Grindstaff Sep 08 '10 at 15:36
-
http://bytes.com/topic/visual-basic-net/answers/349334-whats-equivalent-unload-form1-vb-net – Robert Harvey Sep 08 '10 at 16:04
-
You have saved this poor mortal C# developer an aneurysm thanks to the imperfect VB code im tasked to port. – Luis Robles Aug 31 '11 at 16:21
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