Tags: currentprocess, diagnostics, focus, instance, net, process, sharp, single, system

How to set focus to other process ?

On .Net » .Net C# (C sharp)

3,630 words with 3 Comments; publish: Mon, 02 Jun 2008 17:43:00 GMT; (100109.38, « »)

I need to allow single instance of my process.

I did found the other System.Diagnostics.Process as follow:

System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

// Get all instances of GciViewer running on the local machin.

System.Diagnostics.Process[] localByName = System.Diagnostics.Process.GetProcessesByName("GciViewer");

foreach( System.Diagnostics.Process process in localByName )

{

if( currentProcess.Id != process.Id )

{

// TO DO: How to set the focus to the process !!

}

}

How can I set focus to other process?

--

Thanks

Sharon

All Comments

Leave a comment...

  • 3 Comments
    • Hello Sharon,

      First, use Mutex with "Global\" pattern for single app instance

      And SetForegroundWindow + MainWindowHandle API function (apply pinvoke) to

      bring process on front

      S> I need to allow single instance of my process.

      S> I did found the other System.Diagnostics.Process as follow:

      S> System.Diagnostics.Process currentProcess =S> System.Diagnostics.Process.GetCurrentProcess();

      S> // Get all instances of GciViewer running on the local machin.

      S> System.Diagnostics.Process[] localByName =S> System.Diagnostics.Process.GetProcessesByName("GciViewer");

      S> foreach( System.Diagnostics.Process process in localByName )

      S> {

      S> if( currentProcess.Id != process.Id )

      S> {

      S> // TO DO: How to set the focus to the process !!

      S> }

      S> }

      S> How can I set focus to other process?

      S>

      --

      WBR,

      Michael Nemtsev :: blog: http://spaces.msn.com/laflour

      "At times one remains faithful to a cause only because its opponents do not

      cease to be insipid." (c) Friedrich Nietzsche

      #1; Mon, 02 Jun 2008 17:44:00 GMT
    • I was hoping for C# API/code.

      But if I must use the Win32 API; can I use the send SendMessage() with

      WM_SETFOCUS ?

      Can you post some sample code on how to set the focus to the other process?

      Thanks

      Sharon

      #2; Mon, 02 Jun 2008 17:45:00 GMT