浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

C# 线程无法打开窗口的因素

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


相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...