您好我想更改標籤,當我在我的瀏覽器中鍵入一個鏈接。我創建了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();
}
}
什麼*確切*是您正在獲取的錯誤(以及您在哪裏獲得它)? – 2014-11-25 06:34:48
調用或BeginInvoke無法在控件上調用,直到窗口句柄已創建.InvalidOperationException未處理。在此代碼中:this.Invoke((MethodInvoker)委託人 {0}} {lbl_connectClients.Text =「ASDASDASD」; }); – Achill 2014-11-25 06:47:47
爲什麼我得到-1?因爲我不明白必須做什麼?我做了什麼,說第一個答案,仍然不工作 – Achill 2014-11-25 09:05:56