private void button2_Click(object sender, System.EventArgs e)
{
timer.start();
errors=ConnRemote(ip,nPort,user,userpassword);
}//timer1的事件函数
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Stop();
MessageBox.Show("aa");
}timer的interval=100,
可是timer的tick事件根本不执行,如果把
errors=ConnRemote(ip,nPort,user,userpassword);去掉就正常了
public int ConnRemote(string Ip,int NPort,string User,string Password)
{
Thread thread=new Thread(new ThreadStart(LoginConn));
thread.Start();
allDone.WaitOne();
} public int ConnRemote(string Ip,int NPort,string User,string Password)///用socket连接到远程服务器
{
allDone.Reset();
Thread thread=new Thread(new ThreadStart(LoginConn));
thread.Start();
allDone.WaitOne();
}
private void LoginConn()
{ IPEndPoint ipRemoteEndPoint= new IPEndPoint(IPAddress.Parse(ip), nPort);
conn_sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
connectDone.Reset();
conn_sock.BeginConnect(ipRemoteEndPoint,new AsyncCallback(ConnectCallback),conn_sock);
connectDone.WaitOne();
}我怎么才能让timer有效呢
{
timer.start();
errors=ConnRemote(ip,nPort,user,userpassword);
}//timer1的事件函数
private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Stop();
MessageBox.Show("aa");
}timer的interval=100,
可是timer的tick事件根本不执行,如果把
errors=ConnRemote(ip,nPort,user,userpassword);去掉就正常了
public int ConnRemote(string Ip,int NPort,string User,string Password)
{
Thread thread=new Thread(new ThreadStart(LoginConn));
thread.Start();
allDone.WaitOne();
} public int ConnRemote(string Ip,int NPort,string User,string Password)///用socket连接到远程服务器
{
allDone.Reset();
Thread thread=new Thread(new ThreadStart(LoginConn));
thread.Start();
allDone.WaitOne();
}
private void LoginConn()
{ IPEndPoint ipRemoteEndPoint= new IPEndPoint(IPAddress.Parse(ip), nPort);
conn_sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
connectDone.Reset();
conn_sock.BeginConnect(ipRemoteEndPoint,new AsyncCallback(ConnectCallback),conn_sock);
connectDone.WaitOne();
}我怎么才能让timer有效呢
解决方案 »
- C#DataGridView中的问题
- 用什么发法(最好是语句)调用SQLSRVER2000里面某个字段与界面所设的值进行比对或者是赋值
- 通过窗口的句柄如何对窗口的按钮进行操作?
- 类的问题
- 请教gdi画入图片为什么错位?
- 求救!急!异常"System.executionengineexception"在defaltdomain中发生
- 怎样在winform中显示一个二叉树?(高手进)
- 请教C#如何把字符串内容写到一个空白的html中并使得其作为弹出窗口弹出
- C#中有返回值和无返回值,有什么区别,
- 我的打印预览结果和我的实际打印结果不一样,是什么原因?
- Datalist怎么连接网页?
- DataGrid创建自定义列的问题
Thread thread=new Thread(new ThreadStart(LoginConn));
thread.Start();
allDone.WaitOne();当然阻塞住了,没有调用allDone.Set()那么allDone.WaitOne()不会退出,所以当你调用ConnRemote的时候根本就无法退出,当然不会调用timer的事件了
对应的allDone.Set()方法去哪里了?
我的allDone.Set()当然在函数LoginConn里了to sun926:我想问有没有解决办法to Jim3:
应该是一样的吧,好像没解决问题雅
{ IPEndPoint ipRemoteEndPoint= new IPEndPoint(IPAddress.Parse(ip), nPort);
conn_sock=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
connectDone.Reset();
conn_sock.BeginConnect(ipRemoteEndPoint,new AsyncCallback(ConnectCallback),conn_sock);
connectDone.WaitOne();
}
楼主上面的函数哪里有allDone.Set()????
我都说了,我的代码用了很长时间没问题,只是想加个定时器而已
可以改为使用System.Timers.Timer,这个是真正的Timer,运行在独立线程里,可以同时执行。