我有一个按钮代码,只要用户点击它就可以连接到网络。
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();
}
但是现在我希望此代码在没有任何人点击的情况下自动运行。我想这样做是因为我希望任务调度程序每天运行此代码来进行更新。这个程序将在后面运行,并且没有与用户交互。因此,我希望这个程序自动连接。这可能吗?请指教。
答案 0 :(得分:3)
您可以将此代码放在方法中而不是按钮单击,并从按钮单击事件调用该方法,也可以在InitializeComponent()之后从Form的构造函数调用该方法。
public partial class Sample : Form
{
public Sample()
{
InitializeComponent();
CmdConnect(txtIPAddr.Text, txtPort.Text);
}
private void cmdConnect_Click(object sender, System.EventArgs e)
{
CmdConnect(txtIPAddr.Text, txtPort.Text);
}
private void CmdConnect(string txtIPAddr, string txtPort)
{
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);
//A printer has open port 9100 which can be used to connect to printer
int iPortNo = System.Convert.ToInt16(txtPort);
//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();
}
// Other Methods
}
答案 1 :(得分:2)
在构造函数中调用cmdConnect_Click(null, null)
或者首先运行它的位置。
答案 2 :(得分:2)
您可以通过在FormLoad()事件上调用PerformClick()
来提升按钮控件的单击事件。因此,它会自动运行,无需任何人点击它或Task Scheduler
运行您的应用程序进行更新。
cmdConnect.PerformClick();
但我认为发送点击事件是最好的方式。
如果您只想从表单中的其他位置运行try块中的代码,请将代码放入单独的方法(如DoWork()
)中,并从需要使用它的任何位置调用该方法。这样你就可以访问该功能了。
private void NetworkConnection()
{
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();
}
在按钮点击事件中,只需调用NetworkConnection()
方法:
private void cmdConnect_Click(object sender, System.EventArgs e)
{
NetworkConnection();
}