Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent app from closing before finishing a task?

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?

like image 387
Piotrek Avatar asked Feb 09 '23 05:02

Piotrek


1 Answers

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.

like image 85
Theodoros Chatzigiannakis Avatar answered Feb 15 '23 11:02

Theodoros Chatzigiannakis