2012-05-28 155 views
1

我正在嘗試製作Windows窗體應用程序。該應用程序使用多線程,每個線程調用一個方法,並更新在主線程上創建的控件。我使用invoke更新控件,該應用程序在Windows服務器企業版上運行,但它在Windows 7 64位上運行。在WIndows 7上,應用程序在更新接口2次後會停止執行任何操作。我不知道什麼似乎是問題。我嘗試了多個線程和任務(Task.Factory.StartNew()),我有相同的結果(更新控制2次)。沒有錯誤信息。 謝謝。多線程應用程序不能在Windows 7上工作

編輯:CallMethod()我打電話給一個WCF並等待迴應。看來WCF調用返回的東西前兩個線程,它不是休息...

代碼:

主要方法:

  for (int i = 0; i < NoThreads; i++) 
      { 
       int index = i; 
       Thread t = new Thread(CallMethod); 
       t.Name = "Thread [" + Cicle + "] Cicle [" + i + "]"; 
       threads[i] = t; 

      } 
      for (int i = 0; i < NoThreads; i++) 
      { 
       threads[i].Start(); 
      } 

CallMethod:

private string CallMethod() 
{ 
try 
    { 
     //calling a webservice 

     string message = ..... 
     if (txtResult.InvokeRequired) 
     { txtResult.Invoke((MethodInvoker)(() => txtResult.AppendText(message))); } 
catch 
{throw;} 

} 
+0

假設txtResult.InvokeRequired後的連接是假的......什麼也沒有發生 – spender

回答

-1

在x64機器上運行的示例(Windows 7):

class Server 
{ 
    private TcpListener tcpListener; 
    private Thread listenThread; 
    private static string rootPath = ""; 
    public Server() 
    { 
     IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0]; 
     this.tcpListener = new TcpListener(ipAddress, 9501); 
     //this.tcpListener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint); 
     this.listenThread = new Thread(new ThreadStart(ListenForClients)); 
     Trace.WriteLine("Server Working", "Information"); 
     this.listenThread.Start(); 
    } 


    private void ListenForClients() 
    { 
     this.tcpListener.Start(); 
     Trace.WriteLine("Server TcpListener Started", "Information"); 
     while (true) 
     { 
      //blocks until a client has connected to the server 
      TcpClient client = this.tcpListener.AcceptTcpClient(); 
      Trace.WriteLine("Server TcpListener New Client", "Information"); 
      // create a thread to handle the client 
      //ParameterizedThreadStart HandleClientConn; 
      Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
      clientThread.Start(client); 
     } 
    } 

    private void HandleClientComm(object client) 
    { 
     Trace.WriteLine("Server TcpListener New Client Handle Thread", "Information"); 
     TcpClient tcpClient = (TcpClient)client; 
     NetworkStream nStream = tcpClient.GetStream(); 
     Image img = Image.FromStream(nStream); 
     Trace.WriteLine("Server TcpListener Image is received", "Information"); 
     string imageName = client.GetHashCode() + ".jpg"; 
     string imagePath = Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + @"\", @"approot\"+ imageName); 
     img.Save(imagePath, ImageFormat.Jpeg); 
     rootPath = Environment.GetEnvironmentVariable("RoleRoot"); 
     Dictionary<string, string> templates = GetTemplates(); 
     string sDataDir = String.Format("{0}StasmData\\", rootPath); 
     StasmHelper stasm = new StasmHelper(); 
     Face retVal = stasm.GetHumanFace(imagePath, sDataDir, templates); 

     File.Delete(imagePath); 
    } 
+0

這並不是一個例子。這太愚蠢了。 – spender

+0

爲什麼要花錢?該代碼中有線程的用法。 –

+0

幾乎每個大型應用程序都有線程。你可以粘貼任何這些並聲稱它是一個答案......除非它不回答這個具體問題。在最簡單的情況下,線程代碼的例子可能是大約5行左右。即使這不會回答這個問題,雖然 – spender

0

爲了進行調試,確保所有通過CallMethod()的路徑更新UI(即使它只是帶有「到達此點」文本)。看起來txtResult.InvokeRequired可能是錯誤的,或者您可能從Web請求中獲得異常。

0

問題就解決了 我不得不關閉調用WCF中的每個線程

相關問題