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 in C# by pranav
• 2,580 points
21 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.

Read this for extra help https://dotnettutorials.net/lesson/async-main-csharp/

answered May 30 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 in C# by krishna
• 2,340 points
15 views
0 votes
1 answer

Rich Text Editor on a web page

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

answered May 30 in C# by rajiv
• 1,620 points
13 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 in C# by rajiv
• 1,620 points
39 views
0 votes
1 answer

Why does one use dependency injection?

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

answered Jun 6 in C# by pranav
• 2,580 points
22 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 in C# by krishna
• 2,340 points
15 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,120 points
4,507 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
514 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
7,388 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 in C# by rajiv
• 1,620 points
26 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 in C# by rajiv
• 1,620 points
30 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP