Is async partial void MyPartialMethod () dangerous?

I have seen many warnings about writing code, for example ...

public async void MyDangerousMethodWhichCouldCrashMyApp...

I read that its OK with Eventhandlers, as they should return a void. However, partial methods should also return void. You may have the following code ...

static void Main(string[] args)
{
    MainAsync().Wait();
    Console.ReadLine();
}

async static Task MainAsync()
{
    MyCodeGeneratedClass c = new MyCodeGeneratedClass();
    try
    {
        await c.MyCodeGeneratedMethod();
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

public partial class MyCodeGeneratedClass
{
    public async Task MyCodeGeneratedMethod()
    {
        HttpClient client = new HttpClient();
        Console.WriteLine(await client.GetStringAsync("http://msdn.microsoft.com"));
        MyCustomCode();
    }

    partial void MyCustomCode();
}

and then implement how ...

partial class MyCodeGeneratedClass
{
    async partial void MyCustomCode()
    {
        HttpClient client = new HttpClient();
        Console.WriteLine(await client.GetStringAsync("http://msdn.microsoft.com"));
        throw new Exception("Boom");
    }
}

But what will happen to the application if the implementation of MyCustomCode encounters an exception?

If this is not a race, given how common the asynchronous / expected is, does this mean that the partial methods are essentially out of date? Should code generation systems stop exposing partial methods in favor of events or, perhaps, even more secure protected virtual methods in the base class? i.e.

protected virtual Task MyCustomCode(T foo)
{
    return Task.FromResult(0);
}

EDIT: , . , , , . , , , async partial void MyPartialMethod, MyCodeGeneratedMethod, , , . , , .

+4
2

, MyCustomCode ?

async void , SynchronizationContext, . async void async.

, ?

, . , , . , Task, , , .

, , ?

; async void.

"" , , .

, , . , , async void .

+6

, Xamarin, , , ...

https://forums.xamarin.com/discussion/2766/cannot-await-in-an-async-partial-method

, , , try catch ...

public partial class MyCodeGeneratedClass
{
    public async Task MyCodeGeneratedMethod()
    {
        HttpClient client = new HttpClient();
        Console.WriteLine(await client.GetStringAsync("http://msdn.microsoft.com"));
        MyCustomCode();
    }

    partial void MyCustomCode();
}

partial class MyCodeGeneratedClass
{
    async partial void MyCustomCode()
    {
        try
        {
            await MyCustomCodeAsync();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    protected async Task MyCustomCodeAsync()
    {
        HttpClient client = new HttpClient();
        Console.WriteLine(await client.GetStringAsync("http://msdn.microsoft.com"));

        throw new Exception("Boom");
    }
}

async Task . , / Microsoft , .

, , , , , -, . , CustomCode, , .

+1

Source: https://habr.com/ru/post/1653869/


All Articles