Results 1 to 8 of 8

Thread: [2.0] Bring another, running Application to Focus

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Question [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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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).
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  6. #6

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [2.0] Bring another, running Application to Focus

    How about this?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2.0] Bring another, running Application to Focus

    Quote Originally Posted by dee-u
    How about this?
    I already said I'm trying to stay away from using the Win32 API.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width