如何线程/使用哪个线程进行后台 DLing .NET
我正在编写连接到数据库并下载指定项目所需的任何包的前端应用程序。用户应该能够关闭应用程序,该应用程序将不再启动任何 DL,并等待当前完成,并且可以选择强制关闭,这将断开当前下载并退出。
我正在下载的文件可能位于不属于我自己的多个服务器上。我应该使用什么样的线程? IIRC .NET 上有不止一种类型。如果用户请求立即关闭,我可以使用哪些命令告诉他们立即关闭(这可能与终止不同)?
I am writing front end app that connects to a DB and downloads any package it needs for a specified project. The user should be able to close the app which will not start any more DLs and wait for the current to finish and an option to forcefully close which will disconnect the current download and quit.
The files i am downloading may be on several server that are not my own. What kind of threads should i use? IIRC there were more then one type on .NET. What commands can i use to tell them to close now (this may be different then terminate) in case the user request to shut down right away?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在给定的场景中,您应该能够使用诸如
HttpWebRequest
/WebClient
之类的异步特性,它们已经支持所有这些,使用完成端口而不仅仅是线程。重新“立即终止” - 除非您正在拆除进程,否则您不应该中止线程。这是一件坏事,并且可能使系统处于损坏状态,或者具有无法恢复的锁定。然而,工具本身可能提供替代方案:
请注意,在异步回调(上面的
DownloadFileCompleted
)中,在像 winforms/wpf 这样的东西中,您必须切换到 UI 线程;例如:In the scenario given, you should be able to use the async nature of things like
HttpWebRequest
/WebClient
, which will support all this already, using completion-ports rather than just threads.Re "kill now" - you should never abort a thread unless you are tearing down your process. This is a bad thing, and can leave the system in a corrupt state, or with irretrievable locks. However tool itself may offer alternatives:
Note that in an async callback (the
DownloadFileCompleted
above) you would, in anything like winforms/wpf, have to switch to the UI thread; for example:Thread只有1种,但是使用它们的方式有很多种。由于您使用 GUI(WinForms?)工作,BackgroundWorker 是最合适的。
您可以从那里使用异步或同步 I/O。
您不应该 Abort() 线程,而应该关闭或取消您的连接。取决于您想使用什么。
There is only 1 kind of Thread, but a variety of ways to use them. Since you are working from a GUI (WinForms?) the BackgroundWorker is most suitable.
You could use async or sync I/O from there.
You should not Abort() a Thread, but Close or Cancel your connection. Depnds on wht you want to use.
您可能应该查看 ThreadPool.QueueUserWorkItem
You should probably look into ThreadPool.QueueUserWorkItem
如果您使用的是 . NET 2 或更高版本。
Look at the BackgroundWorker componet if you are using .NET 2 or newer.