多线程的多种写法实现
int testFunc(string str)
{
MessageBox.Show(str);
return 1;
}
void test()
{
this.lblMessage.Text = "111";
}
private void Form1_Load(object sender, EventArgs e)
{
//ThreadPool.QueueUserWorkItem(new WaitCallback(x =>
//{
// Func<string, int> func = new Func<string, int>(this.testFunc);
// this.Invoke(func, "fuck");
// this.Invoke(new Action(this.test));
// Thread.Sleep(5000);
// MessageBox.Show("YYYY");
//}));
//MessageBox.Show("XXXX");
//DoWithEasy();
//DoWithParameter();
//DoWithTimer();
//DoWithThreadPool();
//DoWithThreadPoolParameter();
//DoWithAnonymous();
//DoWithLambda();
//DoWithCommon();
//DoWithAction();
//DoWithFunc();
//DoWithPredicate();
}
#region A simple Thread
private void DoWithEasy()
{
Thread t = new Thread(new ThreadStart(this.DoSomethingWithEasy));
t.Start();
}
private void DoSomethingWithEasy()
{
MessageBox.Show("Knights Warrior");
}
#endregion
#region A simple Thread with parameter
private void DoWithParameter()
{
Thread t = new Thread(new ParameterizedThreadStart(this.DoSomethingWithParameter));
t.Start("Knights Warrior");
}
private void DoSomethingWithParameter(object x)
{
MessageBox.Show(x.ToString());
}
#endregion
#region Thread with Timer
public void DoWithTimer()
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += (x, y) =>
{
MessageBox.Show("Knights Warrior");
};
timer.Start();
}
#endregion
#region ThreadPool with no parameter
private void DoWithThreadPool()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomethingWithThreadPoolNO));
}
private void DoSomethingWithThreadPoolNO(object x)
{
MessageBox.Show("Knights Warrior");
}
#endregion
#region ThreadPool with parameter
private void DoWithThreadPoolParameter()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomethingWithThreadPoolParameter), "Knights Warrior");
}
private void DoSomethingWithThreadPoolParameter(object x)
{
MessageBox.Show(x.ToString());
}
#endregion
#region ThreadPool with Anonymous delegate
private void DoWithAnonymous()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object x)
{
MessageBox.Show("Knights Warrior");
}));
}
#endregion
#region ThreadPool with lambda
private void DoWithLambda()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(x =>
{
MessageBox.Show("Knights Warrior");
}));
}
#endregion
#region Thread change UI
private void DoWithCommon()
{
WaitCallback waitCallBack = new WaitCallback(this.InvokeMethod);
ThreadPool.QueueUserWorkItem(waitCallBack, "Knights Warrior");
}
private delegate void InvokeMethodDelegate(string name);
private void InvokeMethod(object x)
{
this.Invoke(new InvokeMethodDelegate(this.ChangeUIWithCommon), x.ToString());
}
private void ChangeUIWithCommon(string name)
{
this.lblMessage.Text = name;
}
#endregion
#region Thread change UI with Action
private void DoWithAction()
{
WaitCallback waitCallback = new WaitCallback(this.DoSomethingWithAction);
ThreadPool.QueueUserWorkItem(waitCallback, "Knights Warrior");
}
private void DoSomethingWithAction(object x)
{
this.Invoke(new Action<string>(this.ChangeUI), x.ToString());
}
private void ChangeUI(string message)
{
this.lblMessage.Text = message;
}
#endregion
#region Thread change UI with Func
private void DoWithFunc()
{
WaitCallback waitCallback = new WaitCallback(this.DoSomethingWithFunc);
ThreadPool.QueueUserWorkItem(waitCallback, "Knights Warrior");
}
private void DoSomethingWithFunc(object x)
{
Func<string, int> f = new Func<string, int>(this.GetFuncMessage);
object result = this.Invoke(f, x.ToString());
MessageBox.Show(result.ToString());
}
private int GetFuncMessage(string message)
{
this.lblMessage.Text = message;
if (message == "Knights Warrior")
{
return 1;
}
else
{
return 0;
}
}
#endregion
#region Thread change UI with Predicate
private void DoWithPredicate()
{
WaitCallback waitCallback = new WaitCallback(this.DoSomethingWithPredicate);
ThreadPool.QueueUserWorkItem(waitCallback, "Knights Warrior");
}
private void DoSomethingWithPredicate(object x)
{
Predicate<string> pd = new Predicate<string>(this.GetPredicateMessage);
object result = this.Invoke(pd, x);
MessageBox.Show(result.ToString());
}
private bool GetPredicateMessage(string message)
{
this.lblMessage.Text = message;
if (message == "Knights Warrior")
{
return true;
}
else
{
return false;
}
}
#endregion