|
-
Apr 4th, 2006, 02:37 AM
#1
[2.0] Bring another, running Application to Focus
Is there a way to bring in another, running application into Focus entirely in .Net (so I can avoid API calls)?
I'm basically trying to make it so only 1 instance of my app is shown and if a user tries to run another instance, it brings the already running versions to the front.
-
Apr 4th, 2006, 02:57 AM
#2
Re: [2.0] Bring another, running Application to Focus
The only way that I'm aware of that doesn't use APIs is the VB.NET AppActivate function, but it won't restore a minimised window. VB 2005 has single-instance functionality built in. I don't know of one but there may be a way to leverage that functionality from a C# app. I've seen articles about using the VB My namespace in C# so it might be an extension of that.
-
Apr 4th, 2006, 07:02 AM
#3
Re: [2.0] Bring another, running Application to Focus
I submitted an article in the codebank about using a Mutex class to create a single-instance and that works great but I don't see anyway of leveraging that to bring the first instance into focus.
Not sure how to use any of the VB specific methods to help in this situation.
I would use an API to do this but I'd like to stay away from it so my app will continue to run under the Mono framework (so I can run it on linux).
-
Apr 4th, 2006, 05:38 PM
#4
Re: [2.0] Bring another, running Application to Focus
I'd guess that VB-specific features like Runtime functions and My would be low on the list of priorities for Mono as most people developing for Mono would be using C#. I'm not aware of any "pure .NET" method of doing what you want, outside of the in-built VB 2005 support that may never see the light of day in Mono. I suppose that you could set up some form of IPC between instances of your app, so the new instance can speak directly to the existing instance.
-
Apr 4th, 2006, 09:56 PM
#5
Re: [2.0] Bring another, running Application to Focus
You can use the System.Diagnostics.Process.GetProcessesByName() to get a Process object that represents your other process, and then perhaps write a message to it on it's StandardInput. In your code, you could have a thread polling on the standard input, and if you receive a message, bring the application to the front.
Probably not the best way to do it, but probably pretty portable.
Edit:
If you add a reference to Microsoft.VisualBasic.dll, you can do the following:
I couldn't test this under mono since apparently my mono install seems to have been borked and can't load libgdiplus, even though it's specified in LD_LIBRARY_PATH.
Code:
Process pThis = Process.GetCurrentProcess();
Process[] pMatches = Process.GetProcessesByName(pThis.ProcessName);
if (pMatches.Length == 2)
{
Process pOther = pMatches[0];
if (pOther.Id == pThis.Id)
pOther = pMatches[1];
Microsoft.VisualBasic.Interaction.AppActivate(pOther.Id);
return;
}
Last edited by sunburnt; Apr 4th, 2006 at 10:21 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Apr 4th, 2006, 11:29 PM
#6
Re: [2.0] Bring another, running Application to Focus
I can't use GetProcessesByName() as it is not reliable. If my app's executable is "MyApp.exe" and company Y already makes a product with its executable name as "MyApp.exe", then I may get their process and not mine.
This was one of the reasons I'm using a Mutex to ensure only 1 instance of my app is active at any given time.
I'm going to look into using IPC for this.
-
Apr 4th, 2006, 11:58 PM
#7
Re: [2.0] Bring another, running Application to Focus
-
Apr 5th, 2006, 06:01 PM
#8
Re: [2.0] Bring another, running Application to Focus
 Originally Posted by dee-u
I already said I'm trying to stay away from using the Win32 API.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|