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 -
Dim MyProcesses() as Process = GetProcessesByName("AnApplication")
-
-
For Each Instance as Process in MyProcesses
-
-
If MsgBox("application is running - do you want to end it?", MsgBoxStyle.YesNo or MsgBoxStyle.Question, "Warning") = MsgBoxResult.Yes then
-
Instance.Kill()
-
Do Until Process.GetProcessByID(Instance.Id).HasExited = True
-
My Application.DoEvents()
-
Loop
-
End if
-
-
Next
-
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
4 2962 tlhintoq 3,525
Recognized Expert Specialist
I still use SendMessage quite often for stuff like this.
Should still work for your need -
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
-
private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
-
private const int APPCOMMAND_VOLUME_UP = 0xA0000;
-
private const int WM_APPCOMMAND = 0x319;
-
-
-
[DllImport("user32.dll")]
-
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
-
-
public static void VolumeUp()
-
{
-
System.Windows.Forms.Form Dummy = new Form();
-
SendMessageW(Dummy.Handle, WM_APPCOMMAND, Dummy.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
-
}
-
-
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.
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.
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: -
Dim MyProcesses() as Process = GetProcessesByName("AnApplication")
-
-
For Each Instance as Process in MyProcesses
-
-
If MsgBox("application is running - do you want to end it?", MsgBoxStyle.YesNo or MsgBoxStyle.Question, "Warning") = MsgBoxResult.Yes then
-
Instance.CloseMainWindow()
-
Do Until Process.GetProcessByID(Instance.Id).HasExited = True
-
My Application.DoEvents()
-
'we probably want to set a timeout in here...
-
Loop
-
End if
-
-
Next
-
-
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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...
|
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.
|
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
| |
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
|
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();
|
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)
|
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.
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |