C# c/s模式小程序问题!急!!!
b端有个按钮,点击,a端应该弹出一个新窗口(意思是b可以远程控制a弹窗口)。但是我做出来的是a端能弹出新窗口,但是新窗口一出来就卡,点两下就没有响应了。同样是f2.sho...
b端有个按钮,点击,a端应该弹出一个新窗口(意思是b可以远程控制a弹窗口)。但是我做出来的是a端能弹出新窗口,但是新窗口一出来就卡,点两下就没有响应了。同样是f2.show();点击a端人按钮弹出的窗口就不卡,很正常。求高手给解决一下,谢谢
a端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace a
{
public partial class Form1 : Form
{
private UdpClient uc; //UDP传输
private IPEndPoint iep; //绑定
private Thread myTh; //监听线程
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
uc = new UdpClient(88);
IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostEntry(Dns.GetHostName());
}
private void Form1_Load(object sender, EventArgs e)
{
myTh = new Thread(new ThreadStart(listen));
myTh.IsBackground = true; //设置在后台运行
myTh.Start();
}
private void listen()
{
while (true)
{
string message = Encoding.UTF8.GetString(uc.Receive(ref iep));
string commandMessage = message.Substring(0, 1);
if (commandMessage.Equals("4"))
{
Form2 f2 = new Form2();
f2.Show();
//MessageBox.Show("111");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
}
}
b端代码:
namespace b
{
public partial class Form1 : Form
{
private UdpClient uc; //UDP传输
private IPEndPoint iep; //绑定
// private Thread myTh; //监听线程
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
uc = new UdpClient(88);
IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostEntry(Dns.GetHostName());
}
private void button1_Click(object sender, EventArgs e)
{
iep = new IPEndPoint(IPAddress.Broadcast, 88);
string temp = "4";
byte[] b = Encoding.UTF8.GetBytes(temp);
uc.Send(b, b.Length, iep);
}
}
} 展开
a端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace a
{
public partial class Form1 : Form
{
private UdpClient uc; //UDP传输
private IPEndPoint iep; //绑定
private Thread myTh; //监听线程
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
uc = new UdpClient(88);
IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostEntry(Dns.GetHostName());
}
private void Form1_Load(object sender, EventArgs e)
{
myTh = new Thread(new ThreadStart(listen));
myTh.IsBackground = true; //设置在后台运行
myTh.Start();
}
private void listen()
{
while (true)
{
string message = Encoding.UTF8.GetString(uc.Receive(ref iep));
string commandMessage = message.Substring(0, 1);
if (commandMessage.Equals("4"))
{
Form2 f2 = new Form2();
f2.Show();
//MessageBox.Show("111");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
}
}
b端代码:
namespace b
{
public partial class Form1 : Form
{
private UdpClient uc; //UDP传输
private IPEndPoint iep; //绑定
// private Thread myTh; //监听线程
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
uc = new UdpClient(88);
IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostEntry(Dns.GetHostName());
}
private void button1_Click(object sender, EventArgs e)
{
iep = new IPEndPoint(IPAddress.Broadcast, 88);
string temp = "4";
byte[] b = Encoding.UTF8.GetBytes(temp);
uc.Send(b, b.Length, iep);
}
}
} 展开
展开全部
应该你这个主线程的问题(通讯线程myTh成了主线程,这样窗体的UI线程就挂起了),你用按钮控制的窗体和通讯方法里的窗体不在相同的线程里,所以不能做比较,你把通讯线程里的f2设置成成员对象不行(这样只能弹出一个)?如果你想弹多个的话那就加个Timer,1秒一次查找是否有新消息(可以用LIST<String>建个消息池)来了...将控制代码放到Timer里来控制,这样控制权就在主线程里了
这样给你改了一下,你按我说方法,加上一个计时器Timer(在组件里),然后设置interval=500ms(随意设),然后添加Timer的Tick事件...下面我改的,你覆盖一下你的原码即可
public partial class Form1 : Form
{
private UdpClient uc; //UDP传输
private IPEndPoint iep; //绑定
private Thread myTh; //监听线程
private bool _flagIsShow = false;//添加标志
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
uc = new UdpClient(88);
IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostEntry(Dns.GetHostName());
}
private void Form1_Load(object sender, EventArgs e)
{
myTh = new Thread(new ThreadStart(listen));
myTh.IsBackground = true; //设置在后台运行
myTh.Start();
this.timer1.Start();
}
private void listen()
{
while (true)
{
byte[] bArrTemp = uc.Receive(ref iep);
string message = Encoding.UTF8.GetString(bArrTemp);
string commandMessage = message.Substring(0, 1);
if (commandMessage.Equals("4"))
{
this._flagIsShow = true;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this._flagIsShow)
{
Form2 f2 = new Form2();
f2.Show();
}
this._flagIsShow = false;
}
}
这样给你改了一下,你按我说方法,加上一个计时器Timer(在组件里),然后设置interval=500ms(随意设),然后添加Timer的Tick事件...下面我改的,你覆盖一下你的原码即可
public partial class Form1 : Form
{
private UdpClient uc; //UDP传输
private IPEndPoint iep; //绑定
private Thread myTh; //监听线程
private bool _flagIsShow = false;//添加标志
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
uc = new UdpClient(88);
IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostEntry(Dns.GetHostName());
}
private void Form1_Load(object sender, EventArgs e)
{
myTh = new Thread(new ThreadStart(listen));
myTh.IsBackground = true; //设置在后台运行
myTh.Start();
this.timer1.Start();
}
private void listen()
{
while (true)
{
byte[] bArrTemp = uc.Receive(ref iep);
string message = Encoding.UTF8.GetString(bArrTemp);
string commandMessage = message.Substring(0, 1);
if (commandMessage.Equals("4"))
{
this._flagIsShow = true;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this._flagIsShow)
{
Form2 f2 = new Form2();
f2.Show();
}
this._flagIsShow = false;
}
}
展开全部
whiel(true)
{
if (commandMessage.Equals("4"))
{
Form2 f2 = new Form2();
f2.Show();
break;
//MessageBox.Show("111");
}
}
跳出while循环!!!
{
if (commandMessage.Equals("4"))
{
Form2 f2 = new Form2();
f2.Show();
break;
//MessageBox.Show("111");
}
}
跳出while循环!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-12-11
展开全部
不晓得 我问你个问题哦SqlConnection conn = DataBase.data();
string sql = "select * from student where " + comboBox1.Text + " like '%" + textBox1.Text + "%'";
SqlDataAdapter ad = new SqlDataAdapter(sql ,conn );
DataSet ds = new DataSet();
ad.Fill(ds,"a");
dataGridView1.datasource=ds;
dataGridView1.DataMember = "a";
后面两句可以写成一句么,。,以前的datagird 是不是现在不用setdatabing
后面怎么写,。将表a中的东西放到datagirdview
string sql = "select * from student where " + comboBox1.Text + " like '%" + textBox1.Text + "%'";
SqlDataAdapter ad = new SqlDataAdapter(sql ,conn );
DataSet ds = new DataSet();
ad.Fill(ds,"a");
dataGridView1.datasource=ds;
dataGridView1.DataMember = "a";
后面两句可以写成一句么,。,以前的datagird 是不是现在不用setdatabing
后面怎么写,。将表a中的东西放到datagirdview
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
断点!自己查.....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询