What happens when app exits and BG Thread is working on db  
Author Message
Tryst





PostPosted: Visual C# General, What happens when app exits and BG Thread is working on db Top

Hi all,

what would happen if my application exits and there is still a background thread running that is working (doing 100's of INSERTS and UPDATES) on the database from which the connection to the database was established in the application (Which is now exited). Would the queries fail to execute from the point the application exits



Visual C#9  
 
 
ahmedilyas





PostPosted: Visual C# General, What happens when app exits and BG Thread is working on db Top

it may well do. have you tried it sometimes it depends as the app will appear to exit but if the thread is still alive and kicking it will not exit fully (you will see it in task manager) until the processing has been complete.

have you tried to see what happens :-)

I guess it can "swing" either way. If the app exits, it will pretty much signal a thread abort exception to all running threads causing the threads to...abort there and then.



 
 
boban.s





PostPosted: Visual C# General, What happens when app exits and BG Thread is working on db Top

By default it will terminate immediatly and application will be closed. That's why you have BG and FG Threads. BG will terminate immediatly and app will be closed. FG will keep the app alive until the last FG is active except the main app thread. You should be carefull especially when working with databases and BG threads. You should explicitly wait for BG to finish using some timeout, calling Thread.Join, and if it still not finished then try to abort, and at the and abandon it. If BG is terminated just like that, finally blok will be skipped, and this is especially important when working with database. Maybe if you are doing some important operation like Rollback or Commit will not be executed.

About your question, it depends. If you work with transactions and want to insert and update all or none of them, then none of them will be commited if BG thread is stopped in the middle. If you don't work in one transaction, the one's that have passed will be in the db, the rest will not be.