相关疑难解决方法(0)

在C#中暂停进程

如何在C#中暂停整个过程(就像我点击Suspend时的Process Explorer一样).

我正在使用Process.Start启动Process,并且在某个事件上,我想暂停该进程以便能够对其"快照"进行一些调查.

.net c#

27
推荐指数
2
解决办法
3万
查看次数

限制C#中的方法

我有一个Game框架,其中有一个实现IBotInterface的Bots列表.这些机器人是由用户定制的,唯一的限制是它们必须实现接口.

然后游戏调用机器人中的方法(希望并行),用于各种事件,如yourTurn和roundStart.我希望机器人在被迫退出计算之前只花费有限的时间来处理这些事件.

我正在尝试的一种例子是:(其中NewGame是代表)

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);
                    }
                }
            );
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我被迫运行可能不会终止的EndInvoke().我想不出一种干净地中止线程的方法.

如果有某种形式的话会很棒

try { 
 bot.NewGame(Info);
} catch (TimeOutException) {
 // Tell bot off.
} finally {
 // Compute things.
}
Run Code Online (Sandbox Code Playgroud)

但我不认为有可能制作这样的结构.

这样做的目的是优雅地处理具有偶然无限循环或需要很长时间计算的AI.

解决这个问题的另一种可能方法就是拥有这样的东西(更多的c#和更少的伪代码)

Class ActionThread {
    pulbic Thread thread { get; set; }
    public Queue<Action> queue { get; set; }

    public void Run() {
        while (true) {
            queue.WaitOne();
            Act a = …
Run Code Online (Sandbox Code Playgroud)

c# multithreading timeout

13
推荐指数
1
解决办法
2658
查看次数

标签 统计

c# ×2

.net ×1

multithreading ×1

timeout ×1