4

I've made a Windows console app in c# that makes some calculations. App window is invisible for user, app runs as a task.

Those calculations are saved to database. Unfortunatey, if user close this app during saving data (by shutting down computer) and not all data will be saved, my calculations won't make sense.

So I'm looking for some way to prevent app from closing immadiately. Are thre any ways to do that?

Piotrek
  • 10,919
  • 18
  • 73
  • 136
  • 2
    Actually, you should remember that the user always has the right to decide when to close your program. Maybe, you should add a "fail field" somewhere at start of the calculations, and delete it at the end, so, when you check, if it's set, you can presume that the data is corrupted. – Desaroll Dec 22 '15 at 17:51
  • 2
    i think you cant prevent user from shutting down computer. (maybe you can but its not a good idea imo). maybe if you save the progress in disk so every time you know where you are. – M.kazem Akhgary Dec 22 '15 at 17:51
  • 2
    You CAN prevent the application from shutting down, but you should give the user the choice. If they try to close it and it is still running, prompt them to confirm that they really want to quit. If they still want to quit, use @Desaroll's suggestion and the task will never be marked as completed. – gmiley Dec 22 '15 at 17:54
  • 1
    If you're saving data to a database, and need to wipe data from unfinished calculations, that's exactly the scenario for database transactions. – Fede Dec 22 '15 at 17:54
  • Ludwik, some things that you need to clear before you can get a good answer: 1) Why did you choose a Console application and not a Windows application for example? 2) If it was a Windows one, you could first display a MsgBox asking the user "Calculation is in progress, are you sure you want to quit in the middle? (Y/N)" - this would already help. Check the `FormClosing event` for example 3) Lastly, can't you make it that if the user indeed chooses Yes, then what has been calculated will be saved, and next time the application is run, the calculation will resume from where it stopped? – spaceman Dec 22 '15 at 18:05
  • @spaceman I've chosen console app because I don't need any UI, so I thought that console app is the best option here – Piotrek Dec 22 '15 at 19:02
  • Maybe you could try hooking into console exit events: https://stackoverflow.com/questions/4646827/on-exit-for-a-console-application – andrew pate Jul 27 '19 at 11:19

1 Answers1

9

I believe you're trying to solve the wrong problem here.

At most, you can make a thread keep the application alive, by setting its System.Threading.Thread.IsBackground property to false. But this is far from reliable, as the user will always be able to kill your process in ways that are out of your control (sending a kill signal to your process, pulling the power plug, etc).

Instead, notice that you're saying that you're doing some calculations and saving them in a database and the calculations "won't make sense" if saving them is interrupted (as I understand it, you mean that the integrity of the database will be compromised). This sounds exactly like a use case for database transactions.

The idea is that you submit your calculations as a single transaction and let the database management system ensure the atomicity, consistency, isolation and durability of each unit of work and preserve the integrity of your database. The way to do this depends on your DBMS, but the core idea is the same.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104