Terminate app after 2 second delay | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Terminate app after 2 second delay

Hi everybody, anybody knows how can i terminate app after 2 second in c#?(which method shall i use ?) for example if user entered wrong number , the program's going to be terminated after 2second.

22nd Jul 2017, 9:55 PM
Amirali nedaee
Amirali nedaee - avatar
5 Answers
+ 4
Try this: Console.WriteLine("Hello, world!"); System.Threading.Thread.Sleep(2000); Environment.Exit(0); Note that this may not work on CodePlayground. Pretty sure CodePlayground won't show these delays.
22nd Jul 2017, 10:47 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
hm. Do you want the program to be responsive during the delay? If not, you can use Thread.sleep(2000); That will basically pause the program for 2000 milliseconds (2 seconds). So you could do some pop up like "closing the app". Then pause it for 2secs then quit. Then, you can use something like Application.exit(); or Environment.exit. https://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application
22nd Jul 2017, 10:23 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
this works fine, i tried it, thanks
22nd Jul 2017, 10:49 PM
Amirali nedaee
Amirali nedaee - avatar
0
i want the program to run like this: Console.WriteLine("Sorry you entered wrong numbers more than twice") and then want to terminate app after 2 second because i wanna give time to user to read the message before the program goes shut down!
22nd Jul 2017, 10:41 PM
Amirali nedaee
Amirali nedaee - avatar
0
i tried this one; Task.Delay(2000); Enviroment.Exit(0); but it didn't work
22nd Jul 2017, 10:42 PM
Amirali nedaee
Amirali nedaee - avatar