在 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();
}
}