如何在c#中将数据传递给运行时的线程 [英] How to pass data to a thread on runtime in c#

查看:178
本文介绍了如何在c#中将数据传递给运行时的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能:



I have a function :

static void fun1(string req)
      {
          while (true)
          {
              if (req == "read")
                  Console.WriteLine(req);
              else
                  Console.WriteLine(req);

          }
      }





这个函数我想在运行时传递一个参数。这个函数将用一个线程映射。



- 这意味着我将数据传递给线程。

- 我想要尝试一个场景......关于用户输入我想在运行时将一个参数传递给线程。

- 如何实现这一点?我试过这样的事情:





线程t =新线程(()=> fun1(你好));



- 我用lambda表达式来传递数据。

- 现在我将启动线程:

t.Start();

- 此线程现在在后台运行。现在在main函数中我将要求用户输入一些数据,让我们说嘿。

- 所以现在后台的线程应该在用户输入的控制台上打印Hey。那么如何实现呢?

- 比如在主要我说这样的话:





console.writeline(输入一些东西);

字符串数据= console.readline();



/////请注意线程是在后台运行///////

/////现在如何将数据传递给线程????



- 我创建了一个类的空构造函数并启动了线程。

- 在主要实例化构造函数。

- 现在程序挂起。 />




谢谢

请指导。



to this function i want to pass an argument on runtime. This function will be mapped with a thread.

- This means that i will be passing data to the thread.
- I want to try out a scenario... on user input i want to pass an argument to the thread during runtime.
- How to achieve this? I tried out something like this:


Thread t = new Thread(() => fun1("hello"));

- I have used a lambda expression to pass data.
- Now i will start the thread:
t.Start();
- This thread now runs in the background. Now in the main function i will ask the user to enter some data lets say "Hey".
- So now the thread in the background should print "Hey" on the console on user input. So how to achieve this?
- Say for example in the main i say something like this :


console.writeline("Enter some thing");
string data = console.readline();

/////please note that the thread is running in the background///////
/////Now how to pass the data to the thread????

- I created an empty constructor of the class and started the thread.
- In the main i instantiate the constructor.
- Now the program hangs.


Thanks
Please guide.

推荐答案

这比你想象的要困难得多。

为什么?

部分是因为你为你的线程显示的代码非常讨厌:它会在那里输出新行到COnsole尽可能快,并导致您的应用程序花费大部分时间分配内存并滚动显示...因此为什么您的程序h angs!



但主要是因为在线程之间获取数据充满了问题,因为therad是独立的进程,你不知道他们在任何时候做了什么另一个线程:所以如果你忙于在一个线程中将一些东西加载到一个变量中,你就不知道另一个线程在移除它的过程中没有一半。只是一个简单的事情,比如在一个线程中计算数字并在另一个线程中减少它们就会成为可怕的,可怕的错误的来源。



如果你真的想这样做,那么您需要首先开始设计线程安全数据传输,但最简单的方法是参与锁定语句 [ ^ ]。
That gets a lot harder than you think.
Why?
Partly because the code you show for your thread is really nasty: it will sit there outputting new lines to the COnsole as fast as it can, and causing your app to spend most of it's time allocating memory and scrolling the display...hence why your program hangs!

But mostly, because getting data between threads is fraught with problems, because therad are independent processes and you don't know what they are doing at any time from another thread: so if you are busy loading something into a variable in one thread, you don't know that the other is not half way through removing it. Just a simple thing like counting numbers up in one thread and reducing them in another becomes a source of horrible, horrible bugs.

If you really want to do this, then you need to start reading up first on designing for thread safe data transfer, but the simplest way is to involve the lock statement[^].


这篇关于如何在c#中将数据传递给运行时的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆