Advertisement
Guest User

Untitled

a guest
Aug 20th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. public partial class FrmMain : Form
  2.     {
  3.         public FrmMain()
  4.         {
  5.             InitializeComponent();
  6.             InitializeNetworkConnectionChecks();
  7.             InitializeConnectionTimer();
  8.         }
  9.  
  10.         private LyncClient client;
  11.         private ConversationManager conversationManager;
  12.         private Timer connectionTimer;
  13.         private bool networkAvailable;
  14.  
  15.         private void InitializeNetworkConnectionChecks()
  16.         {
  17.             NetworkChange.NetworkAddressChanged += NetworkAddress_Changed;
  18.             NetworkChange.NetworkAvailabilityChanged += NetworkAvailability_Changed;
  19.         }
  20.  
  21.         private void InitializeConnectionTimer()
  22.         {
  23.             connectionTimer = new Timer
  24.             {
  25.                 Interval = 1000
  26.             };
  27.  
  28.             connectionTimer.Tick += connectionTimer_Tick;
  29.  
  30.             connectionTimer.Start();
  31.         }
  32.  
  33.         private void CheckConnection()
  34.         {
  35.             CheckNetworkConnection();
  36.             TrySetClient();
  37.             SetConversationManager();
  38.         }
  39.  
  40.         private void CheckNetworkConnection()
  41.         {
  42.             networkAvailable = NetworkInterface.GetIsNetworkAvailable();
  43.         }
  44.  
  45.         private void TrySetClient()
  46.         {
  47.             client = null;
  48.  
  49.             try
  50.             {
  51.                 client = LyncClient.GetClient();
  52.                 client.ClientDisconnected += Client_Disconnected;
  53.                 client.StateChanged += Client_StateChanged;
  54.             }
  55.             catch (Exception)
  56.             {
  57.             }
  58.         }
  59.  
  60.         private void SetConversationManager()
  61.         {
  62.             if (client != null)
  63.             {
  64.                 conversationManager = client.ConversationManager;
  65.                 conversationManager.ConversationAdded += Conversation_Added;
  66.                 conversationManager.ConversationRemoved += Conversation_Removed;
  67.             }
  68.             else
  69.             {
  70.                 conversationManager = null;
  71.             }
  72.         }
  73.  
  74.         private void Client_Disconnected(object sender, EventArgs e)
  75.         {
  76.             CheckConnection();
  77.         }
  78.  
  79.         private void Client_StateChanged(object sender, ClientStateChangedEventArgs e)
  80.         {
  81.             CheckConnection();
  82.         }
  83.  
  84.         private void connectionTimer_Tick(object sender, EventArgs e)
  85.         {
  86.             CheckConnection();
  87.         }
  88.  
  89.         private void Conversation_Added(object sender, ConversationManagerEventArgs e)
  90.         {
  91.             System.Diagnostics.Process.Start("https://www.google.com/");
  92.         }
  93.  
  94.         private void Conversation_Removed(object sender, ConversationManagerEventArgs e)
  95.         {
  96.             // ...
  97.         }
  98.  
  99.         private void NetworkAddress_Changed(object sender, EventArgs e)
  100.         {
  101.             CheckConnection();
  102.         }
  103.  
  104.         private void NetworkAvailability_Changed(object sender, EventArgs e)
  105.         {
  106.             CheckConnection();
  107.         }
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement