C
C#2y ago
c0nstant

✅ [SOLVED] Need help with converting async method to a 'normal' method

I tried a few solutions to be able to call an async method from my Main method but those have failed, here is the link: https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c Here is my code:
public static class FileEncryptor
{
private const int AesKeySize = 256;
private const int AesBlockSize = 128;
public const int KeySizeInBytes = AesKeySize / 8;

/// <summary>
/// Encrypts a file using a 32 character key.
/// </summary>
public static Task EncryptAsync(string inputFilePath, string outputFilePath, string key, CancellationToken token = default)
{
if (string.IsNullOrWhiteSpace(key))
{
//if key is empty do jack
}

byte[] keyBytes = Encoding.UTF8.GetBytes(key);
return EncryptAsync(inputFilePath, outputFilePath, keyBytes, token);
}
public static async Task EncryptAsync(string inputFilePath, string outputFilePath, byte[] keyBytes, CancellationToken token = default)
{
if (!File.Exists(inputFilePath))
{
}
if (keyBytes.Length != KeySizeInBytes)
{
}
using var aes = Aes.Create();
aes.BlockSize = AesBlockSize;
aes.KeySize = AesKeySize;
aes.Key = keyBytes;

await using FileStream outFileStream = new(outputFilePath, FileMode.Create);

// Write initialization vector to beginning of file
await outFileStream.WriteAsync(aes.IV.AsMemory(0, aes.IV.Length), token);

ICryptoTransform encryptor = aes.CreateEncryptor();
await using CryptoStream cryptoStream = new(
outFileStream,
encryptor,
CryptoStreamMode.Write);

await using var inputFileStream = new FileStream(inputFilePath, FileMode.Open);

await inputFileStream.CopyToAsync(cryptoStream, token);
}
}
public static class FileEncryptor
{
private const int AesKeySize = 256;
private const int AesBlockSize = 128;
public const int KeySizeInBytes = AesKeySize / 8;

/// <summary>
/// Encrypts a file using a 32 character key.
/// </summary>
public static Task EncryptAsync(string inputFilePath, string outputFilePath, string key, CancellationToken token = default)
{
if (string.IsNullOrWhiteSpace(key))
{
//if key is empty do jack
}

byte[] keyBytes = Encoding.UTF8.GetBytes(key);
return EncryptAsync(inputFilePath, outputFilePath, keyBytes, token);
}
public static async Task EncryptAsync(string inputFilePath, string outputFilePath, byte[] keyBytes, CancellationToken token = default)
{
if (!File.Exists(inputFilePath))
{
}
if (keyBytes.Length != KeySizeInBytes)
{
}
using var aes = Aes.Create();
aes.BlockSize = AesBlockSize;
aes.KeySize = AesKeySize;
aes.Key = keyBytes;

await using FileStream outFileStream = new(outputFilePath, FileMode.Create);

// Write initialization vector to beginning of file
await outFileStream.WriteAsync(aes.IV.AsMemory(0, aes.IV.Length), token);

ICryptoTransform encryptor = aes.CreateEncryptor();
await using CryptoStream cryptoStream = new(
outFileStream,
encryptor,
CryptoStreamMode.Write);

await using var inputFileStream = new FileStream(inputFilePath, FileMode.Open);

await inputFileStream.CopyToAsync(cryptoStream, token);
}
}
So how do I go about converting this to something that I can easily call like a normal function from Main?
23 Replies
cap5lut
cap5lut2y ago
why not just make ur Main async?
JakenVeina
JakenVeina2y ago
was about to say that's also kinda the opposite of what the title says
cap5lut
cap5lut2y ago
public class Program
{
public static void Main()
{
Console.WriteLine("Hello ...");
Thread.Sleep(1000);
Console.WriteLine("... World!");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello ...");
Thread.Sleep(1000);
Console.WriteLine("... World!");
}
}
and
public class Program
{
public static async Task Main()
{
Console.WriteLine("Hello ...");
await Task.Delay(1000);
Console.WriteLine("... World!");
}
}
public class Program
{
public static async Task Main()
{
Console.WriteLine("Hello ...");
await Task.Delay(1000);
Console.WriteLine("... World!");
}
}
are both valid entry points and do more or the same
c0nstant
c0nstantOP2y ago
I wouldn't know why would I go async, I am too used to the old ways of coding something that works, speed is not really a concern here but I would like to hear how would you do that?
JakenVeina
JakenVeina2y ago
So how do I go about converting this to something that I can easily call like a normal function from Main
You don't, you have async work to do, so your method must be async. Also, the method is ALREADY a "normal" function that can easily be called from Main()
cap5lut
cap5lut2y ago
async doesnt mean that its faster, its just async ;p
JakenVeina
JakenVeina2y ago
you missed async on Main
cap5lut
cap5lut2y ago
noticed and fixed ;p thx for pointing out 🙂
c0nstant
c0nstantOP2y ago
thanks for the advise, I will see what I can do after the changes
JakenVeina
JakenVeina2y ago
on the rare occasion that you DO need to call an async method from a non-async method, and wait for its completion, you do
var result = MyMethodAsync().GetAwaiter().GetResult();
var result = MyMethodAsync().GetAwaiter().GetResult();
avoid doing this unless you have no other option
c0nstant
c0nstantOP2y ago
Somehow, it actually worked this time. So the last time I was trying to run it, my IDE had automatically added async as part of my main method, something like static async Main(string[] args) Now I rebuilt it and can finally run it I don't know how to explain this :/ but it worked thanks 🙂
JakenVeina
JakenVeina2y ago
async methods must return Task 99.99% of the time
c0nstant
c0nstantOP2y ago
by returning task do you mean something like await Task.Delay(1000);?
JakenVeina
JakenVeina2y ago
no, I mean defining the method's return type to be Task or Task<T>
c0nstant
c0nstantOP2y ago
Got it
cap5lut
cap5lut2y ago
the Main method can come in a lot of variants static int Main(string[] args) static int Main() static void Main(string[] args) static void Main() and the same for async, instead of void it would be Task and instead of int it would be Task<int> (and it can be either just static or static async)
JakenVeina
JakenVeina2y ago
uhh, why'd you list each version twice?
cap5lut
cap5lut2y ago
oh fixed 😂
c0nstant
c0nstantOP2y ago
I am getting familiar, thanks for sharing
cap5lut
cap5lut2y ago
lets mention top-level statements for completion as well:
Console.WriteLine("Hello World");
Console.WriteLine("Hello World");
can also be a complete "main method" (the compiler automatically builds a Program class with the Main method then)
JakenVeina
JakenVeina2y ago
lets mention top-level statements for completion as well:
could we, instead, not? 😆
cap5lut
cap5lut2y ago
too late 😒 $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server