我有一个按钮代码,当用户单击它时,它会启用与网络的连接。
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();
}
然而现在我想让这段代码自动运行,而不需要任何人点击它。我想这样做是因为我希望任务计划程序每天运行此代码以进行更新。该程序将在后台运行,并且没有与用户的交互。因此,我希望该程序可以自动连接自己。这是否可行?请给予建议。