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

时间:2014-11-25 06:29:44

标签: c# wcf events

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

  

在窗口之前无法在控件上调用Invoke或BeginInvoke   句柄已创建.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();
    }
}

1 个答案:

答案 0 :(得分: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可能导致您获得的异常