【问题标题】:creating thread using c#使用c#创建线程
【发布时间】:2011-08-05 13:59:47
【问题描述】:

如果我创建了这样的对象

memberdetail md = new memberdetail(arg0, arg1, arg3)

如何为 md 对象创建线程?

Thread t = new Thread((md));
t.Start();

不工作。谢谢

【问题讨论】:

  • 在这种情况下,您需要将线程指向 md 实例中的方法。 ThreadStart 需要指向一个方法,而不是一个“对象”。
  • 您是否尝试将 md 作为参数传递给线程?如果是这样,您将需要使用参数化 ThreadStart msdn.microsoft.com/en-us/magazine/cc163600.aspx
  • 不能为对象创建线程;你宁愿定义一个你想在单独的线程中运行的任务(方法)。

标签: c# multithreading


【解决方案1】:

你不是在一个对象上启动一个线程,而是一个方法

memberdetail md = new memberdetail(arg0, arg1, arg3);
Thread t = new Thread(md.DoSomething);   
t.Start();

【讨论】:

  • 您可以使用特定的对象方法启动线程,但线程和对象之间没有任何联系。
  • 是的!说的对。更新了我的答案以删除这个暗示性陈述。
【解决方案2】:

您必须将对象的方法传递给线程的构造函数,如下所示,

Thread t = new Thread(md.SomeFn);
t.Start();

http://msdn.microsoft.com/en-us/library/ms173178(v=vs.80).aspx

【讨论】:

    【解决方案3】:

    如果您想在启动时将对象传递给线程,请执行以下操作:

    public class Work
    {
        public static void Main()
        {
            memberdetail md = new memberdetail(arg0, arg1, arg3)
            Thread newThread = new Thread(Work.DoWork);
    
            // Use the overload of the Start method that has a
            // parameter of type Object.
            newThread.Start(md);
        }
    
        public static void DoWork(object data)
        {
            Console.WriteLine("Static thread procedure. Data='{0}'", data);
            // You can convert it here
            memberdetail md = data as memberdetail;
            if(md != null)
            {
               // Use md
            }
        }
    }
    

    更多详情请见Thread.Start Method (Object)

    【讨论】:

      【解决方案4】:
      1. 您没有为对象创建线程。线程是分开的。

      2. 在大多数情况下,您不应(不再)使用线程。查看线程池和任务(TPL 库)。

      【讨论】:

      【解决方案5】:

      如果你只想在不同的线程中创建多个对象,试试这个。

      for(int i = 0, i < numThreads; i++)
          (new Thread( () => 
              { memberdetail md = new memberdetail(arg0, arg1, arg3) }).start()
      

      您想要执行的任何其他操作都可以在 lambda 的主体内执行,例如

      for(int i = 0, i < numThreads; i++)
          (new Thread( () => 
              {
                  memberdetail md = new memberdetail(arg0, arg1, arg3);
                  md.ActionOne();
                  md.ActionTwo();
                  //Some other actions...
              }).start()
      

      【讨论】:

        【解决方案6】:

        您不能为对象本身创建线程。您可以传递要在线程上调用的委托实例。

        喜欢MSDN

        线程的功能不包含对象。

        【讨论】:

          【解决方案7】:

          我建议使用 ThreadPool.QueueUserWorkItem 以便应用程序可以管理线程,并确保 memberdetail 继承自对象。

          http://msdn.microsoft.com/en-us/library/4yd16hza.aspx

             memberdetail md = new memberdetail(arg0, arg1, arg3)
             ThreadPool.QueueUserWorkItem(DoWork, md);
          
              private void DoWork(object state)
              {
                 if (state is memberdetail)
                 {
                   var md= state as memberdetail;    
                   //DO something with md
                 }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-06-24
            • 1970-01-01
            • 2023-03-20
            • 2014-04-09
            • 2019-09-11
            • 1970-01-01
            • 2020-01-19
            相关资源
            最近更新 更多