您的位置:首页 > 编程语言 > C#

C# 线程无法开启窗口的原因

2004-04-15 16:08 621 查看
時間: 2004/4/14
作者: Robert
參考: MSDN
電郵: zsc771120@yahoo.com.cn
關鍵字: 线程 Form 开启 窗口 Show Thread Invoke
目的: 幫助受 線程開啟窗口 出錯困擾的人

在 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();
}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: