> For the complete documentation index, see [llms.txt](https://fullstackindie.gitbook.io/easy-code-for-vivox/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fullstackindie.gitbook.io/easy-code-for-vivox/dynamic-events/dynamic-async-events.md).

# Dynamic Async Events

### Dynamic Events Async Examples

* These events examples use an **Input Field (which is a UI element)** as an extra *event parameter***.** <mark style="color:orange;">Normally you will get exceptions for accessing/modifying UI elements or GameObjects asynchronously so be cautious.</mark> <mark style="color:orange;"></mark><mark style="color:orange;">**Most likely an exception will be thrown but it will not be caught so you will never know.**</mark>**&#x20;I did this to test and to access the text inside the InputField (Not Recommended to use extra parameter for Dynamic Async methods).**\
  \&#xNAN;*<mark style="color:red;">**So as a good practice**</mark>*<mark style="color:red;">**,**</mark> *<mark style="color:red;">**don't access UI elements/GameObjects with async invoked dynamic events**</mark>*
* <mark style="color:blue;">public void</mark> method seems to ***run synchronously*** (even though it is executed as a <mark style="color:blue;">Task.Run</mark>()) so it <mark style="color:orange;">may block the UI or prevent</mark> <mark style="color:orange;"></mark><mark style="color:orange;">**async/await concurrency**</mark>. Also, I am not calling await on **`fileStream.WriteAsync(bytes, 0, bytes.Length);`** even though I declared that it should use <mark style="color:blue;">async</mark>. It doesn't seem to bock the UI but either way not using <mark style="color:blue;">await/async</mark> for **async** tasks/work is bad practice. \ <mark style="color:red;">**So,**</mark> <mark style="color:red;">**don't use public void for async invoked dynamic events**</mark>
* Both <mark style="color:blue;">async void</mark> and <mark style="color:blue;">async Task</mark> perform about the same and execute concurrently.&#x20;

### Don't do

```csharp
    // 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 do :grin:

```csharp
    // 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}");
    }
```
