相关疑难解决方法(0)

为什么Microsoft的示例代码崩溃了?

我想学习一点关于.NET中的多线程并从MSDN中获取这个示例.它编译很好但在运行时崩溃.我希望微软会告诉我创建多个线程的正确方法.我无法弄清楚为什么它会崩溃.有人可以帮忙吗?

// Mutex.cs
// Mutex object example
using System;
using System.Threading;

public class MutexSample
{
   static Mutex gM1;
   static Mutex gM2;
   const int ITERS = 100;
   static AutoResetEvent Event1 = new AutoResetEvent(false);
   static AutoResetEvent Event2 = new AutoResetEvent(false);
   static AutoResetEvent Event3 = new AutoResetEvent(false);
   static AutoResetEvent Event4 = new AutoResetEvent(false);

   public static void Main(String[] args)
   {
      Console.WriteLine("Mutex Sample ...");

      // Create Mutex initialOwned, with name of "MyMutex".
      gM1 = new Mutex(true,"MyMutex");
      // Create Mutex initialOwned, with no name. …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading thread-safety

4
推荐指数
1
解决办法
507
查看次数

标签 统计

.net ×1

c# ×1

multithreading ×1

thread-safety ×1