Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c创建线程#_C#_Multithreading - Fatal编程技术网

C# 使用c创建线程#

C# 使用c创建线程#,c#,multithreading,C#,Multithreading,如果我创建了一个像这样的对象 memberdetail md = new memberdetail(arg0, arg1, arg3) 如何为md对象创建线程 Thread t = new Thread((md)); t.Start(); 它不起作用。Thx您不会在对象上启动线程,而是启动一个方法: 必须将对象的方法传递给线程的构造函数,如下所示 Thread t = new Thread(md.SomeFn); t.Start(); 不能为对象本身创建线程。您可以传递要在线程上调用的委托

如果我创建了一个像这样的对象

memberdetail md = new memberdetail(arg0, arg1, arg3)
如何为md对象创建线程

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

它不起作用。Thx

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


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

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

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

喜欢上了


线程不包含函数,而是包含对象。

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

  • 您不会为对象创建线程。线程是分开的

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


  • 如果您只想在不同的线程中创建多个对象,请尝试此操作

    for(int i = 0, i < numThreads; i++)
        (new Thread( () => 
            { memberdetail md = new memberdetail(arg0, arg1, arg3) }).start()
    
    for(inti=0,i
    {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()
    
    for(inti=0,i
    {
    memberdetail md=新的memberdetail(arg0、arg1、arg3);
    md.ActionOne();
    md.ActionTwo();
    //其他一些行动。。。
    }).start()
    
    如果要在开始时将对象传递到线程中,请执行以下操作:

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

    有关详细信息,请参阅。

    在此场景中,您需要将线程指向md实例中的方法。ThreadStart需要指向方法,而不是“对象”。是否尝试将md作为参数传递给线程?如果是,则需要使用参数化的ThreadStart。您不能为对象创建线程,而是要定义任务(方法)你想在单独的线程中运行。你可以用一个特定的对象方法启动一个线程,但是线程和对象之间没有任何联系。是的!说得对。更新了我的答案,删除了这个隐含的语句。对于第3点,线程池通常有显著的优势,可以手动滚动。有人会看一下这个问题吗如果你知道什么,请给我一些建议
    for(int i = 0, i < numThreads; i++)
        (new Thread( () => 
            {
                memberdetail md = new memberdetail(arg0, arg1, arg3);
                md.ActionOne();
                md.ActionTwo();
                //Some other actions...
            }).start()
    
    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
            }
        }
    }