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#_Multithreading_Arguments - Fatal编程技术网

C# 如何将变量传递给另一个线程

C# 如何将变量传递给另一个线程,c#,multithreading,arguments,C#,Multithreading,Arguments,如果您询问如何将参数传递给线程,请参阅以下内容: 我不确定是否正确理解了您的问题,但下面的MSDN文章展示了如何以您正在执行的方式(即通过ThreadStart和委托)将数据传递给线程: 我使用单独的worker类并在构造函数中填充成员变量,然后使用void方法作为使用私有成员变量的委托: using System; using System.Threading; class Test { static void Main() { // To start a

如果您询问如何将参数传递给线程,请参阅以下内容:


我不确定是否正确理解了您的问题,但下面的MSDN文章展示了如何以您正在执行的方式(即通过ThreadStart和委托)将数据传递给线程:


我使用单独的worker类并在构造函数中填充成员变量,然后使用void方法作为使用私有成员变量的委托:

using System;
using System.Threading;

class Test
{
    static void Main() 
    {
        // To start a thread using a static thread procedure, use the
        // class name and method name when you create the ThreadStart
        // delegate. Beginning in version 2.0 of the .NET Framework,
        // it is not necessary to create a delegate explicitly. 
        // Specify the name of the method in the Thread constructor, 
        // and the compiler selects the correct delegate. For example:
        //
        // Thread newThread = new Thread(Work.DoWork);
        //
        ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
        Thread newThread = new Thread(threadDelegate);
        newThread.Start();

        // To start a thread using an instance method for the thread 
        // procedure, use the instance variable and method name when 
        // you create the ThreadStart delegate. Beginning in version
        // 2.0 of the .NET Framework, the explicit delegate is not
        // required.
        //
        Work w = new Work();
        w.Data = 42;
        threadDelegate = new ThreadStart(w.DoMoreWork);
        newThread = new Thread(threadDelegate);
        newThread.Start();
    }
}

class Work 
{
    public static void DoWork() 
    {
        Console.WriteLine("Static thread procedure."); 
    }
    public int Data;
    public void DoMoreWork() 
    {
        Console.WriteLine("Instance thread procedure. Data={0}", Data); 
    }
}

获得向线程传递变量的相同效果的一种方法是,创建一个您希望传递给线程的类型的类范围私有数据成员。在启动线程之前,将该值设置为所需的任何值。如果有多个线程,则需要锁定此类范围的数据成员,以防止出现意外值。或者,您可以使用.NET本机互斥函数来控制对变量的访问

例如(没有对此进行测试,只是在飞行中编写):

如果您有一些数据要在调用序列中“流动”某些数据,也可以使用


我在vb2005中编写的一组方便的类允许轻松创建一个具有一到四个绑定参数和零个或一个未绑定参数的委托。大量的复制/粘贴代码,因为.net不支持可变泛型,但是可以创建一个MethodInvoker,调用foo(bar,boz),方法如下(vb.net语法,但在C#中的方法相同):

方法invoker=InvMaker.NewInv(foo、bar、boz的地址) methodInvoker()调用foo(bar,boz) 它将生成一个对象,其中包含字段Param1作为BarType,Param2作为BozType,以及Action作为Action(属于BarType,BozType)。它将这些字段设置为bar、boz和foo,并返回一个MethodInvoker,该方法将调用doIt,一个调用Action(Param1、Param2)的方法。如果我需要一个动作(整数),我会使用:

theMethodInvoker = InvMaker.NewInv(addressof foo, bar, boz) theMethodInvoker() ' Calls foo(bar,boz) theMethodInvoker=ActionMaker(整数的)。NewInv(foo、bar、boz的地址) 这个方法叫做foo(9,bar,boz)
真滑。lambda避免了剪切粘贴库的需要,但是它们的内部实现是类似的。我已经读到Lambdas导致编辑和继续困难;我知道我的方法没有。

线程有一个重载。Start允许您传入一个参数

theMethodInvoker = ActionMaker(of Integer).NewInv(addressof foo, bar, boz) theMethodInvoker(9) ' Calls foo(9,bar,boz)
哈哈,‘猜问题’嗯,
消息
变量已经被传递到
Write
方法(它将在另一个线程上执行),那么您还想要什么呢?我想这就是答案。就像杰帕迪一样。我想我做错了。因为我忘了启动线程。LOL noobness
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
    private string threadVariable;

    static void Main(string[] args)
    {
        Console.WriteLine("Taking data from Main Thread\n->");
        string message = Console.ReadLine();

        threadVariable = "stuff";

        Thread myThread = new Thread(Write);
        Thread.IsBackground = true;
        Thread.Start();
    }

    public static void Write()
    {
        Console.WriteLine(stuff);
        Console.Read();
    }
}
}
using System;  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 

namespace ConsoleApplication1 
{ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
        Console.WriteLine("Taking data from Main Thread\n->"); 
        string message = Console.ReadLine(); 

        //Put something into the CallContext
        CallContext.LogicalSetData("time", DateTime.Now);

        ThreadStart newThread = new ThreadStart(delegate { Write(message); }); 

        Thread myThread = new Thread(newThread); 

    } 

    public static void Write(string msg) 
    { 
        Console.WriteLine(msg); 
        //Get it back out of the CallContext
        Console.WriteLine(CallContext.LogicalGetData("time"));
        Console.Read(); 
    } 
  } 
} 
theMethodInvoker = InvMaker.NewInv(addressof foo, bar, boz) theMethodInvoker() ' Calls foo(bar,boz) theMethodInvoker = ActionMaker(of Integer).NewInv(addressof foo, bar, boz) theMethodInvoker(9) ' Calls foo(9,bar,boz)
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Taking data from Main Thread\n->");
        string message = Console.ReadLine();  

        Thread myThread = new Thread(Write);
        myThread.Start(message);

    }

    public static void Write(object obj)
    {
        string msg = (string)obj;
        Console.WriteLine(msg);
        Console.Read();
    }
}