基于xClient+Windows计划任务的断网重连程序实现(C#)

实现思路:

添加Windows计划任务,每隔一分钟执行一次编写的程序。在该程序中,首先判断网络是否正常。若正常,则关闭程序;若不正常,则重新打开xClient程序,并触发其“触发认证”按钮及隐藏窗口,而后关闭本程序。

1、在VS2008中,创建控制台应用程序。

2、Program.cs

[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;

namespace AlwaysOnline
{
class Program
{
[DllImport(“user32.dll”, EntryPoint = “FindWindow”, CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport(“user32.dll”, EntryPoint = “FindWindowEx”, CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport(“User32.dll”, EntryPoint = “SendMessage”)]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

static void Main(string[] args)
{
if (!isNetConnected()) connect();
}

///

/// 打开xClient,触发认证,并隐藏窗口
///

private static void connect()
{
string path = “D:\\Program Files\\xClient\\xClient.exe”;
Process p = Process.Start(path);
if (p == null)
Console.WriteLine(“Warning:process may already exist”);

Console.WriteLine(“Finding main window handle”);
IntPtr mainWindows = FindMainWindowHandle(“xClient 802.1x 客户端”, 100, 25);
Console.WriteLine(“Handle to main window is ” + mainWindows);

//有名字控件句柄
Console.WriteLine(“Findding handle to button1”);
IntPtr butt = FindWindowEx(mainWindows, IntPtr.Zero, null, “触发认证”); // 找到按钮
IntPtr butt1 = FindWindowEx(mainWindows, IntPtr.Zero, null, “隐藏窗口”); // 找到按钮
if (butt == IntPtr.Zero)
throw new Exception(“Unable to find button1”);
else
Console.WriteLine(“Handle to button1 is ” + butt);
//SendMessage(mainWindows, 0X101, butt, null);
SendMessage(butt, 0x201, butt, null); // 左键按下
SendMessage(butt, 0x202, butt, null); // 左键弹起
SendMessage(butt1, 0x201, butt1, null); // 左键按下
SendMessage(butt1, 0x202, butt1, null); // 左键弹起

//没有名字或者重名控件
//Console.WriteLine(“Findding handle to listbox1”);
//IntPtr lb = FindWindowByIndex(mwh, 3);
//if (lb == IntPtr.Zero)
// throw new Exception(“Unable to find listbox1”);
//else
// Console.WriteLine(“Handle to listbox1 is ” + lb);
}

///

/// 网络是否已连接
///

///
private static bool isNetConnected()
{
Ping p = new Ping();
PingReply pr1 = p.Send(“8.8.8.8”, 5000);
return pr1.Status == IPStatus.Success;
}

//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent, int index)
{
if (index == 0)
{
return hwndParent;
}
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hwndParent, result, null, null);
if (result != IntPtr.Zero)
{
++ct;
}
} while (ct < index && result != IntPtr.Zero); return result; } } //获得待测程序主窗体句柄 private static IntPtr FindMainWindowHandle(string caption, int delay, int maxTries) { IntPtr mwh = IntPtr.Zero; bool formFound = false; int attempts = 0; while (!formFound && attempts < maxTries) { if (mwh == IntPtr.Zero) { Console.WriteLine("Form not yet found"); Thread.Sleep(delay); ++attempts; mwh = FindWindow(null, caption); } else { Console.WriteLine("Form has been found"); formFound = true; } } if (mwh == IntPtr.Zero) throw new Exception("Could not find main window"); else return mwh; } } } [/csharp] 3、因为程序每分钟要触发一次,而控制台应用程序默认会显示黑色命令行窗口,因而为了在程序运行时不显示该窗口,参看本博客另外一篇文章《C# 控制台应用程序如何不弹出窗口》。

4、添加Windows计划任务

1) 将上一步生成的程序保存到某个位置,而后在控制面板中添加计划任务。

2) 在计划任务中,选择生成的程序,并选择“每天”执行,起始时间设置为“00:00”;

3) 创建该计划任务完成后,右键该任务选择属性,在高级选项中,做如下设置



发表评论

邮箱地址不会被公开。