You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UWP C#控制台应用调用BackgroundDownloader下载时挂起求助

UWP控制台应用中BackgroundDownloader下载挂起问题

问题场景

我有一个UWP WRC库需要通过命令行应用测试,但启动下载时出现挂起现象。参照Pieter Nijs的《用C#创建UWP控制台应用》教程编写测试应用,复现了使用BackgroundDownloader启动下载时的挂起问题——设置超时则会超时而非挂起。使用Visual Studio 2022在本地Windows 10机器上调试。

测试代码如下:

using System;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Networking.BackgroundTransfer;
using Windows.Foundation;

namespace TestRunnerRt
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                Uri source = new Uri("https://some url"); // insert valid url
                string destination = "downloaded.htm";

                StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(destination, CreationCollisionOption.ReplaceExisting);

                BackgroundDownloader downloader = new BackgroundDownloader();
                DownloadOperation download = downloader.CreateDownload(source, destinationFile);

                Console.WriteLine($"Starting download from {source}");
                var task = download.StartAsync().AsTask();

                // *** The next line hangs ***
                task.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Download Error: ", ex);
            }

            // wait for user to press a key before exiting (and closing the console)
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

尝试过的操作

尝试了多种启动下载的方式,包括用Task.Run()包裹,以及直接await DownloadOperation::StartAsync()调用:

DownloadOperation r = null;
var task = Task.Run(async () => r = await download.StartAsync());

无论哪种方式,应用都会挂起(或超时),生成的downloaded.htm文件始终为0字节。

权限配置

已启用「Internet (Client & Server)」「Internet (Client)」和「Private Networks (Client & Server)」权限,清单文件内容如下:

<?xml version="1.0" encoding="utf-8"?>

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"
  xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
  xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
  xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
  IgnorableNamespaces="uap mp iot uap4 rescap uap3">

  <Identity
    Name="TestRunnerRt"
    Publisher="CN=py122c"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="7c65166f-96f5-4385-b3e7-799a3040b21d" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
    <DisplayName>TestRunnerRt</DisplayName>
    <PublisherDisplayName>py122c</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="TestRunnerRt.App"
      desktop4:Subsystem="console"
      desktop4:SupportsMultipleInstances="true"> <!-- this app will run in the console (and can run in multiple instances) -->
      <uap:VisualElements
        DisplayName="TestRunnerRt"
        Square150x150Logo="Assets\Square150x150Logo.png"
        Square44x44Logo="Assets\Square44x44Logo.png"
        Description="TestRunnerRt"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>
        <!-- Allow app to start from the console -->
        <Extensions>
            <uap5:Extension
                Category="windows.appExecutionAlias"
                Executable="$targetnametoken$.exe"
                EntryPoint="$targetnametoken$.Program">
                <uap5:AppExecutionAlias desktop4:Subsystem="console">
                    <uap5:ExecutionAlias Alias="$targetnametoken$.exe" />
                </uap5:AppExecutionAlias>
            </uap5:Extension>
        </Extensions>
    </Application>
  </Applications>
    
  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="broadFileSystemAccess" />
    <Capability Name="internetClientServer"/>
    <Capability Name="privateNetworkClientServer"/>
    <uap:Capability Name="sharedUserCertificates"/>
    <uap4:Capability Name="userDataTasks"/>
    <uap3:Capability Name="remoteSystem"/>
    <DeviceCapability Name="location"/>
  </Capabilities>
</Package>

内容的提问来源于stack exchange,提问作者Jeff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:35:22