Visual Studio多表格关闭并重新打开

发布于 2025-01-22 07:45:34 字数 1415 浏览 1 评论 0原文

嗨,我真的是UI应用程序的新手 删除代码。

namespace UDP_TEST_2
{
    public partial class Form1 : Form
    {
        double x;
        UdpClient Client = new UdpClient(5000);

        void recv(IAsyncResult res)
        {
            IPEndPoint RemoteIP = new IPEndPoint(IPAddress.Any, 5000);
            byte[] received = Client.EndReceive(res, ref RemoteIP);
            data = Encoding.UTF8.GetString(received);
            Client.BeginReceive(new AsyncCallback(recv), null);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Tick += timer1_Tick;
            timer1.Interval = 001;
            try
            {
                Client.BeginReceive(new AsyncCallback(recv), null);
            }
            catch (Exception ex)
            {
                richTextBox1.Text += ex.Message.ToString();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //reading all bytes ETC
        }
    }
}

该代码在单个表单时起作用,但是自从我创建第二个表单以来,它将其称为f.showdialog(); 第一次操作很棒 的表单的代码

private void button1_Click(object sender, EventArgs e)
{
    Form1 f = new Form1();
    f.ShowDialog();
}

打开读取零件错误 我退出第二屏幕时打印了错误

hi i'm really new to UI applications and i'm trying to make a simple udp reader but everytime i close the program i have an error
stripped down code ..

namespace UDP_TEST_2
{
    public partial class Form1 : Form
    {
        double x;
        UdpClient Client = new UdpClient(5000);

        void recv(IAsyncResult res)
        {
            IPEndPoint RemoteIP = new IPEndPoint(IPAddress.Any, 5000);
            byte[] received = Client.EndReceive(res, ref RemoteIP);
            data = Encoding.UTF8.GetString(received);
            Client.BeginReceive(new AsyncCallback(recv), null);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Tick += timer1_Tick;
            timer1.Interval = 001;
            try
            {
                Client.BeginReceive(new AsyncCallback(recv), null);
            }
            catch (Exception ex)
            {
                richTextBox1.Text += ex.Message.ToString();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //reading all bytes ETC
        }
    }
}

the code works when its a single form but ever since i created a second form and call this one with f.showdialog();
first time it opes great but when i close it and reopen it with the button i get this error
code of the form that opens the read part

private void button1_Click(object sender, EventArgs e)
{
    Form1 f = new Form1();
    f.ShowDialog();
}

error
Error printed when i exit second screen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2025-01-29 07:45:34

关闭并处置udpclient。为此,您必须覆盖表格的distose方法:

protected override void Dispose(bool disposing)
{
    if (disposing) {
        if (components != null) {
            components.Dispose();
        }

        if (Client != null) {
            Client.Close();
            Client = null;
        }
    }

    base.Dispose(disposing);
}

根据

关闭禁用基础插座,并释放与UDPCLIENT相关的所有托管和不受管理的资源。

Close and Dispose the UdpClient. To do this you must override the form's Dispose method:

protected override void Dispose(bool disposing)
{
    if (disposing) {
        if (components != null) {
            components.Dispose();
        }

        if (Client != null) {
            Client.Close();
            Client = null;
        }
    }

    base.Dispose(disposing);
}

According to UdpClient.Close Method / Remarks:

The Close disables the underlying Socket and releases all managed and unmanaged resources associated with the UdpClient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文