I have been researching game loops and thread. I post my code below to accomplish this. Does this look correct to anyone or have a better solution for multithreading a game loop?
// Loop
using System;
namespace Client.Plateform.Window.Lifetime
{
public class Loop : Client.Plateform.Window.OperatingSystem.Threads
{
/// <summary>
///
/// </summary>
private static bool _IsExit = false;
/// <summary>
///
/// </summary>
public static bool IsExit
{
get
{
return _IsExit;
}
set
{
_IsExit = value;
}
}
/// <summary>
///
/// </summary>
public Loop()
{
}
/// <summary>
///
/// </summary>
public void Excute()
{
base.RenderThread.Start();
base.LogicThread.Start();
base.ResourceThread.Start();
base.SoundThread.Start();
}
}
}
// Thread
using System;
using System.Threading;
namespace Client.Plateform.Window.OperatingSystem
{
public class Threads
{
#region Global Var
private ThreadStart _RenderThreadStart = null;
private Thread _RenderThread = null;
private ThreadStart _SoundThreadStart = null;
private Thread _SoundThread = null;
private ThreadStart _ResourceThreadStart = null;
private Thread _ResourceThread = null;
private ThreadStart _LogicThreadStart = null;
private Thread _LogicThread = null;
#endregion
#region Accessors
/// <summary>
/// asdfadsf
/// </summary>
public Thread RenderThread
{
get
{
return _RenderThread;
}
}
/// <summary>
/// asdfadsf
/// </summary>
public Thread SoundThread
{
get
{
return _SoundThread;
}
}
/// <summary>
/// asdfadsf
/// </summary>
public Thread ResourceThread
{
get
{
return _ResourceThread;
}
}
/// <summary>
/// asdfadsf
/// </summary>
public Thread LogicThread
{
get
{
return _LogicThread;
}
}
#endregion
#region Constructor
/// <summary>
///
/// </summary>
public Threads()
{
_RenderThreadStart = new ThreadStart(() => RenderMethod());
_RenderThread = new Thread(_RenderThreadStart);
_SoundThreadStart = new ThreadStart(() => SoundMethod());
_SoundThread = new Thread(_SoundThreadStart);
_ResourceThreadStart = new ThreadStart(() => ResourceMethod());
_ResourceThread = new Thread(_ResourceThreadStart);
_LogicThreadStart = new ThreadStart(() => LogicMethod());
_LogicThread = new Thread(_LogicThreadStart);
}
#endregion
#region Method
/// <summary>
///
/// </summary>
private void RenderMethod()
{
while (!Plateform.Window.Lifetime.Loop.IsExit)
{
Console.WriteLine("Render Method");
Thread.Sleep(3000);
}
}
/// <summary>
///
/// </summary>
private void SoundMethod()
{
while (!Plateform.Window.Lifetime.Loop.IsExit)
{
Console.WriteLine("Sound Method");
Thread.Sleep(8000);
}
}
/// <summary>
///
/// </summary>
private void ResourceMethod()
{
while (!Plateform.Window.Lifetime.Loop.IsExit)
{
Console.WriteLine("Resource Method");
Thread.Sleep(6000);
}
}
/// <summary>
///
/// </summary>
private void LogicMethod()
{
while (!Plateform.Window.Lifetime.Loop.IsExit)
{
Console.WriteLine("Logic Method");
Thread.Sleep(13000);
}
}
#endregion
}
}