Why program working differently on every run when using async

0 votes

So I have an application that uses SMB to move files within an Azure fileshare. The problem is that every time I implement a programme, I get different outcomes. I've seen that some files are being destroyed from their original locations while others are not being copied. It is working fine while I am debugging code step by step. Perhaps it's because my code is performed before my async task is completed. Here's a sample of the code I wrote:

public static class SMB
{

     public static async Task MoveFile()
    {
    ....
        await CreateNestedDirectoryIfNotExistsAsync();
    ....
    }
    private static async Task CreateNestedDirectoryIfNotExistsAsync()
    {
    ....
    }

}

I'm utilising the aforementioned code in my console application project by including the class in my reference, and then using the methods as follows:

public static class SMB
{

     public static async Task MoveFile()
    {
    ....
        await CreateNestedDirectoryIfNotExistsAsync();
    ....
    }
    private static async Task CreateNestedDirectoryIfNotExistsAsync()
    {
    ....
    }

}
May 30, 2022 in C# by pranav
• 2,590 points
521 views

1 answer to this question.

0 votes

You can use the async Main method from c# 7.1 onward but you have to enable the 7.1 features in VS 2017.

Still, if you want to use the synchronous Main method and keep your code, you simply should modify

UseMethodOfDifferentProject().getAwaiter()

to

UseMethodOfDifferentProject().GetAwaiter().GetResult();

GetWaiter() does not return the asynchronous operation's result; GetResult() does. Essentially, the software exits without waiting for the asynchronous process to complete. I can see how this will wind up being a terrible disaster.

answered May 30, 2022 by rajiv
• 1,620 points

Related Questions In C#

0 votes
1 answer

How many files are created when a C program is executed

It depends. Depending on the compiler type ...READ MORE

answered Jun 23, 2022 in C# by krishna
• 2,820 points
3,125 views
0 votes
1 answer

Rich Text Editor on a web page

Perhaps something along the lines of TinyMCE ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
654 views
0 votes
1 answer

Questions every good .NET developer should be able to answer?

If I were interviewing someone who knew ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
748 views
0 votes
1 answer

Why does one use dependency injection?

I believe that many people are perplexed ...READ MORE

answered Jun 6, 2022 in C# by pranav
• 2,590 points
429 views
0 votes
1 answer

How and when to use ‘async’ and ‘await’

The compiler creates a state machine in ...READ MORE

answered Jul 4, 2022 in C# by krishna
• 2,820 points
529 views
0 votes
0 answers

Convert async Task from c# to vb.net

I'm using a C# to VB.NET converter ...READ MORE

Dec 12, 2022 in C# by Roshan
• 300 points
1,570 views
0 votes
1 answer

Flutter, run async function on stream.listen

You can make the listen callback an ...READ MORE

answered Mar 20, 2023 in Android by pooja
1,331 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,681 views
0 votes
1 answer

How can I compile and run c# program without using visual studio

If you have.NET Framework v4 installed, then C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe ...READ MORE

answered Jun 9, 2022 in C# by rajiv
• 1,620 points
1,272 views
0 votes
1 answer

Why should I avoid using Properties in C#?

Jeff dislikes properties because they resemble fields, ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
638 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP