473,804 Members | 3,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ending another application gracefully

!NoItAll
297 Contributor
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.GetProc essesByName 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 2962
tlhintoq
3,525 Recognized Expert Specialist
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
297 Contributor
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 Recognized Expert Expert
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
297 Contributor
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 .CloseMainWindo w() 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

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

Similar topics

6
4127
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: Connect Receive response Send Connect ACK
2
1776
by: Gary | last post by:
Hi, there, I have an application running on remote machine, and would like to close it gracefully. Like a human go to there, push the stop button, and close it after several seconds. How to implement that in C#? Any suggestion? Thanks lot, Gary
3
1561
by: Boris | last post by:
I notice the following strange behavior of the applications running under ..NET Framework. Let say I have application MyApp.exe which reference assembly MyAssembly.dll. The MyAssembly.dll is strong named assembly, which is installed in the GAC. I notice that if MyAssembly.dll is not installed in GAC or the different version of MyAssembly.dll is installed in GAC then MyApp.exe starts OK. But as soon as the MyApp.exe app tries to call...
4
1873
by: Greg Smith | last post by:
I am working on my first web app. What is the proper way to end the application and close the aspx page? Any help is greatly appreciated.
10
2634
by: dan | last post by:
1. I have created a program P1 which is going to be triggered by a Windows Service. What command should be added in the Sub OnStop to force the process P1 to end when the service is stopped? 2. I am using the method Shell to start a program P2 from a program P3. What method can be used to end the program P2? Many thanks, Dan
27
2085
by: cj | last post by:
I run this program and to exit click the X in the upper right corner. But apparently it isn't really ending the program. If I return to VB and make changes then try to rebuild the app it says the exe is still in use--I find it is still a process in Task Manager. What do I need to do to make clicking that X actually end the program? Public Class Form1 Inherits System.Windows.Forms.Form
8
1799
by: RobcPettit | last post by:
Hi, Im using public void threadcontrol() { Thread scan = new Thread (new ThreadStart(GethtmlStream)); scan.Start(); name = "Scanning"; } in a class, and calling it from button1 with BJresults sr = new BJresults();
10
2197
by: Jon Slaughter | last post by:
Since a thread doesn't have a Stop feature and I'm not supose to use abort, I'm wondering how I stop a thread? My problem is that I simply want to excute a function in the background and possibly "restart" it. What've been doing is essentially: if (SerialRowThread != null)
15
2633
by: (PeteCresswell) | last post by:
Some time within the next nine months, there's a pretty good chance I'll have the "opportunity" to port a bond trading system that I wrote - whose front end is currently MS Access - to .NET. Quotes bc I need this like I need another hole in the head. But in the words of one of my esteemed colleagues: "I don't sell programming; I sell happiness." And if this will make my clients happy, that's what floats my boat in the long run.
0
9579
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,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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
10077
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...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.