In the wpf application there is a canvas which i pass to my Game class. In my gameclass I want to update the canvas every 0.02 seconds. At the moment my code is working but it feels 'hacky', my code:
public void Start()
{
bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.ProgressChanged += bw_ProgressChanged;
bw.WorkerReportsProgress = true;
bw.RunWorkerAsync();
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
GameRender();
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
while (Running)
{
GameUpdate();
//GameRender();
bw.ReportProgress(1);
//repaint();
try
{
Thread.Sleep(10);
}
catch (Exception ex)
{
Console.WriteLine(ex + "");
}
}
}
Because it seems like a hack to me I tried using a Thread like;
public void Start()
{
new Thread(() =>
{
while (true)
{
GameUpdate();
GameRender();
}
}).Start();
}
But this crashes in my GameRender() where i try to update my canvas. Error:
An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
How should I update the Canvas from a class?