简体   繁体   English

使用线程发送数据

[英]Send data using threads

I need to know how to send data over my threads, I have this code. 我需要知道如何通过线程发送数据,我有这段代码。

            new Thread(BattleArena.ArenaGame(12)).Start();

And over BattleArena class I have 在BattleArena课上,我有

public static void ArenaGame(int test)
    {
        while (true)
        {

            Console.WriteLine(test);

            Thread.Sleep(400);
        }
    }

But that is not a valid way... 但这不是有效的方法...

You need to use parameterised threads. 您需要使用参数化的线程。 Like 喜欢

 ThreadStart start = () => {     BattleArena.ArenaGame(12);  };

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

Or 要么

 Thread newThread = new Thread(BattleArena.ArenaGame);
 newThread.Start(12);

then change this method as it only takes object as parameter as ThreadStart is not a generic delegate 然后更改此方法,因为它仅将对象作为参数,因为ThreadStart不是通用委托

public static void ArenaGame(object value)
{
    int test = (int)value;
    while (true)
    {

        Console.WriteLine(test);

        Thread.Sleep(400);
    }
}

Right now you are "sending" the result of a method call. 现在,您正在“发送”方法调用的结果。 (Not even compilable). (甚至无法编译)。 You want to send/execute a function: 您要发送/执行一个函数:

new Thread(() => BattleArena.ArenaGame(12)).Start();

Don't use parameterized threads, they are obsolete thanks to lambdas. 不要使用参数化线程,这要归功于lambdas。

To clarify: a thread is not a way to send data. 需要说明的是:线程不是发送数据的方法。 It is a way to execute a function. 这是执行功能的一种方式。 The the function has to contain the data. 该功能必须包含数据。

您应该使用参数化ThreadStart

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

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