【问题标题】:Creating new a Thread() is asking ParameterizedThreadStart object as a parameter in C sharp创建新的 Thread() 要求 ParameterizedThreadStart 对象作为 C 语言中的参数
【发布时间】:2011-04-24 17:07:54
【问题描述】:

啊,我在线程编程方面非常菜鸟,刚刚开始创建多个线程的基本步骤,所以我用谷歌搜索并得到了一些关于在 c# 中创建线程的 sn-ps,这是我找到的 sn-p:

public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(new ThreadStart(this.run)); // here m getting error
    thrd.Name = name; 
    thrd.Start(); 
  } 

  // Entry point of thread. 
  void run() { 
    Console.WriteLine(thrd.Name + " starting."); 

    do { 
      Thread.Sleep(500); 
      Console.WriteLine("In " + thrd.Name + 
                        ", count is " + count); 
      count++; 
    } while(count < 10); 

    Console.WriteLine(thrd.Name + " terminating."); 
  } 
} 

错误是System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)的最佳重载方法匹配有一些无效参数

为什么 Thread 构造函数要求我提供 ParameterizedThreadStart 对象,我想要传递简单的 ThreadStart 对象。

另一件事是 ThreadStart 类没有带有 1 个参数的构造函数,即它需要 0 个参数,但在 sn-p 中他们显示了 new ThreadStart(this.run) this ? m 使用 C# 2008

这是完整的代码

使用系统; 使用 System.Threading; 类我的线程 { 公共整数计数; 公共线程 thrd; 公共MyThread(字符串名称){ 计数 = 0; thrd = new Thread(new ThreadStart(this.run)); thrd.Name = 名称; thrd.Start(); } // 线程的入口点。 无效运行(){ Console.WriteLine(thrd.Name + "starting."); 做 { 线程.睡眠(500); Console.WriteLine("In" + thrd.Name + ", 计数是 " + count); 计数++; } 而(计数

【问题讨论】:

  • 您能否提供一个代码示例来演示该问题?您发布的代码可以正常工作(一旦修复了明显的遗漏)。
  • @Mark:您的“完整”代码显然不完整。给我们一段绝对完整的代码,可以将它复制粘贴到一个新的文本文件中,然后进行编译。

标签: c#


【解决方案1】:

不知道为什么它不起作用,但你可以试试:

thrd = new Thread(run);

run 方法组到ThreadStart 委托的转换是隐式的。

我怀疑您在 System.Threading.ThreadStart 和代码中其他位置定义的另一种类型之间存在名称冲突...尝试将插入符号放在 ThreadStart 上并按 F12 进入声明

【讨论】:

    【解决方案2】:

    我已经完成了你给我们的不完整的例子,我没有遇到同样的编译器错误。

    class Program
    {
        static int count;
        static Thread thrd;
    
    
        public static void MyThread(string name) { 
            count = 0; 
            thrd = new Thread(new ThreadStart(run)); // here m getting error
            thrd.Name = name; 
            thrd.Start(); 
        } 
    
      // Entry point of thread. 
      static void  run() { 
        Console.WriteLine(thrd.Name + " starting.");
    
        do
        {
            Thread.Sleep(500);
            Console.WriteLine("In " + thrd.Name +
                              ", count is " + count);
            count++;
        } while (count == 5);
      }
    
    
    
      static void Main(string[] args)
      {
      }
    }
    

    【讨论】:

      【解决方案3】:

      这绝对没问题,因为 an overload 使用 ThreadStart 而不是 ParameterizedThreadStart

      我怀疑这里还有其他问题...您能否提供一个简短但完整的示例来说明问题?

      除了缺少类声明本身和变量声明之外,您的代码可以毫无问题地为我编译:

      using System;
      using System.Threading;
      
      class MyThread {
      
          int count;
          Thread thrd;
      
          public MyThread(string name) { 
              count = 0; 
              thrd = new Thread(new ThreadStart(this.run)); // here m getting error
              thrd.Name = name; 
              thrd.Start(); 
          } 
      
          // Entry point of thread. 
          void run() { 
              Console.WriteLine(thrd.Name + " starting."); 
      
              do { 
                  Thread.Sleep(500); 
                  Console.WriteLine("In " + thrd.Name + 
                                    ", count is " + count); 
                  count++; 
              } while(count < 10); 
      
              Console.WriteLine(thrd.Name + " terminating."); 
          } 
      }
      

      【讨论】:

      • 但是为什么我在这里遇到错误thrd = new Thread(new ThreadStart(this.run))The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)' has some invalid arguments
      • @FosterZ:我不知道,因为我没有看到我展示的完整代码......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多