程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C# 線程無法開啟窗口的原因

C# 線程無法開啟窗口的原因

編輯:關於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();
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved