Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);



    }
Posted

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
 
Share this answer
 
Comments
Espen Harlinn 14-Apr-11 17:25pm    
Good effort, 5ed!
Sergey Alexandrovich Kryukov 14-Apr-11 19:10pm    
Thank you, Espen.
I think parametrized thread which requires type case is one of the .NET nonsenses. Thanks goodness the work around is so clean and straightforward.
--SA
I don't think you need to worry about it, but you could try using a static variable...

C#
public class MyClass
{
    struct Customer
    {
        int id;
    }

    private static Customer cust = null;

    private buttonClick(...)
    {
        cust = new Customer();
        ...
    }
}
 
Share this answer
 
v2
Comments
esmailian 14-Apr-11 15:10pm    
When I'm defining a static variable inside the string where I am going to tell the problem is
I have this variable to a string to a label like. Each thread was carried Vqt label is changed so much.
Sergey Alexandrovich Kryukov 14-Apr-11 16:56pm    
There is a comprehensive solution!
Please see my Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900