Avatar of Kinger247
Kinger247
 asked on

How to crash a .net application ?

Ok, I'm that good a coder I cant get my application to crash ... :)
But for the sake of testing, I need to mimic a complete crash of my application (ie it just shuts down).
I could do a alt-f4, but I want to really have something in code to hit and crash.

The reason why I want to do this is because I have the app connected to a vb.net server via tcp and when the client app crashes, the server doesnt recognise its gone.
But only when it crashes.

Any ideas ?
cheers K.
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
abel

8/22/2022 - Mon
abel

What about creating a button with an illegal operation under it, i.e., division by zero? You have to disguise it a bit, otherwise it won't compile...
abel

This works for me:


private void Q24183289_Click(object sender, EventArgs e)
{
    int i = 0;
    int j = 0;
    int result = i / j;   // raises DivideByZeroException unhandled
}

Open in new window

abel

Maybe that's not "crashy" enough? I just found out that the .NET runtime presents me with a very nice error message, allowing me to continue...

ScreenShot081.png
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Kinger247

ASKER
yes, I need it to crash, no error just a complete vanish ...
daveamour

So throwing an exception is no good eg:

throw new Exception("Blah");
When an exception is raised in your app then it will be handled by and try catch blocks in your code and if it remains unhandled then it will be handled by the .net runtime hence the error message you posted an image of.
It sounds like you are wanting the app to crash and the .net runtime to crash too!
 
appari

try to call a procedure recursively without any exit pooint, it should throw stack overflow error. not sure if it displays same continue/quit window or it crashes. right now i dont have a system with VS installed.

try something like this

private sub callMeAgain()
           callMeAgain()
           callMeAgain()
end sub


and from button click event call callMeAgain() sub
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
daveamour

Better try it on your pc and not mine!
Only need callMeAgain once though, the second one wil never be reached :)
Mikael Jansson

I dont know if this will help you but you could download "Debugging tools for Windows" and run a debugger on the application, then hit CTRL-C to abort the debugger (this will crash the application), but Im not sure what you want to achieve with the crash since I thing you will get an error related to the Attached debugger or Application shutdown or something like that.
But if you want to try just download and install Debugging tools for windows that you will find here

 
Then you start your application and open a command shell, goto the folder for "Debugging tools for windows" and run this
 
cscript adplus.vbs -crash -pn [application name]

Then you will be presented with a debugging window (minimized), select that window and press CTRL-C (maybe twice), this will dump the memory for your application and quit the application. but as I said before this might not be what you wanted as it might present you with a misleading crash cause. But at least this is working for a windows sevice where service control manager log this as "terminated unexpectedly"
/ Mikael
Mikael Jansson

Sorry forgot the link to the debugging tools for windows
http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx#a
/ Mikael
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
abel

This will do what you want, the trick is to override the default exception handler and then to raise an exception. I deducted this from info on http://www.codeproject.com/KB/dotnet/unhandledexceptions.aspx and http://msdn.microsoft.com/en-us/magazine/cc188720.aspx, the latter being a quite good read.

The screenshot I applied can be different depending on your windows version.

private void Q24183289_Click(object sender, EventArgs e)
{
    //AppDomain.CurrentDomain.UnhandledException += // CLR
    //    new UnhandledExceptionEventHandler(OnUnhandledException);
    Application.ThreadException += // Windows Forms
         new System.Threading.ThreadExceptionEventHandler(
             OnGuiUnhandedException);
 
 
    int i = 0;
    int j = 0;
    int result = i / j;  // throws exception
}
 
// Windows Forms unhandled exception
public static void OnGuiUnhandedException(Object sender,
   System.Threading.ThreadExceptionEventArgs e)
{
    throw new InvalidOperationException(
        "Invalid operation.");
 
}

Open in new window

ScreenShot082.png
abel

> then it will be handled by the .net runtime hence the error message you posted an image of.

I was the one showing that image ;)
Using the code above, the .NET runtime will not be the one that takes over the error handling, because you just overrid it with the line

   Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);

You can use the other (commented) line if it is a console application.
appari

>>Only need callMeAgain once though, the second one wil never be reached :)
yeah, while doing copy paste did it twice:)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kinger247

ASKER
do you know hwo to this part in vb.net  ?

Application.ThreadException += // Windows Forms
         new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);

cant get the Application.ThreadException part working.
Kinger247

ASKER
Seems to be what I need though :)
abel

> do you know hwo to this part in vb.net  ?

I can give it a try. Hold on.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
abel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kinger247

ASKER
cheers for your help.
abel

You're welcome :)