展开全部
你这个根据接收数据控制窗体的启动隐藏什么意思呢?
我简单写个吧。不会你在问我
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
private void button1_Click(object sender, EventArgs e)
{
/*构建UDP服务器*/
/*得到本机IP,设置UDP端口号*/
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10000);
Socket command_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
/*绑定网络地址*/
command_sock.Bind(ipep);
Console.WriteLine("这是个UDP服务器,名字是 {0}", Dns.GetHostName());
while (true)
{
/*接收信息*/
command_sock.ReceiveTimeout = 10000; //设置接收超时时间单位毫秒
try
{
int recv = command_sock.ReceiveFrom(data, ref Remote);
/*得到目标的IP 端口*/
Console.WriteLine("Message received from {0}: ", Remote.ToString());
/*得到收到的信息*/
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
catch
{
Console.WriteLine("连接超时~~ ");
continue;
}
if(data[0] == 1)
{
/*打开新窗口,前提你已经创建了新的form*/
form2 new_form= new form2 ();
new_form.ShowDialog();
}
else if(data[0] == 2)
{
/*关闭现在的窗口*/
this.Close();
}
else if(data[0] == 3)
{
/*隐藏现在窗口*/
this.Hide();
}
}
}
不过这样写就是个按钮事件,而且是循环的。会导致你主窗口卡主。如果想留长点,用多线程多进程写好点
我简单写个吧。不会你在问我
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
private void button1_Click(object sender, EventArgs e)
{
/*构建UDP服务器*/
/*得到本机IP,设置UDP端口号*/
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10000);
Socket command_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
/*绑定网络地址*/
command_sock.Bind(ipep);
Console.WriteLine("这是个UDP服务器,名字是 {0}", Dns.GetHostName());
while (true)
{
/*接收信息*/
command_sock.ReceiveTimeout = 10000; //设置接收超时时间单位毫秒
try
{
int recv = command_sock.ReceiveFrom(data, ref Remote);
/*得到目标的IP 端口*/
Console.WriteLine("Message received from {0}: ", Remote.ToString());
/*得到收到的信息*/
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
catch
{
Console.WriteLine("连接超时~~ ");
continue;
}
if(data[0] == 1)
{
/*打开新窗口,前提你已经创建了新的form*/
form2 new_form= new form2 ();
new_form.ShowDialog();
}
else if(data[0] == 2)
{
/*关闭现在的窗口*/
this.Close();
}
else if(data[0] == 3)
{
/*隐藏现在窗口*/
this.Hide();
}
}
}
不过这样写就是个按钮事件,而且是循环的。会导致你主窗口卡主。如果想留长点,用多线程多进程写好点
追问
要是多线程呢???该是怎样的呢???
追答
private void button1_Click(object sender, EventArgs e)
{
Thread thread_de_online = new Thread(new ThreadStart(thread_jd_form));
thread_de_online.IsBackground = true;
thread_de_online.Start();
}
void thread_jd_form()
{
//这里面写上面那些东西就运行了
}
这样就后台运行了,form不会卡死。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询