2014-11-25 41 views
-1

您好我想更改標籤,當我在我的瀏覽器中鍵入一個鏈接。我創建了wcf服務,並使用一個線程來更改主表單中的標籤。現在,當我點擊瀏覽器http://:5001/Connect中的網址時,出現這種錯誤的原因。我不明白這裏的錯誤。在創建窗口句柄之前,無法在控件上調用Invoke或BeginInvoke。 C#win表格

調用或BeginInvoke無法控制,直到窗口 句柄調用已created.InvalidOperationException了未處理

必須創建對象CashDesk_Form?必須做什麼。這裏是我的代碼:

我的形式

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.ServiceModel; 
using System.Threading; 

namespace tameio 
{ 
    public partial class CashDesk_Form : Form 
    { 
     //Αντικείμενα 
     ServiceHost host; 
     public WCFService wcf; 

     //Μεταβλητές 
     string WCFPort = "5001"; 

     //(ΔΗΜΙΟΥΡΓΟΣ) του Server 
     public CashDesk_Form() 
     { 
      InitializeComponent(); 
      Thread startServerThread = new Thread(StartWCFServer); 
      startServerThread.IsBackground = true; 
      startServerThread.Start(); 
      this.FormClosed += new FormClosedEventHandler(CashDesk_Form_FormClosed); 
     } 

     void CashDesk_Form_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      if (host != null) 
      { 
       try { host.Close(); } 
       catch { } 
       host = null; 
      } 
      else MessageBox.Show("Ο Server είναι ήδη Απενεργοποιημένος"); 
     } 

     public void AddNewConnection() 
     { 
      Thread clientThread = new Thread(new ThreadStart(_AddNewConnection)); 
      clientThread.IsBackground = true; 
      clientThread.Start(); 
     } 
     public void _AddNewConnection() 
     { 
      if (!IsHandleCreated) 
       this.CreateControl(); 

    // ----> Exception here 
      this.Invoke((MethodInvoker)delegate 
      { 
       lbl_connectClients.Text = "ASDASDASD"; 

      }); 

     } 

     //(FUNCTION) - > Εκκίνηση του Server 
     private void StartWCFServer() 
     { 
      if (host == null) 
      { 
       Uri baseAddress = new Uri("http://localhost:" + WCFPort + "/"); 
       host = new ServiceHost(typeof(WCFService), baseAddress); 
       host.AddServiceEndpoint(typeof(IWCFService), new WSHttpBinding(), "Services"); 
       try 
       { 
        host.Open(); 
       } 
       catch (Exception e) 
       { 
        if (e.GetType().FullName.ToString() == "System.InvalidOperationException") return; 
        else 
        { 
         MessageBox.Show("Βεβαιωθείτε ότι έχετε δικαιώματα διαχειριστή σε αυτόν τον υπολογιστή"); 
        } 
       } 
      } 
      else 
      { 
       MessageBox.Show("Υπήρξε πρόβλημα κατά του άνοιγμα του WCF Server. Είτε ο WCF Server είναι Ενεργός, είτε το Port: " + WCFPort + " χρεισιμοποιείτε κάπου αλλού, είτε η IP του δικτύου δεν είναι σωστή"); 
      } 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

     } 
    } 
} 

WCFService.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace tameio 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. 
    public class WCFService : CashDesk_Form, IWCFService 
    { 

     public string connect() 
     { 
      AddNewConnection(); 
      return "Έχετε συνδεθεί επιτυχώς με την εφαρμογή του ταμείου"; 
     } 
    } 
} 

IWCFService.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 
using System.ServiceModel.Web; 
namespace tameio 
{ 
    [ServiceContract] 
    public interface IWCFService 
    { 
     [OperationContract(Name = "SendMessage")] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Bare, 
      UriTemplate = "Connect")] 
      //UriTemplate = "Send?Message={txt}")] 
     string connect(); 
    } 
} 
+0

什麼*確切*是您正在獲取的錯誤(以及您在哪裏獲得它)? – 2014-11-25 06:34:48

+0

調用或BeginInvoke無法在控件上調用,直到窗口句柄已創建.InvalidOperationException未處理。在此代碼中:this.Invoke((MethodInvoker)委託人 {0}} {lbl_connectClients.Text =「ASDASDASD」; }); – Achill 2014-11-25 06:47:47

+0

爲什麼我得到-1?因爲我不明白必須做什麼?我做了什麼,說第一個答案,仍然不工作 – Achill 2014-11-25 09:05:56

回答

0

嘗試和移動服務的啓動到以後的時間:

public partial class CashDesk_Form : Form 
{ 

    public CashDesk_Form() 
    { 
     InitializeComponent(); 
     this.FormClosed += new FormClosedEventHandler(CashDesk_Form_FormClosed); 
    } 

    protected override void OnShown(EventArgs e) 
    { 
     //at this point the handle *is* created 

     base.OnShown(e); 
     Thread startServerThread = new Thread(StartWCFServer); 
     startServerThread.IsBackground = true; 
     startServerThread.Start(); 
    } 
} 

你得到的異常我因爲你正在你的窗體構造函數中啓動WCF服務。此時表單的句柄尚未創建 - 因此調用Connect可能會導致您正在收到的例外

+0

我該如何使用此功能?我不明白。什麼是base.OnShown(); – Achill 2014-11-25 06:51:49

+1

我明白你爲什麼提出這個建議,但是爲什麼這個修復解決了這個問題並不明顯。你需要提供一些解釋。 – Enigmativity 2014-11-25 06:54:49

+0

我想給你解釋。當我點擊瀏覽器中的鏈接時,我想更改標籤。我認爲這可能是由事件或線程造成的。我試圖用事件做出來,但我不能。現在我想用線程,如果我使用MessageBox.Show(「消息」);在內部調用((MethodInvoker)消息顯示正常。但標籤並沒有改變在那一點 – Achill 2014-11-25 07:01:51

相關問題