【问题标题】:Threading in C# produces errorsC# 中的线程会产生错误
【发布时间】:2010-06-13 17:27:49
【问题描述】:

我正在使用此代码:

private void Form1_Load(object sender, EventArgs e)
{

}

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    string response = serialPort1.ReadLine();
    this.BeginInvoke(new MethodInvoker(
        () => textBox1.AppendText(response + "\r\n")
    ));
}

ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();

但我遇到了很多错误:

错误 2 找不到类型或命名空间名称“ThreadStart” (您是否缺少 using 指令或程序集引用?) C:\Users\alexluvsdanielle\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 31 44 WindowsFormsApplication1

错误 3 当前上下文中不存在名称“ThreadWork” C:\Users\alexluvsdanielle\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 31 56 WindowsFormsApplication1

错误 4 找不到类型或命名空间名称“线程”(是 您缺少 using 指令或程序集引用?) C:\Users\alexluvsdanielle\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 32 31 WindowsFormsApplication1

错误 5 字段初始值设定项无法引用非静态字段, 方法或属性“WindowsFormsApplication1.Form1.myThreadDelegate” C:\Users\alexluvsdanielle\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 32 38 WindowsFormsApplication1

我做错了什么?

【问题讨论】:

  • 你在那个文件中有using System.Threading; 行吗?
  • 您的线程代码块不在方法中。

标签: c# multithreading


【解决方案1】:

也许我在这里提到了明显的流血,但从代码示例中,您的代码块:

ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();

不在方法中。我以前做过。

一旦你解决了这个问题,其他答案显然会适用。

【讨论】:

  • 哇 - 我以为这只是发布代码的 sn-ps 的问题 :)
【解决方案2】:

ThreadThreadStart 位于 System.Threading MSDN page

using System.Threading; 添加到代码顶部的命名空间。

ThreadWork 不是 .NET 中定义的类。我找到了一个 MSDN 页面 here,它在示例代码中使用。所以你需要用你定义了DoWork方法的类的名字来替换它。如果它与此代码在同一类中,则只需传递DoWork

【讨论】:

    【解决方案3】:

    您的代码文件中缺少using System.Threading

    【讨论】:

      【解决方案4】:

      其他人已经给出了这个特定问题的解决方案,但真正的答案是“我做错了什么?”是(IMO)“您没有阅读错误消息。”出现错误消息是有原因的:作为提示告诉您出了什么问题。

      您收到了几条类似这样的错误消息:

      类型或命名空间名称'Fpp' 找不到(您是否缺少 使用指令或程序集 参考?)

      错误消息在这里给了你一个提示:它找不到类型,但这可能是因为你没有正确的引用或using 指令。因此,请检查您的参考资料,并检查您的 using 指令。在这种情况下,Thread 位于 mscorlib 中,因此您不太可能遇到程序集引用问题 - 所以接下来要检查的是您的 using 指令。它在System.Threading 命名空间中,所以你需要

      using System.Threading;
      

      在顶部 - 或者您当然可以在任何地方指定 System.Threading.Thread,但这有点冗长。

      仔细阅读错误消息通常意味着您不需要问自己做错了什么:他们会告诉您,就像他们在这里所做的那样。

      ThreadWork 错误的修复更难确定 - 你应该使用你想在新线程中执行的任何方法......假设你真的想自己启动一个新线程。你确定你真的这样做了吗?

      【讨论】:

      • 我认为ThreadWork来自MSDN上的一些示例代码,例如在这个页面上-msdn.microsoft.com/en-us/library/…
      • @ChrisF:是的 - 我的意思是很难说代码实际上应该是什么,因为我们不知道 OP 的新线程是什么意思。
      • jon,你输入的速度到底是多少?
      【解决方案5】:

      首先确保你有一个

      using System.Threading;
      

      在文件的顶部。

      另外——你不能在方法之外调用myThread.Start();

      【讨论】:

        【解决方案6】:

        您应该将以下代码放在 C# 代码文件的顶部:
        using System.Threading

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-02
          • 2021-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-28
          • 2020-12-30
          相关资源
          最近更新 更多