Use Running App

B

Brian P. Hammer

All - I have used mtux to check for a running app and close it down if it a
previous app instance is running. Now, when a user double clicks on one of
the saved application files, I'd like for the existing instance of the
application to come to the front and use the file the user double clicked.
I have no issues doing this when no instance of the app is running but if it
is already running, how do I go about this?

Thanks,
Brian
 
B

Bob Powell [MVP]

See the article in Windows Forms Tips and Tricks.

It doesn't use a mutex but it does bring the running application to the
foreground.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Brian P. Hammer

Thanks Bob. I'll give it a shot.


Bob Powell said:
See the article in Windows Forms Tips and Tricks.

It doesn't use a mutex but it does bring the running application to the
foreground.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Brian P. Hammer

Bob - This doesn't seem to work in 2005. RunningProcesses.Length always
return 0

Thanks,
Brian

Public Enum....

<Runtime.InteropServices.DllImport("User32.dll")> _
Public Shared Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal swCommand
As Integer) As Integer
End Function


<STAThread()> _
Public Shared Sub Main()
Dim RunningProcesses As Process() = Process.GetProcessesByName("Test")
'Change the name above to suit your application
If (RunningProcesses.Length = 1) Then
Application.Run(New MainGUI.MDIMain)
Else
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_SHOWMINIMIZED)
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_RESTORE)
End If
End Sub
 
B

Brian P. Hammer

Never mind.... It's too late to miss the simple thing of my app name...

Brian
 
B

Brian P. Hammer

Bob - Thanks for your help thus far. The only thing I cannot seem to
overcome is when I use ShowWindowAsync, how do I get a handle to the current
running app so I can pass the opening file as a string to a procedure in the
main form?

Thanks,
Brian
 
J

Jeffrey Tan[MSFT]

Hi Brian,

I am not sure what your current problem is, can you provide a little more
context information of your problem so that I can understand it? Actually,
I did not find the article "Bob Powell [MVP]" pointed to in
http://www.bobpowell.net/tipstricks.htm, so I do not know how you solution
uses ShowWindowAsync.

I will wait for your further information. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Powell [MVP]

Hi Jeffrey and Brian.

Jeffry, this is the link: http://www.bobpowell.net/singleinstance.htm

Brian, the code clearly shows how to use the MainWindowHandle from the array
of running processes.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Thanks for your good follow up and your information sharing. Let's wait
Brian to see if this meets his need. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian P. Hammer

To an ole pro like you it does. ;-)

To a beginner like me, it doesn't say much but to copy it and paste into my
app. :)

I'll play around with it a bit and see if I can figure out how to call a
procedure on a form of the existing running instance.

Thanks Bob,
Brian



Bob Powell said:
Hi Jeffrey and Brian.

Jeffry, this is the link: http://www.bobpowell.net/singleinstance.htm

Brian, the code clearly shows how to use the MainWindowHandle from the
array of running processes.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"Jeffrey Tan[MSFT]" said:
Hi Brian,

I am not sure what your current problem is, can you provide a little more
context information of your problem so that I can understand it?
Actually,
I did not find the article "Bob Powell [MVP]" pointed to in
http://www.bobpowell.net/tipstricks.htm, so I do not know how you
solution
uses ShowWindowAsync.

I will wait for your further information. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi Brian,

Thanks for your feedback.

Actually, I am not sure of what is the problem you are real facing. Bob's
code leverages Process.GetProcessesByName() method to determine if there
are more than 1 process in the system has the same name as your
application, if so, there must be a previous application running. So it
p/invokes Win32 api ShowWindowAsync to post a show-window event to the
message queue of the previous process' main window.

The first parameter passed to ShowWindowAsync API is the window we want to
active, which we obtained through RunningProcesses[0].MainWindowHandle,
that is Process.MainWindowHandle property(you can search this in MSDN for
more information).

Does this make this process clear? If you have any more concern, please
feel free to tell us, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian P. Hammer

Jeffery - Beginners pains....

My issue was Application.Run(new MDIMain). I declared frmMDI outside of the
procedure and just call Application.Run(frmMDI) if it is the first app or
just pass my data to frmMDI.SoProcedure...

Thanks for sticking with me.

Regards,
Brian
 
B

Brian P. Hammer

Jeffery - Did you have an opportunity to look at the sample app I posted?

Thanks,
Brian
 
J

Jeffrey Tan[MSFT]

Hi Brian,

I am sorry, but I have reviewed your sample project yesterday, and posted a
modified sample to you in a further reply. Can you see my further reply? If
you fail to get my reply, please feel free to send a email to
(e-mail address removed)(remove "online."), I will email the modified
sample to you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian P. Hammer

Jeffery - I sent you an email. I can now see the post only on Microsoft's
web enable reader but unable from there to download attachments. Please
send me the modified sample app to (e-mail address removed). Please remove
nospammers.

Thanks for all the help,
Brian
 
B

Brian P. Hammer

sorry, make that (e-mail address removed) or
(e-mail address removed)


Thanks,
Brian



Brian P. Hammer said:
Jeffery - I sent you an email. I can now see the post only on Microsoft's
web enable reader but unable from there to download attachments. Please
send me the modified sample app to (e-mail address removed). Please
remove nospammers.

Thanks for all the help,
Brian



"Jeffrey Tan[MSFT]" said:
Hi Brian,

I am sorry, but I have reviewed your sample project yesterday, and posted
a
modified sample to you in a further reply. Can you see my further reply?
If
you fail to get my reply, please feel free to send a email to
(e-mail address removed)(remove "online."), I will email the modified
sample to you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi Brian,

Thanks for your feedback.

Yes, you can notget the attachment through IE, but you can get it through
Outlook Express. Anyway, I have replied your email with the modified sample
project. If you have any further issue, please feel free to feedback here.
Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Brian P. Hammer

Thanks, got your email. For what ever reason, the original message never
shows up in OE. Even after a reset of the folder.

Regards,
Brian
 
J

Jeffrey Tan[MSFT]

Ok, if you have any further issue, please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top