简体   繁体   English

使用c#创建线程

[英]creating thread using c#

If I have a created a object like this 如果我有一个像这样的对象

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

how can I create a thread for the md object? 如何为md对象创建线程?

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

is not working. 不管用。 Thx 谢谢

You don't start a thread on an object , but rather a method : 您不是在对象上启动线程,而是在方法上启动

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

You have to pass the object's method to the Thread's constructor as below, 你必须将对象的方法传递给Thread的构造函数,如下所示,

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

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

If you want to pass the object into the thread on start, do this: 如果要在启动时将对象传递给线程,请执行以下操作:

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
        }
    }
}

See Thread.Start Method (Object) for more details. 有关更多详细信息,请参见Thread.Start方法(对象)

You can't create a thread for objects per se. 您无法为对象本身创建线程。 You can pass an instance of a delegate to be invoked on the thread. 您可以传递要在线程上调用的委托实例。

like on MSDN . 喜欢在MSDN上

Threads do functions not, contain objects. 线程不起作用,包含对象。

  1. You do not create a Thread for an object. 您不为对象创建线程。 Threads are separate. 线程是分开的。

  2. In most cases you should not be using Threads (anymore). 在大多数情况下,您不应该使用Threads(不再)。 Look at ThreadPool and Tasks (TPL library). 查看ThreadPool和Tasks(TPL库)。

Try this if all you want to do is create multiple objects in different threads. 如果你想要做的就是在不同的线程中创建多个对象,请尝试这样做。

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

Any other actions you want to perform you can do inside the body of the lambda eg 您想要执行的任何其他操作都可以在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()

I would recommend using ThreadPool.QueueUserWorkItem so that the application can manage the thread, and make sure memberdetail inherits from object. 我建议使用ThreadPool.QueueUserWorkItem,以便应用程序可以管理线程,并确保memberdetail继承自object。

http://msdn.microsoft.com/en-us/library/4yd16hza.aspx 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
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM