private void _timerMessageHandling_Elapsed(object sender, ElapsedEventArgs e)
 {
     _timerMessageHandling.Stop();
     try
     {
         TimerMessageHandlingLock.AcquireWriterLock(Options.LockTimeOut);
         try
         {
             MessageLock.AcquireWriterLock(Options.LockTimeOut);
             try
             {
                 if (MessagesReceived.Count > 0)
                 {
                     try
                     {
                         CefListLock.AcquireWriterLock(Options.LockTimeOut);
                         foreach (var message in MessagesReceived)
                         {
                             //DEBUG
                             //Console.WriteLine(message);
                             string plain = EncodingEx.Base64.Decoder.DecodeString(Encoding.UTF8, message);
                             try
                             {
                                 CefDecodeResult cefDecodeResult = CefDecoding.Decode(plain);
                                 if (cefDecodeResult.DecodedObject is CefBrowserControl.BrowserAction)
                                 {
                                     BrowserActionsList.Add(cefDecodeResult.UCID, cefDecodeResult.DecodedObject);
                                 }
                                 else if (cefDecodeResult.DecodedObject is BrowserCommand)
                                 {
                                     BrowserCommandsList.Add(cefDecodeResult.UCID, cefDecodeResult.DecodedObject);
                                 }
                             }
                             catch (Exception ex)
                             {
                                 ExceptionHandling.Handling.GetException("Unexpected", ex);
                             }
                         }
                         MessagesReceived.Clear();
                     }
                     catch (ApplicationException ex1)
                     {
                         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                     }
                     finally
                     {
                         if (CefListLock.IsWriterLockHeld)
                         {
                             CefListLock.ReleaseWriterLock();
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 ExceptionHandling.Handling.GetException("Unexpected", ex);
             }
         }
         catch (ApplicationException ex1)
         {
             ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
         }
         finally
         {
             if (MessageLock.IsWriterLockHeld)
             {
                 MessageLock.ReleaseWriterLock();
             }
         }
         try
         {
             CefListLock.AcquireWriterLock(Options.LockTimeOut);
             try
             {
                 if (BrowserCommandsCompleted.Count > 0)
                 {
                     try
                     {
                         MessageLock.AcquireWriterLock(Options.LockTimeOut);
                         try
                         {
                             foreach (var ucidToCommand in BrowserCommandsCompleted)
                             {
                                 BrowserCommand cmd = (BrowserCommand)ucidToCommand.Value;
                                 MessagesPending.Add(new KeyValuePairEx <string, string>(RcpServerName,
                                                                                         CefEncoding.Encode(cmd.UCID, cmd)));
                             }
                             BrowserCommandsCompleted.Clear();
                         }
                         catch (Exception ex)
                         {
                             ExceptionHandling.Handling.GetException("Unexpected", ex);
                         }
                     }
                     catch (ApplicationException ex1)
                     {
                         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                     }
                     finally
                     {
                         if (MessageLock.IsWriterLockHeld)
                         {
                             MessageLock.ReleaseWriterLock();
                         }
                     }
                 }
                 if (BrowserActionsCompleted.Count > 0)
                 {
                     try
                     {
                         MessageLock.AcquireWriterLock(Options.LockTimeOut);
                         try
                         {
                             foreach (var ucidToAction in BrowserActionsCompleted)
                             {
                                 CefBrowserControl.BrowserAction action = (CefBrowserControl.BrowserAction)ucidToAction.Value;
                                 MessagesPending.Add(new KeyValuePairEx <string, string>(RcpServerName,
                                                                                         CefEncoding.Encode(action.UCID, action)));
                             }
                             BrowserActionsCompleted.Clear();
                         }
                         catch (Exception ex)
                         {
                             ExceptionHandling.Handling.GetException("Unexpected", ex);
                         }
                     }
                     catch (ApplicationException ex1)
                     {
                         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                     }
                     finally
                     {
                         if (MessageLock.IsWriterLockHeld)
                         {
                             MessageLock.ReleaseWriterLock();
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 ExceptionHandling.Handling.GetException("Unexpected", ex);
             }
         }
         catch (ApplicationException ex1)
         {
             ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
         }
         finally
         {
             if (CefListLock.IsWriterLockHeld)
             {
                 CefListLock.ReleaseWriterLock();
             }
         }
     }
     catch (ApplicationException ex1)
     {
         ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
     }
     finally
     {
         if (TimerMessageHandlingLock.IsWriterLockHeld)
         {
             TimerMessageHandlingLock.ReleaseWriterLock();
         }
     }
     _timerMessageHandling.Start();
 }
        private void _timerBrowserHandling_Elapsed(object sender, ElapsedEventArgs e)
        {
            _timerBrowserHandling.Stop();
            try
            {
                TimerCefBrowserLock.AcquireWriterLock(Options.LockTimeOut);
                try
                {
                    CefListLock.AcquireWriterLock(Options.LockTimeOut);
                    try
                    {
                        List <string> forRemoving = new List <string>();
                        foreach (var ucidToBrowsercommand in BrowserCommandsList)
                        {
                            try
                            {
                                string commandType = ucidToBrowsercommand.Value.GetType().Name;
                                //Console.WriteLine(commandType);
                                switch (commandType)
                                {
                                case "Open":
                                    Open open =
                                        (Open)ucidToBrowsercommand.Value;
                                    open.Successful = true;
                                    open.Completed  = true;
                                    forRemoving.Add(open.UCID);
                                    break;

                                case "SwitchWindowVisibility":
                                    SwitchWindowVisibility visibility =
                                        (SwitchWindowVisibility)ucidToBrowsercommand.Value;
                                    //Visibility = visibility.Visible.Value ? Visibility.Visible : Visibility.Hidden;
                                    //ShowInTaskbar = visibility.Visible.Value;
                                    Dispatcher.Invoke(() =>
                                    {
                                        //Visibility = visibility.Visible.Value ? Visibility.Visible : Visibility.Hidden;
                                        //ShowInTaskbar = visibility.Visible.Value;
                                        if (visibility.Visible.Value)
                                        {
                                            Topmost = true;
                                            Activate();
                                            Show();
                                        }
                                        else
                                        {
                                            Topmost = false;
                                            Hide();
                                        }
                                    });
                                    visibility.Successful = true;
                                    visibility.Completed  = true;
                                    forRemoving.Add(visibility.UCID);
                                    break;

                                case "LoadUrl":
                                    LoadUrl loadUrl = (LoadUrl)ucidToBrowsercommand.Value;
                                    Dispatcher.Invoke(() =>
                                    {
                                        Browser.Load(loadUrl.Url.Value);
                                        Title = loadUrl.Url.Value;
                                    });
                                    loadUrl.Successful = true;
                                    loadUrl.Completed  = true;
                                    forRemoving.Add(loadUrl.UCID);
                                    break;

                                case "SwitchUserInputEnabling":
                                    SwitchUserInputEnabling enabling =
                                        (SwitchUserInputEnabling)ucidToBrowsercommand.Value;
                                    throw new Exception("This method is not fully implemented by CefBrowser");
                                    // ReSharper disable once HeuristicUnreachableCode
                                    visibility.Completed = true;
                                    forRemoving.Add(visibility.UCID);
                                    break;

                                case "GetInputFromUser":
                                    GetInputFromUser input = (GetInputFromUser)ucidToBrowsercommand.Value;
                                    Dispatcher.Invoke(() =>
                                    {
                                        GetInput form = new GetInput((GetInputFromUser)ucidToBrowsercommand.Value);
                                        //form.Show();
                                    });
                                    while (true)
                                    {
                                        if (input.Completed || input.TimedOut)
                                        {
                                            break;
                                        }
                                        Thread.Sleep(100);
                                    }
                                    //form.Dispose();
                                    //form = null;
                                    //input.Successful = true;
                                    input.Completed = true;
                                    forRemoving.Add(input.UCID);
                                    break;

                                default:
                                    throw new Exception(
                                              "This browser command method is not implemented by CefBrowser");
                                }
                            }
                            catch (Exception ex)
                            {
                                ExceptionHandling.Handling.GetException("Unexpected", ex);
                            }
                        }
                        foreach (string ucid in forRemoving)
                        {
                            BrowserCommandsCompleted.Add(ucid, BrowserCommandsList[ucid]);
                            BrowserCommandsList.Remove(ucid);
                        }
                        //----BrowserActions
                        forRemoving.Clear();
                        foreach (var ucidToBrowserAction in BrowserActionsList)
                        {
                            try
                            {
                                CefBrowserControl.BrowserAction browserAction =
                                    (CefBrowserControl.BrowserAction)ucidToBrowserAction.Value;
                                ActionManager.AddBrowserActions(browserAction);
                                forRemoving.Add(ucidToBrowserAction.Key);
                            }
                            catch (Exception ex)
                            {
                                ExceptionHandling.Handling.GetException("Unexpected", ex);
                            }
                        }
                        foreach (string ucid in forRemoving)
                        {
                            //BrowserActionsCompleted.Add(ucid, BrowserActionsCompleted[ucid]);
                            //BrowserActionsCompleted.Remove(ucid);
                            BrowserActionsInTransit.Add(ucid, BrowserActionsList[ucid]);
                            BrowserActionsList.Remove(ucid);
                        }
                        //get all completed actions from actionmanager
                        forRemoving.Clear();
                        if (BrowserActionManager.BrowserActionsCompleted.Count > 0)
                        {
                            for (object obj = ActionManager.GetCompletedBrowserAction();
                                 obj != null;
                                 obj = ActionManager.GetCompletedBrowserAction())
                            {
                                CefBrowserControl.BrowserAction browserAction = (CefBrowserControl.BrowserAction)obj;
                                forRemoving.Add(browserAction.UCID);
                            }
                            foreach (string ucid in forRemoving)
                            {
                                BrowserActionsCompleted.Add(ucid, BrowserActionsInTransit[ucid]);
                                BrowserActionsInTransit.Remove(ucid);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ExceptionHandling.Handling.GetException("Unexpected", ex);
                    }
                }
                catch (ApplicationException ex1)
                {
                    ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
                }
                finally
                {
                    if (CefListLock.IsWriterLockHeld)
                    {
                        CefListLock.ReleaseWriterLock();
                    }
                }
            }
            catch (ApplicationException ex1)
            {
                ExceptionHandling.Handling.GetException("ReaderWriterLock", ex1);
            }
            finally
            {
                if (TimerCefBrowserLock.IsWriterLockHeld)
                {
                    TimerCefBrowserLock.ReleaseWriterLock();
                }
                _timerBrowserHandling.Start();
            }
        }