473,659 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sending events to another process

3 New Member
Hi there,

I'm writing an application with forms. my main window can be hidden (minimize to a tray icon) like...Microsof t Outlook for example.

this application can run only one instance (again like outlook) - meaning that on initialization, I check if there is another process running and if so I exit from the new one.

in case the main window is hidden and the user tries to open the application again (from the start menu or desktop icon...), I want to re-display the main window and bring it to foreground as if it was restored from the tray icon (who's keep saying outlook???)

my problem is that I don't know how to do so. I can send a window message in case the main window is minimized by using windows API but how can this be done if the window is hidden????

Thanx in advance,

Uv
Dec 9 '08 #1
5 11447
anijos
52 New Member
Hi,

Try

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. class AppMain
  5. {
  6.     [DllImport("user32.dll")] private static extern 
  7.         bool SetForegroundWindow(IntPtr hWnd);
  8.     [DllImport("user32.dll")] private static extern 
  9.         bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  10.     [DllImport("user32.dll")] private static extern 
  11.         bool IsIconic(IntPtr hWnd);
  12.  
  13.     private const int SW_HIDE = 0;
  14.     private const int SW_SHOWNORMAL = 1;
  15.     private const int SW_SHOWMINIMIZED = 2;
  16.     private const int SW_SHOWMAXIMIZED = 3;
  17.     private const int SW_SHOWNOACTIVATE = 4;
  18.     private const int SW_RESTORE = 9;
  19.     private const int SW_SHOWDEFAULT = 10;
  20.  
  21.     static void Main()
  22.     {
  23.         // get the name of our process
  24.         string proc=Process.GetCurrentProcess().ProcessName;
  25.         // get the list of all processes by that name
  26.         Process[] processes=Process.GetProcessesByName(proc);
  27.         // if there is more than one process...
  28.         if (processes.Length > 1)
  29.         {
  30.             // Assume there is our process, which we will terminate, 
  31.             // and the other process, which we want to bring to the 
  32.             // foreground. This assumes there are only two processes 
  33.             // in the processes array, and we need to find out which 
  34.             // one is NOT us.
  35.  
  36.             // get our process
  37.             Process p=Process.GetCurrentProcess();
  38.             int n=0;        // assume the other process is at index 0
  39.             // if this process id is OUR process ID...
  40.             if (processes[0].Id==p.Id)
  41.             {
  42.                 // then the other process is at index 1
  43.                 n=1;
  44.             }
  45.             // get the window handle
  46.             IntPtr hWnd=processes[n].MainWindowHandle;
  47.             // if iconic, we need to restore the window
  48.             if (IsIconic(hWnd))
  49.             {
  50.                 ShowWindowAsync(hWnd, SW_RESTORE);
  51.             }
  52.             // bring it to the foreground
  53.             SetForegroundWindow(hWnd);
  54.             // exit our process
  55.             return;
  56.         }
  57.         // ... continue with your application initialization here.
  58.     }
  59. }
AniJos
Dec 9 '08 #2
uvklein
3 New Member
Thanx anijos for your reply but I've already done that. if the window is minimized it works fine but in my case the window is not visible and the window's handle return from the call

IntPtr hWnd=processes[n].MainWindowHand le

is zero.

Thanx any way,

any other thoughts?

Uv
Dec 9 '08 #3
anijos
52 New Member
Try Mutex.
You tried this also?

Expand|Select|Wrap|Line Numbers
  1. using System.Windows.Forms;
  2.  
  3. using System.Runtime.InteropServices;
  4.  
  5. using System.Text;
  6.  
  7. using System.Diagnostics;
  8.  
  9. using System.Threading;
  10.  
  11. using System.Reflection;
  12.  
  13. using System.IO;
  14.  
  15. namespace BusLayer
  16.  
  17. {
  18.  
  19. /// <summary>
  20.  
  21. /// Summary description for SingleApp.
  22.  
  23. /// </summary>
  24.  
  25. public class SingleApplication
  26.  
  27. {
  28.  
  29. public SingleApplication()
  30.  
  31. {
  32.  
  33. }
  34.  
  35. /// <summary>
  36.  
  37. /// Imports 
  38.  
  39. /// </summary>
  40.  
  41.  
  42. [DllImport("user32.dll")]
  43.  
  44. private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
  45.  
  46. [DllImport("user32.dll")]
  47.  
  48. private static extern int SetForegroundWindow(IntPtr hWnd);
  49.  
  50. [DllImport("user32.dll")]
  51.  
  52. private static extern int IsIconic(IntPtr hWnd);
  53.  
  54. /// <summary>
  55.  
  56. /// GetCurrentInstanceWindowHandle
  57.  
  58. /// </summary>
  59.  
  60. /// <returns></returns>
  61.  
  62. private static IntPtr GetCurrentInstanceWindowHandle()
  63.  
  64.  
  65. IntPtr hWnd = IntPtr.Zero;
  66.  
  67. Process process = Process.GetCurrentProcess();
  68.  
  69. Process[] processes = Process.GetProcessesByName(process.ProcessName);
  70.  
  71. foreach(Process _process in processes)
  72.  
  73. {
  74.  
  75. // Get the first instance that is not this instance, has the
  76.  
  77. // same process name and was started from the same file name
  78.  
  79. // and location. Also check that the process has a valid
  80.  
  81. // window handle in this session to filter out other user's
  82.  
  83. // processes.
  84.  
  85. if (_process.Id != process.Id &&
  86.  
  87. _process.MainModule.FileName == process.MainModule.FileName &&
  88.  
  89. _process.MainWindowHandle != IntPtr.Zero) 
  90.  
  91. {
  92.  
  93. hWnd = _process.MainWindowHandle;
  94.  
  95. break;
  96.  
  97. }
  98.  
  99. }
  100.  
  101. return hWnd;
  102.  
  103. }
  104.  
  105. /// <summary>
  106.  
  107. /// SwitchToCurrentInstance
  108.  
  109. /// </summary>
  110.  
  111. private static void SwitchToCurrentInstance()
  112.  
  113.  
  114. IntPtr hWnd = GetCurrentInstanceWindowHandle();
  115.  
  116. if (hWnd != IntPtr.Zero) 
  117.  
  118.  
  119. // Restore window if minimised. Do not restore if already in
  120.  
  121. // normal or maximised window state, since we don't want to
  122.  
  123. // change the current state of the window.
  124.  
  125. if (IsIconic(hWnd) != 0)
  126.  
  127. {
  128.  
  129. ShowWindow(hWnd, SW_RESTORE);
  130.  
  131. }
  132.  
  133. // Set foreground window.
  134.  
  135. SetForegroundWindow(hWnd);
  136.  
  137. }
  138.  
  139. }
  140.  
  141. /// <summary>
  142.  
  143. /// Execute a form base application if another instance already running on
  144.  
  145. /// the system activate previous one
  146.  
  147. /// </summary>
  148.  
  149. /// <param name="frmMain">main form</param>
  150.  
  151. /// <returns>true if no previous instance is running</returns>
  152.  
  153. public static bool Run(System.Windows.Forms.Form frmMain)
  154.  
  155. {
  156.  
  157. if(IsAlreadyRunning())
  158.  
  159. {
  160.  
  161. //set focus on previously running app
  162.  
  163. SwitchToCurrentInstance();
  164.  
  165. return false;
  166.  
  167. }
  168.  
  169. Application.Run(frmMain);
  170.  
  171. return true;
  172.  
  173. }
  174.  
  175. /// <summary>
  176.  
  177. /// for console base application
  178.  
  179. /// </summary>
  180.  
  181. /// <returns></returns>
  182.  
  183. public static bool Run()
  184.  
  185. {
  186.  
  187. if(IsAlreadyRunning()) 
  188.  
  189. {
  190.  
  191. return false;
  192.  
  193. }
  194.  
  195. return true;
  196.  
  197. }
  198.  
  199. /// <summary>
  200.  
  201. /// check if given exe alread running or not
  202.  
  203. /// </summary>
  204.  
  205. /// <returns>returns true if already running</returns>
  206.  
  207. private static bool IsAlreadyRunning()
  208.  
  209. {
  210.  
  211. string strLoc = Assembly.GetExecutingAssembly().Location;
  212.  
  213. FileSystemInfo fileInfo = new FileInfo(strLoc);
  214.  
  215. string sExeName = fileInfo.Name;
  216.  
  217. bool bCreatedNew;
  218.  
  219. mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
  220.  
  221. if (bCreatedNew)
  222.  
  223. mutex.ReleaseMutex();
  224.  
  225. return !bCreatedNew;
  226.  
  227. }
  228.  
  229.  
  230. static Mutex mutex;
  231.  
  232. const int SW_RESTORE = 9;
  233.  
  234. }
  235.  
  236. }
Dec 9 '08 #4
uvklein
3 New Member
anijos, again, thanks for your help.

this code won't contribute to the solution I seek for because the "already running" process's window is not visible, hence no handle is found. I think the solution should be something like firing an event to the process in order to notify it should restore the non-visible window. do you know if such thing can be done?
Dec 9 '08 #5
nukefusion
221 Recognized Expert New Member
How about this. Identify the already existing process using code similar to that in the first sample posted by Anijos. Then use some code like the following sample to loop all windows and identify those that share the process ID, restoring them if necessary. You may want to tweak it slightly, i.e. to check whether a window is actually a notify icon or not, etc.

Expand|Select|Wrap|Line Numbers
  1.         [DllImport("user32.dll", SetLastError = true)]
  2.         static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  3.  
  4.         [DllImport("user32.dll", SetLastError=true)]
  5.         static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  6.  
  7.         [DllImport("user32.dll")]
  8.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  9.         private const int SW_RESTORE = 9;
  10.  
  11.         public void restoreWindow(uint procIDtoFind)
  12.         { 
  13.             uint processID;
  14.             IntPtr hwnd = IntPtr.Zero;
  15.             // Loop through windows
  16.             do
  17.             {
  18.                 processID = 0;
  19.                 // Get HWND
  20.                 hwnd = FindWindowEx(IntPtr.Zero, hwnd, null, null);
  21.                 // Get process ID
  22.                 GetWindowThreadProcessId(hwnd, out processID);
  23.                 if (processID == procIDtoFind)
  24.                 {
  25.                     // restore the window
  26.                     ShowWindow(hwnd, SW_RESTORE);
  27.                     break;
  28.                 }
  29.             } 
  30.             while (!hwnd.Equals(IntPtr.Zero));
  31.         }
  32.  
Dec 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
8321
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG: "Could Not Copy Temporary Files to the Output Directory" Error Message When You Build a Solution That Contains Multiple Projects I have tried all the solutions in Microsoft Knowledge Base Article - 313512.
1
5521
by: mikes | last post by:
Hello, I am building a c# web app that uses serialization to generate xml files within a page load event. The page support both creation and modification events. I do not have a problem with the creation function. When I resubmit the page to mod the previously created xml file I get "file xxxx is being used by another process." It appears that all object have be correctly closed but the XmlTextWriter or the FileStream objects have not...
8
1242
by: Corey Scheich | last post by:
I am writing a simple exe that will locate a company file for the user with minimal input and open it in the appropriate application (Same as Explorer) Problem is sometimes the user may want to open a few files. I was opening them in a For....Next loop and it seems that if a file takes longer than a few seconds to open it is dropping the other files. I presume that I need to wait for the process that was sent to stop I think what I need...
13
11127
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
2
1419
by: Victor Rosenberg | last post by:
Hey guys How can I painlessly execute some heavy processing in another process, so the main application may still be closed, but the processing will continue until its done? Also, how can progressing events be implemented in this scenario? Thanks in advance
3
6440
by: Builder | last post by:
Is is possible to send events through application domain boundaries (i.e. from one application domain to another)?
0
960
by: eashokan | last post by:
Hi.. I am able to get the process (as object) running in the sysetm through process id. I need to trigger the event that corresponds to the process (object) i got. Note that the Process object i get is not started by me or started in separate application. In my application, i need to get the object and the trigger of the process events - like process.Exited. code snippet, AddHandler gService.toolProc.Exited, AddressOf...
0
811
by: tamirmalas | last post by:
Hi, iam new to this forum, actually this is my first post, and i have a question, how to read the innerhtml of a web browser created in another process (internet explorer or just a program that uses the web browser)? i saw some code on the net that uses the SHDocVw.ShellExplorer but it wasnt helpfull because it reads only the internet explorer documents (not the web browsers in created in another programs)
0
1847
by: Tim Golden | last post by:
aditya shukla wrote: Why on earth are you *specifying* c:\python25 as the directory for this file? It's automatically created in your user-specific temp directory which has specific permissions and is (obviously) specific to that user. You may -- depending on your needs -- also not need the suffix & prefix. ie, unless you have some specific need in mind, just use:
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.