日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 线程无法开启窗口的原因 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在 C# 里面, 主窗口擁有主線程, 主線程產(chǎn)生子線程監(jiān)控 Socket 埠, 子線程一收到數(shù)據(jù)流就會給主線程發(fā)送一個事件, 創(chuàng)建一個窗口. 現(xiàn)在的情況是子線程能夠收到數(shù)據(jù)流, 主窗口能夠收到子線程發(fā)送過來的事件, 能夠創(chuàng)建一個窗口. 這個窗口有問題: 窗口狀態(tài)像死掉程序的窗口一樣, 反白的.
開發(fā)碰到很棘手的問題, 尋找解決方法. 品味程序出錯過程, 逐步跟蹤程序執(zhí)行過程, 每一行代碼每一條語句全部執(zhí)行, 怪了, 大白天碰到鬼了. 主窗口加入一個按鈕, 按鈕的作用就是執(zhí)行主窗口的事件, 啟動程序, 點擊按鈕, 程序正確創(chuàng)建一個窗口, 按照這個測試結(jié)果來看, 事件處理中的代碼沒有任何問題. 在執(zhí)行程序, 跟蹤, 尋找出錯的過程. 我覺得程序沒有問題, 不應該出現(xiàn)錯誤; 但是真的出錯了, 說明程序一定有問題, 問題是什么呢, 沒有答案; 想起以前高人語錄: 計算器程序設計就是這么簡單, 別管教授專家高手, 寫程序出來到計算器上面一跑就知道誰的程序正確, 是騾子是馬需要牽出來溜溜. 呀, 找不到答案, 轉(zhuǎn)而上網(wǎng), 到論壇盡量尋找這種錯誤相關信息, 時間浪費很多, 結(jié)果不是很好, 沒有找到答案. 之后和 faust 聊天, 詢問這種問題, 他指出一定是訊息回圈和線程之間交互這兩個問題中的一個. 順著 faust 的思路到論壇尋找答案, 很快找到相關訊息.
揭曉最終解決答案, 事件是一個同步處理過程, 就是說雖然子線程觸發(fā)主窗口事件, 可是執(zhí)行的線程仍然是子線程, 創(chuàng)建一個窗口 From frm1 = new Form(); Form.Show(); 能夠執(zhí)行, 可是無法收到 Windows Print() 事件, 所以窗口創(chuàng)建沒有問題, 就是沒有畫出窗口上面的東東, 所以窗口像死掉的窗口一樣, 反白的. 找到原因怎么處理問題呢? 在線程里面使用 delegateDefine delegateTest = new delegateDefine(this.m_from.eventFunction); this.m_from.Invoke(delegateTest); 就能正常執(zhí)行程序了. 解決里面最重要的是 Invoke, 如果有興趣可以看看 Invoke 的介紹.
從問題出現(xiàn)到問題搞定, 花費十個小時, 太辛苦了.


附: 異步委派程序設計范例
下列程序代碼示范 .NET 異步程序設計的用法,使用簡單類別將一些數(shù)字因子分解。
[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();
}
}??

總結(jié)

以上是生活随笔為你收集整理的C# 线程无法开启窗口的原因的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。