代码之家  ›  专栏  ›  技术社区  ›  Anthony Johnston

在.NET中用线程测试代码

  •  1
  • Anthony Johnston  · 技术社区  · 14 年前

    当许多线程调用我的代码时,我需要测试我的代码

    假装代码。。。。

    使用Microsoft.VisualStudio.TestTools测试工具.单元测试

    10 create a server
    
    20 for( a number of iterations ) start a thread running a function
    
    30 while all the threads are still doing stuff hang on to the server
    
    40 now dispose of the server
    

    如果没有等待,我的单元测试将在作业完成之前处理服务器。

    3 回复  |  直到 14 年前
        1
  •  0
  •   Community kfsone    7 年前

    你的目标是什么 ?

    如果是这样的话,你应该看看一个非常详细的答案,我得到了一个类似的问题,我问了一段时间。答案中包含了许多您应该考虑的不同方法。

    The answer that was given is located here .

    希望有帮助!

        2
  •  0
  •   Mike    14 年前

    Join()是最简单的。如果做不到这一点,可以为每个线程创建一个事件,并等待事件签名。或者,您可以为每个线程创建一个具有初始计数的信号量,并等待所有线程释放该信号量。

        3
  •  0
  •   strager    14 年前

    我对.NET了解不多,但在java中我会这样做

    10 create a server
    
    20 for( a number of iterations ) start a thread running a function
    
    30 while(all the threads are still doing stuff){
        MainThread.Sleep(xMiliseconds)
    }
    
    40 now dispose of the server