Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am doing an application in C# which open exe files like notepad, calculator.
So, my aim is when I open an exe file like Word, a button is created automatically
and then when I close the Word app, the button will be removed.
Each app will have its own button when it is launched.
I tried to do it but I have some issues with the closing of the app.
Here is what I did:
C#
var applicationWord = new Microsoft.Office.Interop.Word.Application();


            applicationWord.Visible = true;
               applicationWord.DocumentBeforeClose += DocumentBeforeClose;



               if (applicationWord.Visible == true)
               {


                       button = new Button();
                       button.Image = Properties.Resources.word_80;
                       PIC_Barre.Controls.Add(button);
                       button.AutoSize = true;
                       //     button.Tag = proc.Id;
                       PIC_Barre.Controls.Add(button);


                   foreach (Process proc in Process.GetProcessesByName("WINWORD"))
                   {
                       if (proc.ProcessName.Contains("WINWORD"))
                       {
                           proc.WaitForInputIdle();
                           {
                               Thread.Sleep(500);
                               SetWindowPos(proc.MainWindowHandle.ToInt32(),
                                   (int)SetWinPos_ZOrderOpt.HWND_TOPMOST,
                                   0, 0, 0, 0,
                                   (int)(SetWinPosFlags.SWP_NOSIZE |
                                         SetWinPosFlags.SWP_NOMOVE));
                           }
                           button.Click += (s, e) => { ShowWindowAsync(proc.MainWindowHandle, (int)ShowWindowCommands.Normal); };
                           proc.Exited += (s, e) =>
                           {
                               var method = (Action)(() => PIC_Barre.Controls.Remove(button));
                               // button.Visible = false;
                               if (button.InvokeRequired)
                               {
                                   button.Invoke(method);
                               }


                           };

                       }

                   }
               }


What I have tried:

My problem is how to check when a window of a MS Word is closed so that the button will be removed.
Posted
Updated 19-Sep-16 5:56am
Comments
[no name] 14-Sep-16 7:00am    
http://www.codeproject.com/Questions/1124792/Csharp-how-to-check-if-an-exe-is-open-or-closed

VisualBasic knows GetObject[^] method, which returns COM object or nothing (null).

[EDIT]
There's no C# equivalent method, unless you add a reference to Microsoft.VisualBasic (not recommended).


There is a C# equivalent to GetObject - Marshal.GetActiveObject[^].

Thank you, Richard for that valuable comment.

[/EDIT]

Please, follow the instruction in below article to resolve your issue:
A C# alternative for the Visual Basic GetObject function[^]

Note: didn't tested by myself!
 
Share this answer
 
v3
Comments
TatsuSheva 14-Sep-16 12:23pm    
look at this... Do you think this will resolve my problem ? http://www.codeproject.com/Questions/186322/How-to-get-running-insatnce-of-MS-word-using-C
Maciej Los 14-Sep-16 12:25pm    
Looks like it might be helpful.
Maciej Los 14-Sep-16 12:33pm    
If my answer was helpful, please accept it (green button) to remove your question from unanswered list.
TatsuSheva 14-Sep-16 12:36pm    
How could I associate the button and the object together so that when I close my app the button will be removed ?
Maciej Los 14-Sep-16 12:45pm    
It depends on many factors. If you call an instance of MS Word application, you have full control over that application. If not, you have to check (in some period of time, probably by using BackgroundWorker) if instance of MS Word application is open and keep a button visible till the application is opened.
this will end any MS Word, maybe this is a start :

C#
using System.Diagnostics;

...
C#
Process[] word_apps= Process.GetProcessesByName("winword");
foreach(var word_app in word_apps){
 word_app.Kill();
}
 
Share this answer
 
Comments
TatsuSheva 14-Sep-16 9:05am    
No, It is not what I need. When the window of the MS Word is closed, the button must be removed.
[no name] 19-Sep-16 9:52am    
use a timer
and see when word_apps count is zero to remove button
TatsuSheva 19-Sep-16 10:52am    
for each app there is a button are you sure that it will work ?
[no name] 20-Sep-16 4:34am    
Just try it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900