Dynamic Async Events

Dynamic Events Async Examples

  • These events examples use an Input Field (which is a UI element) as an extra event parameter. Normally you will get exceptions for accessing/modifying UI elements or GameObjects asynchronously so be cautious. Most likely an exception will be thrown but it will not be caught so you will never know. I did this to test and to access the text inside the InputField (Not Recommended to use extra parameter for Dynamic Async methods). So as a good practice, don't access UI elements/GameObjects with async invoked dynamic events

  • public void method seems to run synchronously (even though it is executed as a Task.Run()) so it may block the UI or prevent async/await concurrency. Also, I am not calling await on fileStream.WriteAsync(bytes, 0, bytes.Length); even though I declared that it should use async. It doesn't seem to bock the UI but either way not using await/async for async tasks/work is bad practice. So, don't use public void for async invoked dynamic events

  • Both async void and async Task perform about the same and execute concurrently.

Don't do

    // Don't Use void without async
    [LoginEventAsync(LoginStatus.LoggedIn)]
    public void DynamicEventVoid(ILoginSession loginSession, InputField inputField)
    {
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        Debug.Log($"Inputfiled {inputField.text}");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 200; i++)
        {
            stringBuilder.AppendLine($"Adding {inputField.text} : {i}");
            var bytes = Encoding.Unicode.GetBytes(stringBuilder.ToString());
            using (FileStream fileStream = new FileStream($"{Directory.GetCurrentDirectory()}\\Assets\\void with async file stream {i}.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, bufferSize: 4096, useAsync: true))
            {
                fileStream.WriteAsync(bytes, 0, bytes.Length);
            }
            Debug.Log("Done creating text file");
        }

        stopwatch.Stop();
        Debug.Log($"Dynamic void Invoked by Async Method took {stopwatch.Elapsed}");
    }
    // Do Use void with async
    [LoginEventAsync(LoginStatus.LoggedIn)]
    public async void DynamicEventAsyncVoid(ILoginSession loginSession, InputField inputField)
    {
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        Debug.Log($"Inputfiled {inputField.text}");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 200; i++)
        {
            stringBuilder.AppendLine($"Adding {inputField.text} : {i}");   
        var bytes = Encoding.Unicode.GetBytes(stringBuilder.ToString());
        using (FileStream fileStream = new FileStream($"{Directory.GetCurrentDirectory()}\\Assets\\async void{i}.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, bufferSize: 4096, useAsync: true))
        {
            await fileStream.WriteAsync(bytes, 0, bytes.Length);
        }
        Debug.Log("Done creating text file");
        }

        stopwatch.Stop();
        Debug.Log($"Dynamic Async Void Invoked Method took {stopwatch.Elapsed}");
    }
    
    // Do Use async Task
    [LoginEventAsync(LoginStatus.LoggedIn)]
    public async Task DynamicEventAwaitAsync(ILoginSession loginSession, InputField inputField)
    {
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        Debug.Log($"Inputfiled {inputField.text}");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 200; i++)
        {
            stringBuilder.AppendLine($"Adding {inputField.text} : {i}"); 
            var bytes = Encoding.Unicode.GetBytes(stringBuilder.ToString());
            using (FileStream fileStream = new FileStream($"{Directory.GetCurrentDirectory()}\\Assets\\async Task{i}.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite, bufferSize: 4096, useAsync: true))
            {
                await fileStream.WriteAsync(bytes, 0, bytes.Length);
            }
            Debug.Log("Done creating text file");
        }

        stopwatch.Stop();
        Debug.Log($"Dynamic Async Task Invoked Method took {stopwatch.Elapsed}");
    }

Last updated