如何线程/使用哪个线程进行后台 DLing .NET

发布于 2024-08-09 15:34:13 字数 205 浏览 3 评论 0原文

我正在编写连接到数据库并下载指定项目所需的任何包的前端应用程序。用户应该能够关闭应用程序,该应用程序将不再启动任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放手` 2024-08-16 15:34:13

在给定的场景中,您应该能够使用诸如 HttpWebRequest / WebClient 之类的异步特性,它们已经支持所有这些,使用完成端口而不仅仅是线程。

重新“立即终止” - 除非您正在拆除进程,否则您不应该中止线程。这是一件坏事,并且可能使系统处于损坏状态,或者具有无法恢复的锁定。然而,工具本身可能提供替代方案:

    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

请注意,在异步回调(上面的 DownloadFileCompleted)中,在像 winforms/wpf 这样的东西中,您必须切换到 UI 线程;例如:

    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";
        });
    };

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:

    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; 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";
        });
    };
孤独患者 2024-08-16 15:34:13

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.

ゞ记忆︶ㄣ 2024-08-16 15:34:13

您可能应该查看 ThreadPool.QueueUserWorkItem

You should probably look into ThreadPool.QueueUserWorkItem

诗化ㄋ丶相逢 2024-08-16 15:34:13

如果您使用的是 . NET 2 或更高版本。

Look at the BackgroundWorker componet if you are using .NET 2 or newer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文