Thread Examples
6459 단어 example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp
{
/// <summary>
///
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//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("A simple Thread");
}
#endregion
#region A simple Thread with parameter
private void DoWithParameter()
{
Thread t = new Thread(new ParameterizedThreadStart(this.DoSomethingWithParameter));
t.Start("Thread with parameter");
}
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("timer thread.");
};
timer.Start();
}
#endregion
#region ThreadPool with no parameter
private void DoWithThreadPool()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomethingWithThreadPoolNO));
}
private void DoSomethingWithThreadPoolNO(object x)
{
MessageBox.Show("ThreadPool");
}
#endregion
#region ThreadPool with parameter
private void DoWithThreadPoolParameter()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.DoSomethingWithThreadPoolParameter),"ThreadPool Parameter");
}
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("ThreadPool Anonymous delegate.");
}));
}
#endregion
#region ThreadPool with lambda
private void DoWithLambda()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(x =>
{
MessageBox.Show("ThreadPool Lambda");
}));
}
#endregion
#region Thread change UI
private void DoWithCommon()
{
WaitCallback waitcallback = new WaitCallback(this.InvokeMethod);
ThreadPool.QueueUserWorkItem(waitcallback," Thread change UI.");
}
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, "Thread change UI with Action");
}
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,"Test Func");
}
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 == "Test Func")
{
return 1;
}
else
{
return 0;
}
}
#endregion
#region Thread change UI with Predicate
private void DoWithPredicate()
{
WaitCallback waitcallback = new WaitCallback(this.DoSomethingWithPredicate);
ThreadPool.QueueUserWorkItem(waitcallback, "Predicate");
}
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 == "Predicate")
{
return true;
}
else
{
return false;
}
}
#endregion
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간단한 Golang HTTP 서버 예제Go에서 HTTP 서버를 만드는 것은 다음과 같이 간단합니다. http.ListenAndServe(":8222", nil) 여기서 우리는 머신의 8222 포트에서 요청을 수신하는 간단한 서버를 만들었습니다. 이것은 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.