简体   繁体   English

关闭程序后如何保持进程运行?

[英]How to keep process running after closing the program?

I am now developing an application on Windows Mobile 6.5 with .Net Compact Framework 3.5 using C#. 我现在正在使用C#在带有.Net Compact Framework 3.5的Windows Mobile 6.5上开发应用程序。 There is a function in the program that I use it to update the location information periodically from server side, but if I keep running this computation, it would cost too much energe. 程序中有一个功能,我可以用它来定期从服务器端更新位置信息,但是如果我继续运行此计算,则会花费太多精力。 For this reason, I want to run it in background and I try to use BackgroundWorker to do this and it works well. 由于这个原因,我想在后台运行它,并尝试使用BackgroundWorker做到这一点,并且效果很好。

The problem I have now is that I can't minimize the program so that I have to keep the main form of the program run in foreground even if it's not doing anything and this is very inconvinence for a user. 我现在遇到的问题是我无法最小化程序,因此即使程序没有执行任何操作,也必须使程序的主要形式保持在前台运行,这对用户来说非常不便。 However, when I close the program, the BackgroundWorker will also be closed. 但是,当我关闭程序时,BackgroundWorker也将关闭。

Is there any method to keep the update process running (somewhere in memory or so) when I close the program? 关闭程序时,是否有任何方法可以使更新过程(在内存中的某个位置)保持运行? and then can restore the information when I restart the program? 然后可以在重新启动程序时还原信息?

如何创建服务而不是后台工作者?

If your Form closes, then Application.Run (probably called over in Program.Main) returns and the process' primary thread exits, causing the application to terminate. 如果您的窗体关闭,则Application.Run(可能在Program.Main中调用)返回,并且进程的主线程退出,导致应用程序终止。

The solution, then, is don't close the Form, simply Hide it. 因此,解决方案是不关闭表单,只需隐藏它即可。 By default the "MinimizeBox" property for your Form should have been true and it should have an [X] in the upper right corner. 默认情况下,表单的“ MinimizeBox”属性应为true,并且其右上角应具有[X]。 Clicking this minimizes the Form and will not exit your application. 单击此按钮将最小化表单,并且不会退出您的应用程序。

The other option in some cases is to not have a Form at all. 在某些情况下,另一个选择是根本没有表格。 The challenge here is that the CF doesn't have any Application.Run overload that doesn't accept in a Form (like the desktop framework does). 这里的挑战是CF没有任何Application.Run重载,而该重载在Form中是不可接受的(就像桌面框架一样)。 The Smart Device Framework does provide one if you want to go that route. 如果您想走那条路线,智能设备框架会提供一个。

I have not used the .NETCF 3.5. 我没有使用过.NETCF 3.5。 However in the previous version on .NETCF 1.0/2.0 I observed that even if you close the application using (X) button, it just goes to background but remain in the memory. 但是,在.NETCF 1.0 / 2.0的先前版本中,我观察到,即使使用(X)按钮关闭应用程序,它也只会进入后台,但仍保留在内存中。

If that is the case with .NETCF 3.5 as well then I think you do not need to anything here. 如果.NETCF 3.5也是如此,那么我认为您在这里不需要任何操作。 The background worked will be running even if you close the application. 即使关闭了应用程序,后台工作仍将运行。

I Hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM