Application.Exit() vs Application.ExitThread() vs Environment.Exit()

前端 未结 2 1552
-上瘾入骨i
-上瘾入骨i 2020-12-09 02:18

I am trying to figure out which I should be using. On closing my WinForm app fires of a Form in Dialog mode. That form runs a Background worker that Syncs the DB with the

2条回答
  •  情话喂你
    2020-12-09 02:41

    Environment.Exit() is used for console apps.

    You want to use: System.Windows.Forms.Application.Exit()

    By exiting thread, you are only exiting the current thread context, while leaving any started foreground threads running. I suspect the thread that is causing the error is still running, so you've essentially masked the problem, not worked around it. I would try and figure out why you are getting this error "Collection was modified; enumeration operation may not execute." on exit. It's being exposed by Application.Exit(), but it's not caused by it.

提交回复
热议问题