NetStatus变量的值,按理应该是true才对的,为什么执行起来,总是false,可是F11调试时,也有时是true的,不明白这是为什么,多线程的问题吗?怎么解决呢?代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace TestConnSql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
wait_conn(); }
private void wait_conn() {
bool NetStatus, net = false;
string DataS = "192.168.160.1";
int ty; CheckIpConn conn = new CheckIpConn();
ty = conn.TT;
//net = conn.Connected; conn.CheckIp(DataS); //Thread.Sleep(2000); ty = conn.TT;
NetStatus = conn.Connected; //net = CheckIpConn.CheckIp(DataS); // MessageBox.Show(net.ToString()); //conn.setvalue();
NetStatus = conn.Connected; if (!NetStatus)
MessageBox.Show(conn.Connected.ToString() + ty.ToString());
}
}
}using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Windows.Forms;namespace TestConnSql
{
public class CheckIpConn
{
private int _port = 1433;
private volatile bool _connect;
private IPEndPoint _iPEndPoint;
private TcpClient _client;
private TimerCallback _timerCallBack;
private System.Threading.Timer _timer;
private int tt = 5; public int TT
{
get { return tt; }
} public int Port
{
get { return _port; }
set { _port = value; }
}
public bool Connected
{
get { return _connect; }
} public void setvalue()
{
_connect = true;
} public void CheckIp(string Ip)
{
_iPEndPoint = new IPEndPoint(IPAddress.Parse(Ip), _port);
_timerCallBack = new TimerCallback(CheckConnect);
_timer = new System.Threading.Timer(_timerCallBack, null, 10, 1000);
_timer.Dispose();
} public void CheckConnect(object o)
{
try
{
_client = new TcpClient();
_client.Connect(_iPEndPoint);
_connect = true;
this._connect = true;
tt = 50;
//MessageBox.Show("cc" + _connect);
_client.Close();
}
catch
{
_connect = false;
//MessageBox.Show("cc" + _connect); }
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace TestConnSql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
wait_conn(); }
private void wait_conn() {
bool NetStatus, net = false;
string DataS = "192.168.160.1";
int ty; CheckIpConn conn = new CheckIpConn();
ty = conn.TT;
//net = conn.Connected; conn.CheckIp(DataS); //Thread.Sleep(2000); ty = conn.TT;
NetStatus = conn.Connected; //net = CheckIpConn.CheckIp(DataS); // MessageBox.Show(net.ToString()); //conn.setvalue();
NetStatus = conn.Connected; if (!NetStatus)
MessageBox.Show(conn.Connected.ToString() + ty.ToString());
}
}
}using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Windows.Forms;namespace TestConnSql
{
public class CheckIpConn
{
private int _port = 1433;
private volatile bool _connect;
private IPEndPoint _iPEndPoint;
private TcpClient _client;
private TimerCallback _timerCallBack;
private System.Threading.Timer _timer;
private int tt = 5; public int TT
{
get { return tt; }
} public int Port
{
get { return _port; }
set { _port = value; }
}
public bool Connected
{
get { return _connect; }
} public void setvalue()
{
_connect = true;
} public void CheckIp(string Ip)
{
_iPEndPoint = new IPEndPoint(IPAddress.Parse(Ip), _port);
_timerCallBack = new TimerCallback(CheckConnect);
_timer = new System.Threading.Timer(_timerCallBack, null, 10, 1000);
_timer.Dispose();
} public void CheckConnect(object o)
{
try
{
_client = new TcpClient();
_client.Connect(_iPEndPoint);
_connect = true;
this._connect = true;
tt = 50;
//MessageBox.Show("cc" + _connect);
_client.Close();
}
catch
{
_connect = false;
//MessageBox.Show("cc" + _connect); }
}
}
}
我是想让 CheckConnect 尽快返回结果。
如果直接调用,当目标连接不上时,会等上一些时间,不想这样。
执行完这一句, conn.CheckIp(DataS);
conn.Connected的值就是TRUE。
加与不加Thread.Sleep(3000),好像没什么意义。
_timer = new System.Threading.Timer(_timerCallBack, null, 10, 1000);这句话不是说等10毫秒后执行CheckConnect,执行完CheckConnect,_connect=true。把这句 Thread.Sleep(2000);设为多大也没用啊,我一直在试。
http://stackoverflow.com/questions/8098301/c-sharp-timercallback-return-value-error
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;namespace conn
{
class Program
{
static void Main(string[] args)
{
wait_conn();
}
private static void wait_conn()
{
bool NetStatus = false;
string DataS = "127.0.0.1"; CheckIpConn conn = new CheckIpConn(); conn.CheckIp(DataS); Thread.Sleep(300); NetStatus = conn.Connected; if (!NetStatus)
Console.WriteLine(conn.Connected.ToString());
else
Console.WriteLine("OK!");
Console.Read();
}
}
public class CheckIpConn
{
private int _port = 135;
private volatile bool _connect;
private IPEndPoint _iPEndPoint;
private TcpClient _client;
private TimerCallback _timerCallBack;
private System.Threading.Timer _timer; public bool Connected
{
get { return _connect; }
}
public void CheckIp(string Ip)
{
_iPEndPoint = new IPEndPoint(IPAddress.Parse(Ip), _port);
_timerCallBack = new TimerCallback(CheckConnect);
_timer = new System.Threading.Timer(_timerCallBack, null, 0, 1000);
Thread.Sleep(2000);
_timer.Dispose(); } public void CheckConnect(object o)
{
try
{
_client = new TcpClient();
_client.Connect(_iPEndPoint);
_connect = true;
_client.Close();
}
catch
{
_connect = false;
}
}
}
}