How to check if an MS Word is closed in C#
I am doing an application in C# which open exe files like notepad, calculator.<br /> So, my aim is when I open an exe file like Word, a button is created automatically<br /> and then when I close the Word app, the button will be removed. <br />Each app will have its own button when it is launched. <br />I tried to do it but I have some issues with the closing of the app. <br /><div>Here is what I did: </div><div> </div><div><div class="dp-highlighter"><ol class="dp-c" start="1"><li class="alt"><span><span>var applicationWord = </span><span class="keyword">new</span><span> Microsoft.Office.Interop.Word.Application(); </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> applicationWord.Visible = <span class="keyword">true</span><span>; </span></span></li><li class="alt"><span> applicationWord.DocumentBeforeClose += DocumentBeforeClose; </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">if</span><span> (applicationWord.Visible == </span><span class="keyword">true</span><span>) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> button = <span class="keyword">new</span><span> Button(); </span></span></li><li class=""><span> button.Image = Properties.Resources.word_80; </span></li><li class="alt"><span> PIC_Barre.Controls.Add(button); </span></li><li class=""><span> button.AutoSize = <span class="keyword">true</span><span>; </span></span></li><li class="alt"><span> <span class="comment">// button.Tag = proc.Id;</span><span> </span></span></li><li class=""><span> PIC_Barre.Controls.Add(button); </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">foreach</span><span> (Process proc </span><span class="keyword">in</span><span> Process.GetProcessesByName(</span><span class="string">"WINWORD"</span><span>)) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">if</span><span> (proc.ProcessName.Contains(</span><span class="string">"WINWORD"</span><span>)) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> proc.WaitForInputIdle(); </span></li><li class=""><span> { </span></li><li class="alt"><span> Thread.Sleep(500); </span></li><li class=""><span> SetWindowPos(proc.MainWindowHandle.ToInt32(), </span></li><li class="alt"><span> (<span class="keyword">int</span><span>)SetWinPos_ZOrderOpt.HWND_TOPMOST, </span></span></li><li class=""><span> 0, 0, 0, 0, </span></li><li class="alt"><span> (<span class="keyword">int</span><span>)(SetWinPosFlags.SWP_NOSIZE | </span></span></li><li class=""><span> SetWinPosFlags.SWP_NOMOVE)); </span></li><li class="alt"><span> } </span></li><li class=""><span> button.Click += (s, e) => { ShowWindowAsync(proc.MainWindowHandle, (<span class="keyword">int</span><span>)ShowWindowCommands.Normal); }; </span></span></li><li class="alt"><span> proc.Exited += (s, e) => </span></li><li class=""><span> { </span></li><li class="alt"><span> var method = (Action)(() => PIC_Barre.Controls.Remove(button)); </span></li><li class=""><span> <span class="comment">// button.Visible = false;</span><span> </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (button.InvokeRequired) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> button.Invoke(method); </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> }; </span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> } <br /></span></li></ol></div> </div>