110,572
社区成员
发帖
与我相关
我的任务
分享
namespace ThreadTest
{
public class MoreTime
{
public void WaitMoreTime()
{
for (int i ; i<500;i++)
{
DoSomething()
Thread.sleep(5000)
}
}
}
}
namespace ThreadTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MoreTime mt = new MoreTime();
ThreadStart start = new ThreadStart(mt.WaitMoreTime);
Thread thread = new Thread(start);
thread.Start();
}
}
}
// This event handler creates a thread that calls a
// Windows Forms control in a thread-safe way.
private void setTextSafeBtn_Click(
object sender,
EventArgs e)
{
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));
this.demoThread.Start();
}
// This method is executed on the worker thread and makes
// a thread-safe call on the TextBox control.
private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
}
// This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.
private 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.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
namespace ThreadTest
{
public class MoreTime
{
public delegate void InvokeOtherThead(int i);
public InvokeOtherThead MainThread;
public void WaitMoreTime()
{
for (int i= 0 ; i<20;i++)
{
Thread.Sleep(2000);
MainThread(i);
}
}
}
}
namespace ThreadTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MoreTime mt = new MoreTime();
mt.MainThread = new MoreTime.InvokeOtherThead(AddToList);
ThreadStart start = new ThreadStart(mt.WaitMoreTime);
Thread thread = new Thread(start);
thread.Start();
}
public void AddToList(int i)
{
if (this.listBox1.InvokeRequired)
{
MoreTime mt = new MoreTime();
mt.MainThread = new MoreTime.InvokeOtherThead(AddToList);
this.Invoke(mt.MainThread, new object[] { i});
}
else
{
listBox1.Items.Add(i.ToString());
}
}
}
}