在创建窗口句柄之前,无法在控件上调用调用 Invoke 或 BeginInvoke.C# 获胜形式



嗨,我想在浏览器中输入一个链接时更改标签。我创建了 wcf 服务,并使用一个线程来更改主窗体中的标签。现在,当我在浏览器中单击url时 http://:5001/Connect 出现此错误是有原因的。我不明白这里的错误。

窗口出现之前,无法在控件上调用调用 Invoke 或 BeginInvoke 已创建句柄。无效操作异常未处理

必须创建对象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 "Έχετε συνδεθεί επιτυχώς με την εφαρμογή του ταμείου";
        }
    }
}

IWCFS服务.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();
    }
}
尝试稍后

将服务的启动时间:

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();
    }
 }

您得到的异常是 i 因为您正在窗体构造函数中启动 WCF 服务。此时,表单的句柄尚未创建 - 因此调用Connect可能会导致您获得的异常

最新更新