How to update thread parameter?
string str = "hello world";
private static Thread test = newThread(new ParameterizedThreadStart(invariant_loop));
private void Form1_Load(object sender, EventArgs e)
{
test.Start(str);
}
private static void invariant_loop(object value)
{
do
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(value.ToString());
}
while (true);
}
private void button1_Click(object sender, EventArgs e)
{
str = maskedTextBox1.Text; // update value ?
}
Since
Stringis immutable data structure you can not doing such kind of operations (passing parameters). Use an other reference type or wrap string by a class with appropriate methods which able to modify underlying string field:Define TextWrapper field on the level where you’ve defined
str, then instantiate and pass instance of TextWrapper to a thread.See very good article by Yoda: Parameter passing in C#