how to set focus and launch the already running application on button click event in c#.net3.5?

血红的双手。 提交于 2019-12-01 19:03:46

This is be able to find and switch to an already running process that matches what you are trying to start.

[DllImport( "user32.dll" )]
public static extern bool ShowWindowAsync( HandleRef hWnd, int nCmdShow );
public const int SW_RESTORE = 9;

public void SwitchToCurrent() {
  IntPtr hWnd = IntPtr.Zero;
  Process process = Process.GetCurrentProcess();
  Process[] processes = Process.GetProcessesByName( process.ProcessName );
  foreach ( Process _process in processes ) {
    // Get the first instance that is not this instance, has the
    // same process name and was started from the same file name
    // and location. Also check that the process has a valid
    // window handle in this session to filter out other user's
    // processes.
    if ( _process.Id != process.Id &&
      _process.MainModule.FileName == process.MainModule.FileName &&
      _process.MainWindowHandle != IntPtr.Zero ) {
      hWnd = _process.MainWindowHandle;

      ShowWindowAsync( NativeMethods.HRef( hWnd ), SW_RESTORE );
      break;
    }
  }
 }
Josh

I posted an answer a while back to a question about Delphi. I explained that I didn't have a background in Delphi but I described at a high level what I did in C# to build a component that uses InterProcess Communication (IPC) with .NET remoting to not only activate a running instance, but also forward the command line parameters from the second instance into the first instance. I linked to a pretty simple to use component that wraps all this functionality up. It may be useful to you.

Hers's my answer from the other question:

The best way to do this is actually in the the startup code of your exe. In other words, let Explorer launch a second copy of the exe which then proceeds to detect that it is already running and have it send a message to the running instance.

Personally, I have practically no experience with Delphi, but the way I did this in a .NET application was using a mutex and an interprocess communication channel.

The general idea was that the first instance of the application would start, and begin listening on an IPC channel. It would also create a named interprocess mutex. When the second instance launched, it would be unable to create the mutex of the same name which meant that a previous instance was running and listening for calls on the IPC channel. The second instance then sent the command line arguments to the first instance over IPC and the first instance took action on them. The second instance then exits without showing any UI.

I've uploaded the code for this component (C#) and the link is below. I don't believe it has any external dependencies and I don't know what the equivalent communication mechanism in Delphi would be - but hopefully this gives you some ideas.

InstanceManager Component (C#)

Note that usage of named mutexes is discouraged for security reasons. Any process (even one running under guest account) can create a mutex with the same name before your process was started. Solving these security problems is usually harder than just not using named mutex at all.

To solve your problem, you just need to store process handler or process ID and then look for a window with that process ID. This is similar to the way task manager works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!