I have a Game framework where there is a list of Bots who implement IBotInterface. These bots are custom made by user with the only limitation that they must implement the interface.
The game then calls methods in the bots (hopefully in parallel) for various events like yourTurn and roundStart. I want the bot to only spend a limited amount of time handling these events before being forced to quit computing.
An example of the kind of thing i'm trying is : (where NewGame is a delegate)
Parallel.ForEach(Bots, delegate(IBot bot)
{
NewGame del = bot.NewGame;
IAsyncResult r = del.BeginInvoke(Info, null, null);
WaitHandle h = r.AsyncWaitHandle;
h.WaitOne(RoundLimit);
if (!r.IsCompleted)
{
del.EndInvoke(r);
}
}
);
In this case I am forced to run EndInvoke() which might not terminate. I cant think of a way to cleanly abort the thread.
It would be great if there was some kind of
try {
bot.NewGame(Info);
} catch (TimeOutException) {
// Tell bot off.
} finally {
// Compute things.
}
But I dont think its possible to make such a construct.
The purpose of this is to gracefully handle AI's who have accidental infinite loops or are taking a long time to compute.
Another possible way of tackling this would be to have something like this (with more c# and less pseudocode)
Class ActionThread {
pulbic Thread thread { get; set; }
public Queue<Action> queue { get; set; }
public void Run() {
while (true) {
queue.WaitOne();
Act a = queue.dequeue();
a();
}
}
Class foo {
main() {
....
foreach(Bot b in Bots) {
ActionThread a = getActionThread(b.UniqueID);
NewGame del = b.NewGame;
a.queue.queue(del);
}
Thread.Sleep(1000);
foreach (ActionThread a in Threads) {
a.Suspend();
}
}
}
Not the cleanest way but it would work. (I'll worry about how to pass paramaters in and take return values out later).
[Further Edit]
I'm not too sure what an appdomain is, from the looks of it I could do so, but it cant see how it would help
I hope not to expect malicious code. Trying to kill the other bots threads is not a valid way to win the game. I just wanted to give every bot a second to compute then move on with the gameflow so mainly expecting slow or bugged code here.
I'm trying to see what I can do with Task, getting somewhere slowly.
I'll read into what CAS can do, thank you guys
[More Edit]
My head hurts, I can't seem to think or code anymore. I'm setting up a message pass system to a dedicated thread per bot and will suspend/sleep those threads
I have decided that I will use a fully socketed server client system. So that the client can do whatever it wants and I'll just ignore if it refuses to reply to server messages. Pity it had to come to this.