如何在 C# 中将变量传递到线程中?

发布于 2024-12-09 07:25:33 字数 222 浏览 0 评论 0原文

在我的代码中,我正在运行一个线程。我现在需要做的是将变量传递给“SayHello”方法。由于它在单独的线程中调用,因此我的变量对该线程不可见。

ThreadStart ts = new ThreadStart(SayHello);
mThread = new Thread(ts);
mThread.Start();

我是 C# 新手,请让我知道如何执行此操作。

In my code im running a thread. What I need to do now is to pass a variable to "SayHello" method. Since it is calling in a separate thread my variables are not visible for the thread.

ThreadStart ts = new ThreadStart(SayHello);
mThread = new Thread(ts);
mThread.Start();

Im new to C# and please let me know how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-12-16 07:25:33

这就是我传递价值观的方式。首先,您的参数必须是一个对象,因此您需要在方法中进行强制转换。你的方法必须返回void,如果你需要返回值,你可以通过多种方式来实现,易失性变量,或者线程通信等。 然后:

string sParameters = "This is my parameter";
Thread thrProcess = new Thread(MyMethod);
thrProcess.IsBackgroud = true;   // only if needed
thrProcess.Start(sParameters);   // string derives from object

并且在你的方法中:

void MyMethod(object param)
{
    string sParameterValue = (string)param;
// Now you can work with sParameterValue
}

如果你需要传递多个变量,那么创建一个class 并将您的值分配给属性,并在您的方法中进行转换,但还有其他方法可以对此进行归档。希望这有帮助。如果您需要更多信息,请检查此
链接: C# 中的线程

祝你好运!

this is how I pass values. First, your parameter must be a object, so you need to cast in your method. Your method must return void, if you need to return values, you can do it in many ways, volatile variables, or thread communication, etc. Then:

string sParameters = "This is my parameter";
Thread thrProcess = new Thread(MyMethod);
thrProcess.IsBackgroud = true;   // only if needed
thrProcess.Start(sParameters);   // string derives from object

And in your method:

void MyMethod(object param)
{
    string sParameterValue = (string)param;
// Now you can work with sParameterValue
}

If you need to pass more than one variable, then create a class and assign your values to properties, and cast in your method, but there are other ways to archive this. Hope this helps. If you need more info, chech this
link: Threading in C#

Good luck!

回心转意 2024-12-16 07:25:33

你只能穿过一个物体。

//Declaring the Thread    
Thread T1;

// Calling the Thread
object[] threadPass = { String1, String2, Int1 };
T1 = new thread(Threadvoid);
T1.Start(threadPass);

// Thread Void
void Threadvoid(object passedObject)
{
    //Take the variables back out of the object
    object[] ThreadPass = passedObject as object[];
    string String1 = ThreadPass[0].ToString();
    string String2 = ThreadPass[1].ToString();
    int Int1 = Convert.ToInt32(ThreadPass[2]);
}

You can only pass through an object.

//Declaring the Thread    
Thread T1;

// Calling the Thread
object[] threadPass = { String1, String2, Int1 };
T1 = new thread(Threadvoid);
T1.Start(threadPass);

// Thread Void
void Threadvoid(object passedObject)
{
    //Take the variables back out of the object
    object[] ThreadPass = passedObject as object[];
    string String1 = ThreadPass[0].ToString();
    string String2 = ThreadPass[1].ToString();
    int Int1 = Convert.ToInt32(ThreadPass[2]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文