private static void WorkerThreadProc(object args)
{
WorkerInterface workInterface = args as WorkerInterface;
workInterface.Error = null;
workInterface.Progress = 0.0f;
try
{
// All work is performed here.
if (!workInterface.owner.MainThreadRequired)
{
var taskEnumerator = workInterface.Task(workInterface).GetEnumerator();
while (taskEnumerator.MoveNext()) {}
}
// All work is actually performed in the main thread.
else
{
// Wait a little so the main thread has time for drawing the GUI.
Thread.Sleep(20);
// Perform task on the main thread - which is necessary because OpenGL and friends don't like multithreading.
// In order to keep the GUI updated, the task is split into chunks. After each chunk, GUI events can be processed.
var taskEnumerator = workInterface.Task(workInterface).GetEnumerator();
DateTime lastCheck = DateTime.Now;
while ((bool)workInterface.MainForm.Invoke((Func<bool>)taskEnumerator.MoveNext))
{
TimeSpan lastTime = DateTime.Now - lastCheck;
// Wait a little so the main thread has time for drawing the GUI.
if (lastTime.TotalMilliseconds > 1000.0f)
{
Thread.Sleep(20);
lastCheck = DateTime.Now;
}
}
}
// Assume the task finished completely
workInterface.Progress = 1.0f;
}
catch (Exception e)
{
Log.Editor.WriteError("Failed to perform task: {0}", Log.Exception(e));
workInterface.Error = e;
}
}