In my program I am writing a Resource File into some Location the user selects. For that I am using GetManifestResourceStream. Everything worked fine.
Next I wanted to make my writing operation not-blocking the UI. So I changed the code using async await. Unfortunately GetManifestRresourceStream is returning null now. Changing back without using async await everything works fine again.
What am I doing wrong and how can I solve it so that I am able to copy the file and the UI is not blocked.
This Is how it works without async/await (Please dont be confused by the Method Names)
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));
}
----------
And this Is how I tried it with 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();
}
Assembly.GetCallingAssembly
is not returning the assembly you're expecting