I'm using OpenNETCF 2.3 in a .NET Compact Framework 3.5 mobile device application. At program startup I am looking for duplicate running instances of my application. I am confused why the current ProcessID I get from various methods seems to be incorrect about half the time.
Public Shared Sub Main()
Dim appName As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name & ".exe"
Dim intCurrentProcessId As Integer = OpenNETCF.Diagnostics.ProcessHelper.GetCurrentProcessID
'Dim intCurrentProcessId As Integer = Process.GetCurrentProcess.Id()
For Each p As ToolHelp.ProcessEntry In ToolHelp.ProcessEntry.GetProcesses
If p.ProcessID <> intCurrentProcessId AndAlso p.ExeFile.Contains(appName) Then
MessageBox.Show("KILLING p.ExeFile: " & p.ExeFile & " p.processid: " & p.ProcessID & " intCurrentProcessId: " & intCurrentProcessId)
'p.Kill()
End If
Next
End Sub
Why is it that intCurrentProcessId would seemingly randomly not match p.ProcessId and be a negative number?
My rep is too low to post a screenshot that shows p.ProcessID = 3459667490 and intCurrentProcessID = -835299806.
Please understand that alternate methods of testing for single instances don't interest me. Thank you.
First, a quick look at signed versus unsigned numbers (feel free to use Calculator on your desktop to validate these).
3459667490 in unsigned decimal is 0xCE365622 in 32-bit hex.
-835299806 in signed decimal is 0xCE365622 in 32-bit hex.
See the similarity?
If you cast so the are both either signed or unsigned I'm willing to bet that 100% of the time the ID's will match.