469,578 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Ending another application gracefully

!NoItAll
296 100+
I have a program that needs to access the files that are open by another program on the computer.
When my application comes up I can easily see if the other application is running with the .net Process.GetProcessesByName method

Expand|Select|Wrap|Line Numbers
  1. Dim MyProcesses() as Process = GetProcessesByName("AnApplication")
  2.  
  3. For Each  Instance as Process in MyProcesses
  4.  
  5.      If MsgBox("application is running - do you want to end it?", MsgBoxStyle.YesNo or MsgBoxStyle.Question, "Warning") = MsgBoxResult.Yes then
  6.          Instance.Kill()
  7.          Do Until Process.GetProcessByID(Instance.Id).HasExited = True
  8.              My Application.DoEvents()
  9.          Loop
  10.      End if
  11.  
  12. Next
  13.  
The problem is that the Instance.Kill does not allow the application to end gracefully. In VB6 you could use the SendMessage API to send a close message to the application and it would end gracefully - i.e. give the user the normal closing prompt. This just slays the application - leaving any open files in their potentially incomplete state, etc.
Does anyone know of a way (other than reverting to unmanaged code) to do this properly? I'm disapointed .NET does it the way it does - it's not kosher.

Des
Sep 23 '09 #1
4 2783
tlhintoq
3,525 Expert 2GB
I still use SendMessage quite often for stuff like this.
Should still work for your need

Expand|Select|Wrap|Line Numbers
  1.         private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
  2.         private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
  3.         private const int APPCOMMAND_VOLUME_UP = 0xA0000;
  4.         private const int WM_APPCOMMAND = 0x319;
  5.  
  6.  
  7.         [DllImport("user32.dll")]
  8.         public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  9.  
  10.         public static void VolumeUp()
  11.         {
  12.             System.Windows.Forms.Form Dummy = new Form();
  13.             SendMessageW(Dummy.Handle, WM_APPCOMMAND, Dummy.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
  14.         }
  15.  
  16.  
Sep 23 '09 #2
!NoItAll
296 100+
Yeah - I can do that, but so far my program has not had to revert to unmanaged code ... yet.
I don't think what I'm doing is that out-of-the-ordinary. It's a shame that .net, at 3.5, doesn't offer it.
Sep 24 '09 #3
Plater
7,872 Expert 4TB
Well killing a Process is just that, killing it (tell the kernal to stop working it and free up all its resources). All other forms of closing are just special messages sent to it on the message pump. Makes sense that they would use different objects.
That said, I don't know why they didn't put the SendMessage stuff into .NET directly.
Sep 24 '09 #4
!NoItAll
296 100+
OK - I know the answer now - and I simply can't believe I missed this. It was staring me in the face all along!
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. Dim MyProcesses() as Process = GetProcessesByName("AnApplication") 
  2.  
  3. For Each  Instance as Process in MyProcesses 
  4.  
  5.      If MsgBox("application is running - do you want to end it?", MsgBoxStyle.YesNo or MsgBoxStyle.Question, "Warning") = MsgBoxResult.Yes then 
  6.          Instance.CloseMainWindow()
  7.          Do Until Process.GetProcessByID(Instance.Id).HasExited = True 
  8.              My Application.DoEvents() 
  9.              'we probably want to set a timeout in here...
  10.          Loop 
  11.      End if 
  12.  
  13. Next 
  14.  
  15.  
So the missing element was the .CloseMainWindow() method that, for some very strange reason, I just never saw in the intellisense. Entirely my fault - as I'm sure it was there all along!

Crap - I've earned an Idiot Flag!
Sep 24 '09 #5

Post your reply

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

Similar topics

4 posts views Thread by Greg Smith | last post: by
27 posts views Thread by cj | last post: by
8 posts views Thread by RobcPettit | last post: by
10 posts views Thread by Jon Slaughter | last post: by
15 posts views Thread by (PeteCresswell) | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.