Skip to content

Instantly share code, notes, and snippets.

@Altair200333
Last active January 13, 2022 09:49
Show Gist options
  • Save Altair200333/5a694b48d9c7500af1bdde7a36229d51 to your computer and use it in GitHub Desktop.
Save Altair200333/5a694b48d9c7500af1bdde7a36229d51 to your computer and use it in GitHub Desktop.
SyncTimerDispatcher executes specified method on the UI thread every [ms] milliseconds
namespace audioTest
{
//Executes specified method on the UI thread every [ms] milliseconds
class SyncTimerDispatcher
{
public delegate void TickDelegate();
//Your custom function to be called periodically
public TickDelegate TickHandler;
public bool Start(int ms)
{
if(TickHandler == null)
return false;
System.Threading.Thread.CurrentThread.Name = "UI THREAD";
System.Timers.Timer t = new System.Timers.Timer(ms);
t.Elapsed += OnTimeElapsed;
t.Start();
return true;
}
void OnTimeElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//dispatch handler to the UI thread
Application.Current?.Dispatcher.Invoke(TickHandler);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment