如何在线程函数中通过引用调用 [英] How can call by reference in a thread function

查看:126
本文介绍了如何在线程函数中通过引用调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 如何通过线程函数中的引用进行调用
例如在以下代码中,我需要一个将其值更改为线程的字符串变量,然后使用button1_Click函数

Hi How can call by reference in a thread function
for example in follow code I need a string variable that Change its value into thread and then I use the button1_Click function

struct Customer
{
    public int Id { get; set; }
}
    public  void thrad(object parm)
    {
        Customer customer = (Customer)parm;


    }
    private void button1_Click(object sender, EventArgs e)
    {

        Customer customer1 = new Customer() { Id = 1 };
        Thread t1 = new Thread(new ParameterizedThreadStart(Showcustomer));
        Thread barberT = new Thread(barber);
        t1.Start(customer1);



    }

推荐答案

不要使用参数化线程!
我的线程包装器完全解决了这个问题:

如何将ref参数传递给线程 [ ^ ].

我在参考资料中提供了非常详细的说明.

—SA
Don''t use parametrized thread!
My thread wrapper solves the problem completely:

How to pass ref parameter to the thread[^].

I provide quite detailed explanation in my reference.

—SA


我认为您不必担心,但是您可以尝试使用静态变量...

I don''t think you need to worry about it, but you could try using a static variable...

public class MyClass
{
    struct Customer
    {
        int id;
    }

    private static Customer cust = null;

    private buttonClick(...)
    {
        cust = new Customer();
        ...
    }
}


这篇关于如何在线程函数中通过引用调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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