当预期为false时,InvokeRequiredProperty变为true。 [英] InvokeRequiredProperty gets true when expected false.

查看:88
本文介绍了当预期为false时,InvokeRequiredProperty变为true。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个类form1.cs和handleClient.cs和GlobalVariable.cs,form1.cs中有一个方法,它有代码在textbox中显示数据,当从form1.cs本身调用这个方法时,它运行正常但如果在handleClient.cs中调用此方法不起作用。我将要在文本框中显示的数据分配给GloabalVariable类中声明的静态字符串变量somedata。并且数据显示在文本框中,但在关闭连接对象后,可以说关闭客户端的线程。但我想在运行时显示数据,如何使这成为可能?请帮忙。



谢谢。



这是我的代码,


GlobalVariable类




I have 3 classes form1.cs and handleClient.cs and GlobalVariable.cs , There is one method in form1.cs which have code to display data in textbox , when called this method from form1.cs itself, it is working fine but if called in handleClient.cs this method doesnt works. I assign data to be display in textbox to static string variable "somedata" declared in GloabalVariable class. and data is display in textbox but after closing connection objects, lets say closing threads of clients. But i want to display data at runtime, How to make this possible ? Please help.

thanks .

here is my code,

in GlobalVariable class

class GlobalVariable
  {
      public static string SomeData = "";
  }




$ 1 $ b in form1类



in form1 class

namespace connectSuccess
{
    public partial class Form1 : Form
    {
        private handleClient handleClient;
        private bool IsRunning = false;
        TcpListener serverSocket;
        TcpClient clientSocket;
      
        public Form1()
        {
            InitializeComponent();
        }

        public Form1(handleClient handleClient)
        {
            // TODO: Complete member initialization
            this.handleClient = handleClient;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string ipaddre = getip();
            IPAddress iP = IPAddress.Parse(ipaddre);
            AppendTxtip(iP.ToString());
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            new Thread(startServer).Start();
        }

        public  void startServer()
        {
            try
            {
                int port = Convert.ToInt32(txtport.Text);
                string ipaddre = getip();
                IPAddress iP = IPAddress.Parse(ipaddre);
                // AppendTxtip(iP.ToString());
                serverSocket = new TcpListener(iP, port);
                clientSocket = default(TcpClient);
                int counter = 0;
                serverSocket.Start();
               AppendTxtdata("Server Started , Waiting for Client Connection" + Environment.NewLine);

                IsRunning = true;

                while (IsRunning)
                {
                    counter += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    AppendTxtdata(" >> " + "Client No:" + Convert.ToString(counter) + " Connected-- IP --" + clientSocket.Client.RemoteEndPoint.ToString() + "");
                    AppendTxtdata(Environment.NewLine);
                    handleClient client = new handleClient();
                    client.startClient(clientSocket, Convert.ToString(counter));
                    // break;
                }
               
            }
            catch { }
            finally { }
        }

        public void StopServer()
        {
            IsRunning = false;
            
        }

 public void AppendTxtdata(string myval)
        {
            try
            {
                if (InvokeRequired)
                {
                        this.Invoke(new Action<string>(AppendTxtdata), new object[] { myval });
                        return;
                }
                
                GlobalVariable.SomeData += myval;
                txtdata.Text = GlobalVariable.SomeData;
            }
            catch 
            { }
        }





我这样在startCLient方法的handleClient.cs中调用这个方法,





I am calling this method in handleClient.cs in startCLient method in this way,

public void startClient(TcpClient inClientSocket, string clineNo)
      {
          this.clientSocket = inClientSocket;
          inClientSocket.NoDelay = true;
          this.clNo = clineNo;

          new Thread(GetData).Start();
      }

      public void GetData() // to run server //
      {
          con.Open();
          Form1 f = new Form1();

          try
          {
              NetworkStream networkStream = clientSocket.GetStream();

              for (int p = 0; p >= 0; p++)
              {
                  byte[] b = new byte[100];

                  int k = networkStream.Read(b, 0, b.Length);

                 f.AppendTxtdata("The Data from Client ( " + clNo + " ) Recieved..." + Environment.NewLine);

                  for (int i = 0; i < k; i++)
                  {
                      f.AppendTxtdata("" + Convert.ToChar(b[i]));
                  }

                  f.AppendTxtdata(Environment.NewLine);

                  var j = b.Length - 1;
                  while (b[j] == 0)
                  {
                      --j;
                  }

                  var temp = new byte[j + 1];
                  Array.Copy(b, temp, j + 1);

                  string res = System.Text.Encoding.ASCII.GetString(temp);

推荐答案

嗯......它可能确实有效,但你看不到它。

您无法看到它,因为数据会转到您新的表单实例:

Well...it probably does work, but you can't see it.
And you can't see it because the data goes to your new instance of the form:
        public void GetData() // to run server //
        {
            con.Open();
            Form1 f = new Form1();
...
                   f.AppendTxtdata("The Data from Client ( " + clNo + " ) Recieved..." + Environment.NewLine);
...

你没有显示,而不是去你已经运行的表格实例你可以看到。



请参阅此处:在两个表单之间传输信息,第2部分:Child to Parent [ ^ ]它指的是形式,但是当孩子是一个班级时它是同一个过程。

Which you don't display, instead of going to the already running instance of teh form you can see.

See here: Transferring information between two forms, Part 2: Child to Parent[^] it refers to forms, but it's the same process when the child is a class.


这篇关于当预期为false时,InvokeRequiredProperty变为true。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆