简体   繁体   English

如何线程/哪个线程用于后台DLing .NET

[英]How to thread/which thread to use for background DLing .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. 用户应该能够关闭不会启动任何更多DL的应用程序,并等待当前完成,并强制关闭选项,这将断开当前下载并退出。

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. IIRC在.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? 我可以使用哪些命令告诉他们现在关闭(这可能会有所不同,然后终止),以防用户请求立即关闭?

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. 在给出的场景中,您应该能够使用HttpWebRequest / WebClient类的异步性质,它将使用完成端口而不仅仅是线程来支持所有这些。

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: 但是工具本身可能提供替代品:

    WebClient wc = new WebClient();
    wc.DownloadFileCompleted += (sender, args) =>
    {
        if (args.Cancelled) Console.WriteLine("cancelled");
        else if (args.Error != null) Console.WriteLine(args.Error.Message);
        else Console.WriteLine("got it");
    };
    wc.DownloadFileAsync(uri, filePath);
    // wc.CancelAsync(); // to abort

Note that in an async callback (the DownloadFileCompleted above) you would, in anything like winforms/wpf, have to switch to the UI thread; 请注意,在异步回调(上面的DownloadFileCompleted )中,您将在winforms / wpf之类的任何内容中切换到UI线程; for example: 例如:

    wc.DownloadFileCompleted += (sender, args) =>
    {
        this.Invoke((MethodInvoker) delegate {
            if (args.Cancelled) txtStatus.Text = "cancelled";
            else if (args.Error != null) txtStatus.Text = args.Error.Message;
            else txtStatus.Text = "got it";
        });
    };

There is only 1 kind of Thread, but a variety of ways to use them. 只有一种Thread,但有多种方法可以使用它们。 Since you are working from a GUI (WinForms?) the BackgroundWorker is most suitable. 由于您使用的是GUI(WinForms?),因此BackgroundWorker最适合。

You could use async or sync I/O from there. 您可以从那里使用异步或同步I / O.

You should not Abort() a Thread, but Close or Cancel your connection. 你不应该中止()一个线程,但关闭或取消你的连接。 Depnds on wht you want to use. 发布你想要使用的内容。

您应该查看ThreadPool.QueueUserWorkItem

如果您使用的是.NET 2或更高版本,请查看BackgroundWorker组件。

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

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