【发布时间】:2014-03-14 03:41:20
【问题描述】:
我有一个按钮代码,只要用户点击它就可以连接到网络。
private void cmdConnect_Click(object sender, System.EventArgs e)
{
try
{
EnableCommands(true);
//Creating instance of Socket
m_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );
// retrieve the remote machines IP address
IPAddress ip = IPAddress.Parse (txtIPAddr.Text);
//A printer has open port 9100 which can be used to connect to printer
int iPortNo = System.Convert.ToInt16 ( txtPort.Text);
//create the end point
IPEndPoint ipEnd = new IPEndPoint (ip.Address,iPortNo);
//connect to the remote host
m_socClient.Connect ( ipEnd );
EnableCommands(false);
//wait for data to arrive
WaitForData();
}
catch(SocketException se)
{
MessageBox.Show (se.Message );
EnableCommands(true);
}
page_counter();
}
但是现在我希望此代码首先自动运行,而无需任何人点击它。我想这样做是因为我希望任务调度程序每天运行此代码以进行更新。该程序将在后面运行,并且与用户没有交互。因此我希望这个程序自行自动连接。这可能吗?请指教。
【问题讨论】: