尝试从其他表单向列表框添加数据值时如何解决

时间:2019-01-21 04:58:42

标签: c# c#-4.0 telnet

在尝试从主窗体向列表框添加数据时遇到问题,我试图从项目中的新类向该列表添加数据,我需要该新类才能向其中添加数据我的没有错误的列表框,我尝试使用调用,并且遇到以下错误(System.InvalidOperationException:“在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。”)堆栈溢出中的其他问题,但与我的问题类似,我将在此处添加代码的两个类,一个是主类,另一个是我创建的第二类,需要将数据添加到列表框中。数据来自telnet tcp / ip和端口23,表明连接正常,问题是将该数据添加到我的列表框中。

主类从其他类中调用函数

命名空间BarcodeReceivingApp {

//TelnetConnection stopConnection = new TelnetConnection();
public partial class BarcodeReceivingForm : Form
{
    //GLOBAL VARIABLES
    const string Hostname = "myip";
    private const int Port = 23;


    public BarcodeReceivingForm()
    {
        InitializeComponent();
    }

    private void btn_ConnectT_Click(object sender, EventArgs e)
    {

        var readData = new TelnetConnection(Hostname, Port);
        readData.ServerSocket(Hostname, Port);

    }

    private void btn_StopConnection_Click(object sender, EventArgs e)
    {
        //var connection = new TelnetConnection(Hostname, Port);
       // connection.CloseConnection();
    }
}

}

该类将从主类更改我的列表框的数据。

 namespace BarcodeReceivingApp

{     公共类TelnetConnection     {         public BarcodeReceivingForm BcForm = new BarcodeReceivingForm();

    private Thread _readWriteThread;
    private TcpClient _client;
    private NetworkStream _networkStream;
    private string _hostname;
    private int _port;

    public TelnetConnection(string hostname, int port)
    {
        this._hostname = hostname;
        this._port = port;
    }

    public void ServerSocket(string ip, int port)
    {

        try
        {
            _client = new TcpClient(ip, port);         
        }
        catch (SocketException)
        {
            MessageBox.Show(@"Failed to connect to server");
            return;
        }

        //Assign networkstream
        _networkStream = _client.GetStream();

        //start socket read/write thread
        _readWriteThread = new Thread(ReadWrite);
        _readWriteThread.Start();
    }

    public void ReadWrite()
    {

        //Set up connection loop
        while (true)
        {
            var command = "test";
            if (command == "STOP1")
                break;

            //write(command);
             var received = Read();


            BcForm.lst_BarcodeScan.Invoke(new Action (() => BcForm.lst_BarcodeScan.Items.Add(received)));


        }

    }

    public string Read()
    {
        byte[] data = new byte[1024];
        var received = "";

        var size = _networkStream.Read(data, 0, data.Length);
        received = Encoding.ASCII.GetString(data, 0, size);

        return received;
    }

    public void CloseConnection()
    {
        _networkStream.Close();
        _client.Close();
    }
}
}

像我说的那样,最终结果是运行循环时的ReadWrite方法将把数据从主表单类添加到我的列表框中

这是我收到错误的图片 Image of Error

1 个答案:

答案 0 :(得分:0)

像这样写你的第二堂课

using System;
using System.Threading;
using System.Windows.Forms; 
namespace BarcodeReceivingApp { 
    public class TelnetConnection { 

        private Thread _readWriteThread;
        private TcpClient _client;
        private NetworkStream _networkStream;
        private string _hostname;
        private int _port;
        private Form foo;

        public TelnetConnection(string hostname, int port)
        {
            this._hostname = hostname;
            this._port = port;
        }

        public void ServerSocket(string ip, int port,Form f)
        {
            this.foo = f;
            try
            {
                _client = new TcpClient(ip, port);         
            }
            catch (SocketException)
            {
                MessageBox.Show(@"Failed to connect to server");
                return;
            }

            _networkStream = _client.GetStream();

            _readWriteThread = new Thread(ReadWrite());
            _readWriteThread.Start();
        }  

        public void ReadWrite()
        {
            while (true)
            {
                var command = "test";
                if (command == "STOP1")
                    break;

                //write(command);
                 var received = Read();
                 if (foo.lst_BarcodeScan.InvokeRequired)
                 {
                         foo.lst_BarcodeScan.Invoke(new MethodInvoker(delegate {foo.lst_BarcodeScan.Items.Add(received);}));
                 }
            }
        }

        public string Read()
        {
            byte[] data = new byte[1024];
            var received = "";

            var size = _networkStream.Read(data, 0, data.Length);
            received = Encoding.ASCII.GetString(data, 0, size);

            return received;
        }

        public void CloseConnection()
        {
            _networkStream.Close();
            _client.Close();
        }
    }
}

然后在您的主类中使用它:

private void btn_ConnectT_Click(object sender, EventArgs e)
{

    var readData = new TelnetConnection(Hostname, Port);
    readData.ServerSocket(Hostname, Port, this);

}