【发布时间】:2020-03-28 12:26:49
【问题描述】:
在我的程序中,我正在将资源文件写入用户选择的某个位置。为此,我正在使用 GetManifestResourceStream。一切正常。
接下来我想让我的书写操作不阻塞 UI。所以我使用异步等待更改了代码。 不幸的是 GetManifestRresourceStream 现在返回 null 。在不使用 async await 的情况下改回来一切正常。
我做错了什么以及如何解决它,以便我能够复制文件并且 UI 不会被阻止。
这就是它在没有 async/await 的情况下的工作方式(请不要被方法名称混淆)
private void InstallButton_Click(object sender, RoutedEventArgs e)
{
InstallProgrammAsyc();
}
private void InstallProgrammAsyc()
{
try
{
FinalMessage = "";
PreInstallationBlock.Visibility = Visibility.Collapsed;
DuringInstallationBlock.Visibility = Visibility.Visible;
CopyFileToDestinationAsync("MyNameSpace", InstallPath, "Resources", "some.exe");
PrepareProgrammForFinish();
}
catch (Exception ex)
{
DuringInstallationBlock.Visibility = Visibility.Collapsed;
AfterInstallationBlock.Visibility = Visibility.Visible;
FinalMessage = $"Unexpected Error occured. Please try again. {ex.Message}";
}
}
private void CopyFileToDestinationAsync(string nameSpace,string outDirectory, string internalPath, string resourceName)
{
Assembly assembly = Assembly.GetCallingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(nameSpace + "." + (internalPath == "" ? "" : internalPath + ".") + resourceName))
{
using( BinaryReader br = new BinaryReader(stream))
{
using(FileStream fs = new FileStream(outDirectory + "\\" + resourceName, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
// await Task.Run( ()=> bw.Write(br.ReadBytes((int)stream.Length)));
bw.Write(br.ReadBytes((int)stream.Length));
}
}
}
}
Thread.Sleep(2000);
//For User Friendliness wait 2 seconds to finish
// await Task.Run(() => Thread.Sleep(2000));
}
----------
这就是我使用 async/await 尝试的方式
private async Task CopyFileToDestinationAsync(string nameSpace,string outDirectory, string internalPath, string resourceName)
{
Assembly assembly = Assembly.GetCallingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(nameSpace + "." + (internalPath == "" ? "" : internalPath + ".") + resourceName))
{
using( BinaryReader br = new BinaryReader(stream))
{
using(FileStream fs = new FileStream(outDirectory + "\\" + resourceName, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
await Task.Run( ()=> bw.Write(br.ReadBytes((int)stream.Length)));
// bw.Write(br.ReadBytes((int)stream.Length));
}
}
}
}
//Thread.Sleep(2000);
//For User Friendliness wait 2 seconds to finish
await Task.Run(() => Thread.Sleep(2000));
}
private void FinishButton_Click(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
private async void InstallProgrammAsyc()
{
try
{
FinalMessage = "";
PreInstallationBlock.Visibility = Visibility.Collapsed;
DuringInstallationBlock.Visibility = Visibility.Visible;
await CopyFileToDestinationAsync("MyNameSpace", InstallPath, "Resources", "some.exe");
PrepareProgrammForFinish();
}
catch (Exception ex)
{
DuringInstallationBlock.Visibility = Visibility.Collapsed;
AfterInstallationBlock.Visibility = Visibility.Visible;
FinalMessage = $"Unexpected Error occured. Please try again. {ex.Message}";
}
}
private void InstallButton_Click(object sender, RoutedEventArgs e)
{
InstallProgrammAsyc();
}
【问题讨论】:
-
我通过在外部调用 GetManifestResourceStream 函数并将 Stream 作为参数传递给异步函数来解决了这个问题。但是我真的很想知道为什么它不像我以前尝试过的那样运行。
-
Assembly.GetCallingAssembly很可能没有返回您期望的程序集 -
是的,也许。我也尝试使用其他的。但这也无济于事。
标签: c# wpf async-await .net-assembly