Easy Code For Vivox
  • Introduction
    • Official Links
    • Getting Started
      • Wheres The Code
      • Setup EasyCode in My Project
    • Setup Demo Scenes
      • Vivox Developer Portal
      • Unity Gaming Services (UGS) Dashboard
    • Things To Consider
    • FAQ
  • Development Info
    • Design Decisions
    • Release Notes
      • v2.0
      • v1.3
      • v1.2
      • v1.1
      • Template
    • Not Supported
  • Easy Code For Vivox
    • How do I setup EasyCode?
      • Setup Your Credentials
    • How do I do this in EasyCode?
      • Login / Logout
        • Login Properties
        • SetTransmissionMode
        • Login Events
      • Join / Leave Channel
        • Audio Channel
          • Audio Channel Events
        • Text Channel
          • Text Channel Events
        • Channel Events
      • Send Messages
        • Message Events
      • Mute / Unmute
        • Mute / Unmute Events
      • Subscribe to User Events
      • Volume / Audio Settings
        • Audio Device Events
      • Text To Speech ( TTS )
        • TTS Events
    • Vivox Access Tokens
      • Unity Cloud Code
    • Supported Vivox Events
      • Callback Methods
    • Folder Structure / Info
      • / Demo Scenes /
      • / Documentation /
      • / Examples /
      • / Plugins /
      • / Resources /
      • / Scripts /
      • / Settings /
    • Common Errors
  • API Info
    • EasySession.cs
    • EasyManager.cs
      • Main Methods
      • Vivox Event Callbacks
    • Easy3DPositional.cs
    • EasyVivoxUtilities.cs
    • EasySettings.cs
    • Extension Methods
      • EasySIPExtensions.cs
      • GameObjectExtensions.cs
      • TTSMessageExtensions.cs
      • UIExtensions.cs
      • VivoxExtensions.cs
      • EasyDebug.cs
    • EasyEvents.cs
  • Dependency Injection
    • Zenject vs Extenject
    • Install Dependencies
    • Inject Classes
  • Dynamic Events
    • Dynamic Events
    • Tests
    • Gotchas
    • Dynamic Async Events
      • Dont Do
    • Event Examples
  • Related Info
    • How do I do this in Vivox?
      • Conference Chat
    • Pre-Processor Directives
    • How to set iOS Info.plist for Unity?
    • Unity Gaming Services
  • The Future
    • Roadmap
    • Todo / Notes / Changelog
Powered by GitBook
On this page
  • Dynamic Events Async Examples
  • Don't do
  • Do do

Was this helpful?

  1. Dynamic Events

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}");
    }
PreviousGotchasNextDont Do

Last updated 2 years ago

Was this helpful?

Do do

😁