類的成員怎麼能夠使用類本身呢?
1、http://www.cnblogs.com/danielWise/archive/2012/02/10/2346117.html
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
2、https://msdn.microsoft.com/zh-cn/library/vstudio/ms171728(v=vs.100).aspx
class NETMutex
{
static Mutex myMutex;
public static void Main()
{
myMutex = new Mutex(true, "WROX");
NETMutex nm = new NETMutex();
Thread t = new Thread(new ThreadStart(nm.Run));
t.Start();
Console.WriteLine("Thread Sleep for 5 sec");
Thread.Sleep(5000);
Console.WriteLine("Thread Woke Up");
myMutex.ReleaseMutex();
Console.WriteLine("Before WaitOne");
myMutex.WaitOne();
Console.WriteLine("Lock owned by Main Thread");
Console.ReadLine();
}
經驗證,第一個示例是一個死循環。進行以下改造後測試通過!
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(UpdateResult);
this.Invoke(d, new object[] { text });
}
else
{
UpdateResult(text);
//this.textBox1.Text = text;//直接這樣也可以
}
}
private void UpdateResult(string text)
{
this.textBox1.Text = text;
}
雖然問題解決了,但感覺這樣的邏輯好啰嗦的有木有?