| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.Threading;
- using Xilium.CefGlue.Helpers.Log;
- namespace Xilium.CefGlue.WPF
- {
- public class UiHelper : IUiHelper
- {
- private readonly ILogger _log;
- public UiHelper(ILogger log)
- {
- if (log == null)
- {
- throw new ArgumentNullException("log");
- }
- _log = log;
- }
- public void PerformInUiThread(Action action)
- {
- if (action == null)
- {
- throw new ArgumentNullException("action");
- }
- var currentApplication = System.Windows.Application.Current;
- if ((currentApplication == null) || (Thread.CurrentThread == currentApplication.Dispatcher.Thread))
- {
- action.Invoke();
- }
- else
- {
- currentApplication.Dispatcher.Invoke(action);
- }
- }
- public void StartAsynchronously(Action action)
- {
- if (action == null)
- {
- throw new ArgumentNullException("action");
- }
- (new Thread(action.Invoke)).Start();
- }
- public void PerformForMinimumTime(Action action, bool requiresUiThread, int minimumMillisecondsBeforeReturn)
- {
- if (action == null)
- {
- throw new ArgumentNullException("action");
- }
- var startTime = Environment.TickCount;
- if (requiresUiThread)
- {
- PerformInUiThread(action);
- }
- else
- {
- action.Invoke();
- }
- var remainingTime = minimumMillisecondsBeforeReturn - (Environment.TickCount - startTime);
- if (remainingTime > 0)
- {
- Thread.Sleep(remainingTime);
- }
- }
- public void IgnoreException(Action action)
- {
- if (action == null)
- {
- throw new ArgumentNullException("action");
- }
- try
- {
- action.Invoke();
- }
- catch (Exception e)
- {
- _log.ErrorException("Invocation failed", e);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="action"></param>
- /// <returns>Default if fails</returns>
- public T IgnoreException<T>(Func<T> action)
- {
- if (action == null)
- {
- throw new ArgumentNullException("action");
- }
- try
- {
- return action.Invoke();
- }
- catch (Exception e)
- {
- _log.ErrorException("Invokation failed", e);
- return default(T);
- }
- }
- public void Sleep(int milliseconds)
- {
- Thread.Sleep(milliseconds);
- }
- public void Sleep(TimeSpan sleepTime)
- {
- Thread.Sleep(sleepTime);
- }
- }
- }
|