zoukankan      html  css  js  c++  java
  • 自动检查网络连接状况,如果异常自动重连

    {
    author:cxg
    断网自动检查并提示,自动重连
    }

    unit ClientTestConnected;

    interface

    uses
      SysUtils, Classes, SqlExpr,uNetCommon;

    type
      TWaitThread = class(TThread)
      private
        { Private declarations }
        fNetConnect:Boolean;
        FCheckInterval: Integer;
        FMsg: string;
      protected
        procedure ShowMsg;
        procedure Execute; override;
        procedure ServerOpen;
        procedure ServerClose;
        function ConnRemoteSvr(cn:TSQLConnection;const hostname,UserName,password:string;
          const port:Integer):Boolean;
      public
        NetCheck1: TNetCheck;
        constructor Create(const ACheckInterval: Integer);
        destructor Destroy; override;

        procedure CheckNetConnect(Sender: TObject; var objWbemObject,
          objWbemAsyncContext: OleVariant);
        procedure CheckNetDisconnect(Sender: TObject; var objWbemObject,
          objWbemAsyncContext: OleVariant);
      end;

    implementation

    uses ClientForm;

    procedure TWaitThread.ServerClose;
    begin
      FrmMain.SQLConnection1.Close;
    end;

    procedure TWaitThread.ServerOpen;
    begin
      if fNetConnect then
      FMsg := '正在连接...'
      else FMsg :='网络异常';
      Synchronize(ShowMsg);
      if ConnRemoteSvr(FrmMain.SQLConnection1,'localhost','cxg','123',211) then
      begin
        FMsg := '网络正常';
        Synchronize(ShowMsg);
      end;
    end;

    procedure TWaitThread.ShowMsg;
    begin
      FrmMain.Label1.Caption := FMsg;
    end;

    procedure TWaitThread.Execute;
    begin
      try
        ServerOpen;
        fNetConnect :=True;
      except
        fNetConnect :=False;
      end;

      repeat
        Sleep(FCheckInterval);
        if not fNetConnect then
        begin
          FCheckInterval := 3000;
          ServerClose;
          ServerOpen;
        end;
      until Terminated;
    end;

    procedure TWaitThread.CheckNetConnect(Sender: TObject; var objWbemObject,
      objWbemAsyncContext: OleVariant);
    begin
      fNetConnect := True;
    end;

    procedure TWaitThread.CheckNetDisconnect(Sender: TObject; var objWbemObject,
      objWbemAsyncContext: OleVariant);
    begin
      fNetConnect :=False;
    end;

    function TWaitThread.ConnRemoteSvr(cn: TSQLConnection; const hostname, UserName,
      password: string; const port: Integer): Boolean;
    begin
      try
      cn.Close;
      cn.DriverName := 'DataSnap';
      cn.LoginPrompt :=False;
      Cn.ConnectionName := 'DataSnapCONNECTION';
      cn.Params.Clear;
      cn.Params.Add('DriverName=DataSnap');
      Cn.Params.Add('HostName='+HostName);
      Cn.Params.Add('Port='+inttostr(port));
      Cn.Params.Add('User_Name='+UserName);
      Cn.Params.Add('Password='+PassWord);
      cn.Open;
      Result :=True;
      except
        Result :=False;
      end;
    end;

    constructor TWaitThread.Create(const ACheckInterval: Integer);
    begin
      FCheckInterval := ACheckInterval;
      NetCheck1 :=TNetCheck.Create(nil);
      NetCheck1.OnNetWireConnect:=CheckNetConnect;
      NetCheck1.OnNetWireDisconnect:=CheckNetDisconnect;
      inherited Create(False);
    end;

    destructor TWaitThread.Destroy;
    begin
      NetCheck1.Free;
      NetCheck1 :=nil;
      inherited;
    end;

    end.

  • 相关阅读:
    Puppet报错汇总
    卖买股票的最佳收益
    JavaScript遍历JSON对象数据的方法
    网络IO模型
    STL pair类型的介绍
    JavaScript 代码执行顺序
    每日一练:#0001找出单独出现的数字
    更新GitHub项目出现There is no tracking information for the current branch. Please specify which branch you want to merge with. 怎么解决
    为什么MySQL数据库要用B+树存储索引?
    Node.js 获取本机Mac地址
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940916.html
Copyright © 2011-2022 走看看