Last active
January 13, 2022 09:49
-
-
Save Altair200333/5a694b48d9c7500af1bdde7a36229d51 to your computer and use it in GitHub Desktop.
SyncTimerDispatcher executes specified method on the UI thread every [ms] milliseconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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