Solved

Handle of my appilication?

Posted on 2007-04-02
6
170 Views
Last Modified: 2010-04-23
Hello

I want to search for mutliple instances of my application.
So I am searching through all processes and if the exename is the same as my app-exename, I add currentinstances +1
if currentinstances > 1 then I want to kill all old instances

so I loop again through all processes and then if the exename is the same as my app exe-name, I want to compare the handles, to check if this instance is the actual running instance.

So I tried:

                        Dim prochandle As String = oprocess.Handle.ToString
                        Dim myhandle As String = Me.Handle.ToString
                        If prochandle <> myhandle Then
                            oprocess.Kill()
                        End If

But this did not work, Me.Handle seems to be different from the Process.Handle of my application...

What is wrong?

Thanks

Sven
0
Comment
Question by:Ruttensoft
6 Comments
 
LVL 2

Expert Comment

by:MasterThai
ID: 18836552
This should be of some help.
http://www.developerfusion.co.uk/show/134/
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18836695
this will return you the handle

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
returns long handle >> FindWindow(, "WINDOW TITLE")
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18836962
"So I am searching through all processes and if the exename is the same as my app-exename"

You are doing more work than you need too...

Use Process.GetProcessesByName():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassgetprocessesbynametopic1.asp

Something like...

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim appName = System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)
        Dim ps() As Process = Process.GetProcessesByName(appName)        
        While ps.Length > 1
            ps(0).Kill() ' or ps(0).CloseMainWindow()
            ps = Process.GetProcessesByName(appName)        
        Wend
    End Sub

End Class

You sure you want to kill OLD instances?

What version VB.Net are you working in?

Do you need to pass info to the old and/or new instances?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question