This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Detecting process crash in .NET
I'm writing a c# program that have to determine if another C++ game program (let's call it Foobar) is crashed or not.
When the FooBar program crashes it's notifying the user about the crash with a MessageBox, if you OK that windows the program closes.
So I guess I could determine if the program is crashed if that messagebox is opened/active. Problem I dont know how to do that.
Or if there is any other better solution comes to your mind, please share it with me.
Edit:
I can not edit the C++ program, and it's always a possibility that it will crash. I just need to know if it did.
Duplicate of Detecting process crash in .NET.
The heartbeat is probably the way to go, but there is another way.
When a process crashes, Windows first checks to see if a Just-In-Time debugger is configured on your system. If so, you can have that debugger attach itself to the process right before it crashes. Usually you would use this functionality to generate a memory dump as a process crashes. Whatever debugger you attach gets to know the PID and name of the crashing process. You can either utilize features of existing debugging tools, such as ADPlus, or write your own program and tell Windows that that is your Just-In-Time debugger and should be run when a process crashes. I believe you can set up a JIT debugger specifically for any process name.
See http://msdn.microsoft.com/en-us/library/5hs4b7a6(v=VS.80).aspx
I think that if you set the HKLM\Software\Microsoft\Windows NT\Current Version\AeDebug\Debugger registry entry to '"DirectoryOfYourProgram\YourProgram.exe" -p %ld' where YourProgram.exe expects a PID passed in with the -p flag, your program will be called and passed the correct PID when a process crashes.
If you have access to the code for both apps, you could setup a heartbeat message from the c++ app to the c# app, and do whatever when the heartbeats stop. For the comms you would need something like named pipes or similar. Alternatively your c++ could write to a file regularly, which your c# could detect updates to, using file monitoring.
Related
I'm new to c#. Antivirus deleted the mySolutionName.exe file out of the /debug directory and now I cannot execute my code. I'm concerned that anything I do may make the issue worse.
When I press F5 I get an error of:
CS2012 C# Cannot open for writing --> C:\Users\me\source\repos\MyApp\MyApp\obj\Debug\MyApp.exe''
Can someone please tell me how to rebuild the file so that I can continue developing?
after your edit:
Your program is probably still running outside of the debugger.
You need to use the task-manageer to kill all instances of MyApp.exe if this doesnt solve your issue a reboot should do the trick as well
So why is this happening?
Most liky your application is somewhere stuck on a blocking function or in a never ending loop. maybe there is even a seperate thread still operating that you forgot to close. We lack some information to tell yout that for sure. But to find out what is happening you can close your window while running in debugger mode and see if the application properly closes. if it doesnt you can hit pause and see where the program is stuck and resolve this issue by ending the task/loop/whatever in your OnClosing function of your window.
A good way to solve this issue is looking which process currently accesses the file.
A tool to do that is Microsoft SysInternals Process Explorer. It has a feature called "Find handle or DLL ..." which can be accessed by Ctrl+F.
The result will show the process which accesses the file. You can then judge whether it's Antivirus or something else that prevents you from writing to the file. If possible, you can then take an action in that program to release the file.
Example: a program is accessing my powerpoint presentation, which has the term "Schulungen" in its file name.
Process Explorer figures out: it's open in Powerpoint.exe, so I can simply close the file in Powerpoint - problem fixed.
I have a program developed by C# and build by .net 4.0.
This program is a windows client which would read the barcode from a barcode reader (via com port) then send the barcode to back-end server via WCF.
The customer have a script in the client OS which would reboot the OS and automatically start up my program every day. The OS is Windows XP Embedded.
Now the problem is, sometimes when the system reboot, my program cannot be started and an error message box will popup to ask whether send this error report to Microsoft.
The most strange thing is, if my colleague copy the program folder and paste as "Copy of ...." in the same folder with the original one the exe under "Copy of ..." one can run without any problem. But the original one still cannot.
What my speculation is maybe the program was writing log and other record files while the system was forced to reboot. And the files get the read/write lock unreleased.
I have uploaded the error screen shots to flickr. Please click here link to visit.
Without knowing what the actual exception is, we can only guess.
You will need to catch the exception that is being thrown in your application.
The best practice is to encapsulate your code in try/catch clauses.
If you are still getting application crashes, then you can attach an event handler to AppDomain.UnhandledException, or Application.UnhandledException and log the exception(s) being received.
Make sure to output the entire exception stack trace, so you can see where it's being thrown from.
Once you've got the exception, if you can't figure out the cause, then ask another question here with that specific detail. eg: "I'm getting an FooException being thrown when I call Bar() after start-up on Windows XP Embedded"
Sometimes after a reboot, some device drivers, or some hardware, will NOT reset itself. The machine has to be power cycled (turned off and back on) or a command needs to be discovered that will force the device driver and/or hardware to reset.
Referring to image IMG_1348 you posted, the error is thrown in your form constructor.
Seems like either code you added or InitializeComponent code is throwing.
Since you are using XPe, you have some options to debug this issue:
Add message box statements around the various constructors to show initialization progress. Guard before and after.
public Form1()
{
MessageBox.Show("Before InitializeComponent");
InitializeComponent();
MessageBox.Show("After InitializeComponent");
//MessageBox.Show("Before Other");
//Other Initialization Code
//MessageBox.Show("After Other");
}
Attempt to use the remote debugger. I am not sure if this works on XPe, but if it does, and since your code is throwing in the constructor, you need to add code to wait until the debugger is connected.
public Form1()
{
while (!System.Diagnostics.Debugger.IsAttached){ System.Threading.Thread.Sleep(0); }
InitializeComponent();
//Other Initialization Code
}
I have 2 programs. I want to build an external tool, which prevents one of the programs from starting, when the other program is already running, and vice versa.
I can't touch the code of either of the two programs.
I want to do it preferably with C#, but a scripting language will also be ok.
Can anyone help me with the concept how to implement it?
Thanks in advance.
Write your tool as a windows service that keeps running in background and kills the second program if it starts. It can find out when a new process starts by listening to WMI events see .NET Events for Process executable start
There are couple of ways to do that. If you are using C# you should be getting the process name using the GetProcessesByName(). It would look something like this
Process[] processes = Process.GetProcessesByName(processName);
If you know the process name you can just kill the process
Or else use the [Semaphore Class] (http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx).
I have a bit of a strange scenario that I can't figure out. I have some c# code that will start another process using 'process.start()' The process in question is a custom tool I have written so I have full access to the code. Is there a way that I can use breakpoints in this 'custom tool' process? I know how to do it with 'attach debugger', but that is a manual approach as opposed to an automatic one. Is this sort of thing possible?
EDIT:
I suppose I could just launch the remote debugger process instead of my 'custom tool' ?
What exactly do you mean when you say "use breakpoints"? Do you want to attach debugger automatically on process startup?
Have you looked at ImageFileExecutionOptions registry key? It allows you to set a debugger that can be automatically attached to a proces when it starts up.
When I do throw exception I have added the exit, but after that the exe file is removed. is it a known problem? (in VS2010)
Neither Application.Exit nor Environment.Exit will remove the executable from disk.
It sounds like some other process is trying to delete the executable. As soon as the application exits, the file will no longer be in use, and the .exe could be removed.
Depending on what you're doing, this could, potentially, be from a virus scanner. I would check your Virus Scanner logs to see if there is an issue there. This should be very unlikely if this is a completely managed (C# only) application - but if you're using native code that is "questionable" according to the scanner, it may be removing the executable.
Otherwise, I would check for other processes that may be trying to remove the executable.
No this doesnt sound right. Do you mean the .Exe Application file is been removed from Disk? That cannot/Should not happen on Exiting. Can you send mroe details on what you are trying to do, What kind of application, etc. I tried with a simple C# Console application and it wouldnt seem to happen in VS 2010.