开发者

Create separate thread on application exit

Is it possible to create a separate thread on application exit that will continue to run (after the application closes) until 开发者_开发问答the thread finishes its work?

I'm using C# and I have a case where I want to exit an application but at the same time kick off database interaction that will fire off stored procedures. My problem is that I can have a variable number of Stored Procs to execute, but I do not want to cause the application to hang or be slow to exit.


What does "application exit" mean? By definition, when the application really really exits, all its threads exit, too.

Maybe you want to start a new thread just when the UI is closed? So that the user thinks that the application is exited, because there is no visible UI, whereas the application still runs in the background? Yes, this is possible, but you need to tell us which UI framework are you using. (WinForms? WPF?)


I don't want an application to continue running invisibly when I close it. This is particularly annoying when I shut down and something hangs that process up, or if I close applications to free up resources.

To be polite to your users, you should have some sort of minimizable, taskbar-visible window upon exiting that informs them of what's going on, and what the status is. This doesn't need another thread - you can do it from your exit code.


  1. Does youe database support jobs? Then all you need to do is setup the database job when your application finished.

  2. If you need somekind of interaction from your program to your database, then you can consider making your application invisible when user closes the program and do the database calls that needs to be done.


Another answer again: As much as I understand it's Windows Form application, if you want to finish something before closing application, you can listen for your main form's OnClosing event.

By the way you can not just proceed, as just like @Vlad says any thread created will be destroyed as makes a part of an application which destroyed. So there is no other way then block your main thread and wait other thread and only after exit app (if you really need it, but at this point I don't think so) and finish.

Another sugession, to make "fill" to the user that application closed immediately you, may be can make form invisible and continue do your "dirty" stuff :) But this kind of tricks are very application dependent, it's hard to me just to give you right suggession on subject.

Regards.


As others have mentioned, when the program closes, all threads are terminated. You want to do as you mentioned in your comment on Vlad's answer. You have to be careful about where you do the work, though. If you do it in the Application.ApplicationExit event, your forms will all be disposed and you won't have access to any of the data/classes . You're going to want to place the code in your main form's FormClosing event. If you call this.Hide(), the form will disappear. Execute your DB work there. Once that method exits, the program will terminate as normal.


Regardless of the type of project, WinForm/WPF/etc, you will always be able to find a

static void Main(params object[] args) { ... }

Somewhere in your project. It might be Program.cs or Application.cs depending on which type of project. In this there will be the call that opens the UI of your application, which is a blocking call. Anything you put into that Main is going to be executed after your UI exits, has closed its windows, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜