如何防止应用在完成任务之前关闭? [英] How to prevent app from closing before finishing a task?

查看:47
本文介绍了如何防止应用在完成任务之前关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用c#制作了一个Windows控制台应用程序,该应用程序进行了一些计算.应用程序窗口对用户不可见,应用程序作为任务运行.

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?

推荐答案

我相信您正在尝试解决错误的问题.

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.

这个想法是,您将计算作为单个事务提交,并让数据库管理系统确保原子性,每个工作单元的一致性,隔离性和持久性,并保持数据库的完整性.这样做的方法取决于您的DBMS,但是核心思想是相同的.

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.

这篇关于如何防止应用在完成任务之前关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆