public partial class Form1 : Form
{
delegate void SetTextCallback(string text); public void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.label1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.label1.Text = text;
}
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Thread runMyADD = new Thread(new ThreadStart(MyAdd));
runMyADD.Start();
} private void MyAdd()
{
for (int i = 1; i <= 20; i++)
{
SetText(i.ToString());
Thread.Sleep(100);
}
}
}我想实现线程更新主窗体里的label显示的内容,我现在想把MyAdd写成一个单独的类,不知道应该怎么改,请高手指教,谢谢。
{
delegate void SetTextCallback(string text); public void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.label1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.label1.Text = text;
}
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Thread runMyADD = new Thread(new ThreadStart(MyAdd));
runMyADD.Start();
} private void MyAdd()
{
for (int i = 1; i <= 20; i++)
{
SetText(i.ToString());
Thread.Sleep(100);
}
}
}我想实现线程更新主窗体里的label显示的内容,我现在想把MyAdd写成一个单独的类,不知道应该怎么改,请高手指教,谢谢。
{
public void MyAdd()
{
for (int i = 1; i <= 20; i++)
{
SetText(i.ToString());
Thread.Sleep(100);
}
}
}
}
然后
{
Add a = new Add();
Thread runMyADD = new Thread(new ThreadStart(a.MyAdd));
runMyADD.Start();
}
Class1 runMyADD = new Class1();
runMyADD.MyADD(); class Class1
{
public void MyADD()
{
newform.Show();
for (int i = 1; i <= 20; i++)
{
//这里怎么才能获得form1这个对象呢
form1.setlabel = i.ToString();
Thread.Sleep(100);
}
}
}
{
public Form1()
{
InitializeComponent();
}
delegate void DelSetTextCallback(string text);
public void SetTextCallback(string text)
{
this.BeginInvoke(new DelSetTextCallback(SetText), text);
}
public void SetText(string text)
{
this.label1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
ClassA MyAdd = new ClassA();
MyAdd.SetText = new ClassA.SetTextCallback(SetTextCallback); Thread runMyADD = new Thread(new ThreadStart(MyAdd.MyADD));
runMyADD.Start(); }
}
//ClassA
public class ClassA
{
public delegate void SetTextCallback(string text); public SetTextCallback SetText;
public void MyADD()
{
for (int i = 1; i <= 20; i++)
{
SetText(i.ToString()); Thread.Sleep(100);
} }
}