在 c# 里面, 主窗口擁有主線程, 主線程產生子線程監控 socket 埠, 子線程一收到數據流就會給主線程發送一個事件, 創建一個窗口. 現在的情況是子線程能夠收到數據流, 主窗口能夠收到子線程發送過來的事件, 能夠創建一個窗口. 這個窗口有問題: 窗口狀態像死掉程序的窗口一樣, 反白的.
開發碰到很棘手的問題, 尋找解決方法. 品味程序出錯過程, 逐步跟蹤程序執行過程, 每一行代碼每一條語句全部執行, 怪了, 大白天碰到鬼了. 主窗口加入一個按鈕, 按鈕的作用就是執行主窗口的事件, 啟動程序, 點擊按鈕, 程序正確創建一個窗口, 按照這個測試結果來看, 事件處理中的代碼沒有任何問題. 在執行程序, 跟蹤, 尋找出錯的過程. 我覺得程序沒有問題, 不應該出現錯誤; 但是真的出錯了, 說明程序一定有問題, 問題是什么呢, 沒有答案; 想起以前高人語錄: 計算器程序設計就是這么簡單, 別管教授專家高手, 寫程序出來到計算器上面一跑就知道誰的程序正確, 是騾子是馬需要牽出來溜溜. 呀, 找不到答案, 轉而上網, 到論壇盡量尋找這種錯誤相關信息, 時間浪費很多, 結果不是很好, 沒有找到答案. 之后和 faust 聊天, 詢問這種問題, 他指出一定是訊息回圈和線程之間交互這兩個問題中的一個. 順著 faust 的思路到論壇尋找答案, 很快找到相關訊息.
揭曉最終解決答案, 事件是一個同步處理過程, 就是說雖然子線程觸發主窗口事件, 可是執行的線程仍然是子線程, 創建一個窗口 from frm1 = new form(); form.show(); 能夠執行, 可是無法收到 windows print() 事件, 所以窗口創建沒有問題, 就是沒有畫出窗口上面的東東, 所以窗口像死掉的窗口一樣, 反白的. 找到原因怎么處理問題呢? 在線程里面使用 delegatedefine delegatetest = new delegatedefine(this.m_from.eventfunction); this.m_from.invoke(delegatetest); 就能正常執行程序了. 解決里面最重要的是 invoke, 如果有興趣可以看看 invoke 的介紹.
從問題出現到問題搞定, 花費十個小時, 太辛苦了.
附: 異步委派程序設計范例
下列程序代碼示范 .net 異步程序設計的用法,使用簡單類別將一些數字因子分解。
[c#]
using system;
using system.threading;
using system.runtime.remoting;
using system.runtime.remoting.messaging;
// create an asynchronous delegate.
public delegate bool factorizingasyncdelegate (
int factorizablenum,
ref int primefactor1,
ref int primefactor2);
// create a class that factorizers the number.
public class primefactorizer
{
public bool factorize(
int factorizablenum,
ref int primefactor1,
ref int primefactor2)
{
primefactor1 = 1;
primefactor2 = factorizablenum;
// factorize using a low-tech approach.
for (int i=2;i<factorizablenum;i++)
{
if (0 == (factorizablenum % i))
{
primefactor1 = i;
primefactor2 = factorizablenum / i;
break;
}
}
if (1 == primefactor1 )
return false;
else
return true ;
}
}
// class that receives a callback when the results are available.
public class processfactorizednumber
{
private int _ulnumber;
public processfactorizednumber(int number)
{
_ulnumber = number;
}
// note that the qualifier is one-way.
[onewayattribute()]
public void factorizedresults(iasyncresult ar)
{
int factor1=0, factor2=0;
// extract the delegate from the asyncresult.
factorizingasyncdelegate fd = (factorizingasyncdelegate)((asyncresult)ar).asyncdelegate;
// obtain the result.
fd.endinvoke(ref factor1, ref factor2, ar);
// output the results.
console.writeline("on callback: factors of {0} : {1} {2}",
_ulnumber, factor1, factor2);
}
}
// class that shows variations of using asynchronous
public class simple
{
// the following demonstrates the asynchronous pattern using a callback.
public void factorizenumber1()
{
// the following is the client code.
primefactorizer pf = new primefactorizer();
factorizingasyncdelegate fd = new factorizingasyncdelegate (pf.factorize);
int factorizablenum = 1000589023, temp=0;
// create an instance of the class that is going
// to be called when the call completes.
processfactorizednumber fc = new processfactorizednumber(factorizablenum);
// define the asynccallback delegate.
asynccallback cb = new asynccallback(fc.factorizedresults);
// you can use any object as the state object.
object state = new object();
// asynchronously invoke the factorize method on pf.
iasyncresult ar = fd.begininvoke(
factorizablenum,
ref temp,
ref temp,
cb,
state);
//
// do some other useful work.
//. . .
}
// the following demonstrates the asynchronous pattern using a begininvoke, followed by waiting with a time-out.
public void factorizenumber2()
{
// the following is the client code.
primefactorizer pf = new primefactorizer();
factorizingasyncdelegate fd = new factorizingasyncdelegate (pf.factorize);
int factorizablenum = 1000589023, temp=0;
// create an instance of the class that is going
// to be called when the call completes.
processfactorizednumber fc = new processfactorizednumber(factorizablenum);
// define the asynccallback delegate.
asynccallback cb =
new asynccallback(fc.factorizedresults);
// you can use any object as the state object.
object state = new object();
// asynchronously invoke the factorize method on pf.
iasyncresult ar = fd.begininvoke(
factorizablenum,
ref temp,
ref temp,
null,
null);
ar.asyncwaithandle.waitone(10000, false);
if (ar.iscompleted)
{
int factor1=0, factor2=0;
// obtain the result.
fd.endinvoke(ref factor1, ref factor2, ar);
// output the results.
console.writeline("sequential : factors of {0} : {1} {2}",
factorizablenum, factor1, factor2);
}
}
public static void main(string[] args)
{
simple simple = new simple();
simple.factorizenumber1();
simple.factorizenumber2();
}
}