protected override void RunProxyImpl(CommandSettings settings) { // prepare Windows system event handlers SessionEndingEventHandler onSessionEnding = (o, e) => { AwakeControllerThread(ControllerThreadSynchronizer.EventKind.SystemSessionEnding); }; PowerModeChangedEventHandler onPowerModeChanged = (o, e) => { switch (e.Mode) { case PowerModes.Suspend: AwakeControllerThread(ControllerThreadSynchronizer.EventKind.Suspend); break; case PowerModes.Resume: AwakeControllerThread(ControllerThreadSynchronizer.EventKind.Resume); break; } }; // run the proxy SystemEvents.SessionEnding += onSessionEnding; SystemEvents.PowerModeChanged += onPowerModeChanged; try { base.RunProxyImpl(settings); } finally { SystemEvents.PowerModeChanged -= onPowerModeChanged; SystemEvents.SessionEnding -= onSessionEnding; } return; }
/// <summary> /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed. /// <example> /// powermodechangedeventhandler.BeginInvoke(sender, e, callback); /// </example> /// </summary> public static IAsyncResult BeginInvoke(this PowerModeChangedEventHandler powermodechangedeventhandler, Object sender, PowerModeChangedEventArgs e, AsyncCallback callback) { if (powermodechangedeventhandler == null) { throw new ArgumentNullException("powermodechangedeventhandler"); } return(powermodechangedeventhandler.BeginInvoke(sender, e, callback, null)); }
public static void startListen() { mSystemEvents_SessionSwitch = new SessionSwitchEventHandler(SystemEvents_SessionSwitch); mSystemEvents_PowerModeChanged = new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); mSystemEvent_ScreenChanged = new EventHandler(SystemEvents_DisplaySettingsChanged); SystemEvents.SessionSwitch += mSystemEvents_SessionSwitch; SystemEvents.PowerModeChanged += mSystemEvents_PowerModeChanged; SystemEvents.DisplaySettingsChanged += mSystemEvent_ScreenChanged; }
/** * Create event handlers needed to detect various state changes that affect window capture. * This is done explicitly with assigned fields to allow proper disposal. According to the * documentation, if not properly disposed, there will be leaks (although I'm skeptical for * this use case, since they live the lifetime of the process). */ private void CreateEventHandlers() { this.displaySettingsChangingHandler = (s, e) => { Log.Trace("Display settings changing handler invoked"); this.ignoreCaptureRequests = true; CancelDelayedCapture(); // Throw away any pending captures }; this.displaySettingsChangedHandler = (s, e) => { Log.Trace("Display settings changed"); StateDetector.WaitForWindowStabilization(() => { // CancelDelayedCapture(); // Throw away any pending captures BeginRestoreApplicationsOnCurrentDisplays(); }); }; this.powerModeChangedHandler = (s, e) => { switch (e.Mode) { case PowerModes.Suspend: Log.Info("System Suspending"); break; case PowerModes.Resume: Log.Info("System Resuming"); this.ignoreCaptureRequests = true; CancelDelayedCapture(); // Throw away any pending captures BeginRestoreApplicationsOnCurrentDisplays(); break; default: Log.Trace("Unhandled power mode change: {0}", nameof(e.Mode)); break; } }; this.sessionSwitchEventHandler = (sender, args) => { if (args.Reason == SessionSwitchReason.SessionLock) { Log.Trace("Session locked"); this.isSessionLocked = true; } else if (args.Reason == SessionSwitchReason.SessionUnlock) { Log.Trace("Session unlocked"); this.isSessionLocked = false; } }; }
public TagWindow(int posx, int posy) { InitializeComponent(); pmceh = new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); SystemEvents.PowerModeChanged += pmceh; Height = WindowHeight; Width = WindowWidth; data = new List <TagBase>(); if (posx != -1 && posy != -1) { Left = posx; Top = posy; WindowStartupLocation = WindowStartupLocation.Manual; } }
public NetworkManager(AddInManager addInManager) { this.addInManager = addInManager; networkChangedHandler = new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged); NetworkChange.NetworkAddressChanged += networkChangedHandler; powerModeChangedHandler = new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); SystemEvents.PowerModeChanged += powerModeChangedHandler; if (Windows7Helper.IsWindows7) { locationManager = LocationManager.Instance; locationManager.NewLocationAvailable += new EventHandler(locationManager_NewLocationAvailable); } }
public HoverWindow() { //detect lock event and save state or restore state _sseh = new SessionSwitchEventHandler(SysEventsCheck); SystemEvents.SessionSwitch += _sseh; //detect sleep mode and save state _powerMode = new PowerModeChangedEventHandler(OnPowerModeChanged); SystemEvents.PowerModeChanged += _powerMode; InitializeComponent(); _baseSearch = ConfigurationManager.AppSettings["BaseSearch"]; _ignoreBaseSearch = bool.Parse(ConfigurationManager.AppSettings["IgnoreBaseSearch"]); //_autoUpdate = new AutoUpdater(ConfigurationManager.AppSettings["AutoUpdateURL"],new TimeSpan(int.Parse(ConfigurationManager.AppSettings["VersionUpdateCheckIntervalHours"]), 0, 0)); loadSettings(); //_autoUpdate.Run(); }
public void DoesNotSignalPowerModeChanged(int pmEvent) { bool changed = false; PowerModeChangedEventHandler changedHandler = (o, e) => { changed = true; }; SystemEvents.PowerModeChanged += changedHandler; try { SendMessage(pmEvent); Assert.False(changed); } finally { SystemEvents.PowerModeChanged -= changedHandler; } }
/// <summary> /// Application's main form constructor /// </summary> public formMain() { InitializeComponent(); for (int i = 0; i < 24; i++) { comboHFrom.Items.Add(i); comboHTo.Items.Add(i); } comboMFrom.Items.AddRange(new object[] { 0, 15, 30, 45 }); comboMTo.Items.AddRange(new object[] { 0, 15, 30, 45 }); textHistory.Text = Configuration.History.ToString(); textInterval.Text = Configuration.Interval.ToString(); textWorkingFolder.Text = Configuration.WorkingFolder; Configuration.GetOperationalFrom(out int h, out int m); comboHFrom.SelectedItem = h; comboMFrom.SelectedItem = m; Configuration.GetOperationalTo(out h, out m); comboHTo.SelectedItem = h; comboMTo.SelectedItem = m; Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location); this.Icon = appIcon; this.notifyIcon.Icon = appIcon; notifyIcon.BalloonTipIcon = ToolTipIcon.None; m_bdstimer = new BDSTimer(TimerFiredEvent, Configuration.StartTime, Configuration.EndTime, TimeSpan.FromMinutes(Configuration.Interval)); m_bdstimer.Start(); m_firstStart = false; m_powerHandler = new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); SystemEvents.PowerModeChanged += m_powerHandler; SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvent_SessionEndingHandler); }
public int ModeChanged(bool ignore) { var count = 0; PowerModeChangedEventHandler handler = (s, e) => ++ count; var mock = new PowerModeContext(Power.Mode); Power.Configure(mock); mock.IgnoreStatusChanged = ignore; Power.ModeChanged += handler; mock.Mode = PowerModes.Resume; // Resume -> Resume mock.Mode = PowerModes.StatusChange; // Resume -> StatusChange mock.Mode = PowerModes.Suspend; // StatusChange -> Suspend mock.Mode = PowerModes.Suspend; // Suspend -> Suspend mock.Mode = PowerModes.StatusChange; // Suspend -> StatusChange mock.Mode = PowerModes.Resume; // StatusChange -> Resume Power.ModeChanged -= handler; mock.Mode = PowerModes.Suspend; mock.Mode = PowerModes.Resume; return(count); }
public void SignalsPowerModeChanged(int pmEvent, PowerModes powerMode) { bool changed = false; PowerModeChangedEventArgs args = null; PowerModeChangedEventHandler changedHandler = (o, e) => { changed = true; args = e; }; SystemEvents.PowerModeChanged += changedHandler; try { SendMessage(pmEvent); Assert.True(changed); Assert.NotNull(args); Assert.Equal(powerMode, args.Mode); } finally { SystemEvents.PowerModeChanged -= changedHandler; } }
public static void add_PowerModeChanged (PowerModeChangedEventHandler value) { }
/// <summary> /// The main thread /// </summary> void UploadPhotoList() { // If a folder is just a date (yyyy-mm-dd), we don't upload it - we only do so if // some text has been added to provide a meaningful name. This regex extracts the // meaningful part of the name in group 1 Regex alt = new Regex(@"^[\d- ]*(.*?)$", RegexOptions.Compiled); PowerModeChangedEventHandler eh = null; if (Settings.Default.WaitLogging) { Log("Adding power handler"); eh = new PowerModeChangedEventHandler(delegate(object sender, PowerModeChangedEventArgs e) { Log("Power " + e.Mode); }); SystemEvents.PowerModeChanged += eh; } try { do { try { Form.Log("Starting"); Form.Sets = Sets = new Dictionary<string, Set>(); Form.AltSets = AltSets = new Dictionary<string, Set>(); Form.ClearFolders(); Form.LogError(""); foreach (string folder in Directory.EnumerateDirectories(Settings.Default.Folder)) { if (TaskCancelled()) return; Form.Status("Loading folder {0}", folder); Set s = new Set(folder); if (s.Photos.Count > 0) { Match m = alt.Match(s.Folder.Name); if (m.Success) { // The folder name starts with a date string key = m.Groups[1].Value; if (key == "") { Form.Error("Ignored folder {0} with no description", folder); continue; } // Index set by name part only, in case date has been removed from Flickr set name AltSets[key] = s; } Sets[s.Folder.Name] = s; Form.AddFolder(s); } } // Have finished building local folder list Form.Status(""); // Now compute hashes foreach (Set s in Sets.Values) { if (TaskCancelled()) return; Form.Status("Checking folder {0}", s.Folder.Name); int i = 0; foreach (PhotoInfo p in s.Photos) { if (TaskCancelled()) return; p.LoadHash(); Form.UpdatePhoto(s, i++); } } Form.Status(""); if (Settings.Default.AccessToken == null) { Form.Log("Not logged in"); return; } // Wait until start time (if a start time is specified) if (Wait(false)) return; // Prevent computer going to sleep during upload uint executionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED); if (executionState == 0) Form.LogError("SetThreadExecutionState failed."); try { Form.Status("Downloading Flickr sets"); // Get Flickr sets PhotosetCollection sets = Flickr.PhotosetsGetList(1, 1000); if (TaskCancelled()) return; foreach (Photoset set in sets) { Set ours; // Match Flickr sets to ours if (Sets.TryGetValue(set.Title, out ours) || AltSets.TryGetValue(set.Title, out ours)) { ours.Id = set.PhotosetId; } } foreach (Set s in Sets.Values) { if (TaskCancelled()) return; // Photos matched in the Flickr set HashSet<PhotoInfo> matched = new HashSet<PhotoInfo>(); bool changed = false; if (s.Id != null) { // There is a Flickr set - get Flickr's list of photos in the set Form.Status("Downloading photo details for set {0}", s.Folder.Name); foreach (Photo p in Flickr.PhotosetsGetPhotos(s.Id, PhotoSearchExtras.DateUploaded, 1, 1000)) { if (TaskCancelled()) return; // Match by Id, name, file name or description PhotoInfo info = s.Photos.FirstOrDefault(pf => pf.Flickr.Id == p.PhotoId || pf.Name == p.Title || pf.FileName == p.Title || pf.Description == p.Description); if (info != null) { matched.Add(info); if (info.Flickr.Uploaded != p.DateUploaded) changed = true; info.Flickr.Id = p.PhotoId; info.Flickr.Uploaded = p.DateUploaded; } } } // Now look for local photos not in the Flickr set foreach (PhotoInfo p in s.Photos) { if (!matched.Contains(p)) { if (p.Flickr.Id != null) { // Photo is already on Flickr, but is not in this set if (TaskCancelled()) return; if (DateTime.Now >= stop) { Form.Log("Reached stop time"); return; } try { Flickr.PhotosetsAddPhoto(s.Id, p.Flickr.Id); } catch { // Cannot add photo - perhaps it was deleted from Flickr - mark it as missing p.Flickr.Id = null; p.Flickr.Uploaded = null; changed = true; } } } } if (changed) Form.UpdateSet(s); // Upload remaining photos foreach (PhotoInfo p in s.Photos) { if (TaskCancelled()) return; if (p.Flickr.Id != null) continue; // Photo is already on Flickr if (DateTime.Now >= stop) { Form.Log("Reached stop time"); return; } Form.Log("Uploading {0}", p.FullName); try { if (TaskCancelled()) return; p.Flickr.Id = Flickr.UploadPicture(p.FullName, p.Name, p.Description, "", false, true, false); p.Flickr.Uploaded = DateTime.Now; if (changed) Form.UpdatePhoto(s, s.Photos.IndexOf(p)); if (s.Id == null) { Form.Log("Creating set {0}", s.Folder.Name); s.Id = Flickr.PhotosetsCreate(s.Folder.Name, p.Flickr.Id).PhotosetId; } else { Flickr.PhotosetsAddPhoto(s.Id, p.Flickr.Id); } } catch (Exception ex) { Form.LogError("Exception: {0}", ex); } } } Form.Status(""); Form.LogError("Completed"); } finally { if (executionState != 0) NativeMethods.SetThreadExecutionState(executionState); } } catch (Exception ex) { try { Form.LogError("Exception: {0}", ex); } catch { } } } while (!Wait(true)); } finally { if (eh != null) { Log("Removing power handler"); SystemEvents.PowerModeChanged -= eh; } } }
/// <summary> /// The main thread /// </summary> void UploadPhotoList() { // If a folder is just a date (yyyy-mm-dd), we don't upload it - we only do so if // some text has been added to provide a meaningful name. This regex extracts the // meaningful part of the name in group 1 Regex alt = new Regex(@"^[\d- ]*(.*?)$", RegexOptions.Compiled); PowerModeChangedEventHandler eh = null; if (Settings.Default.WaitLogging) { Log("Adding power handler"); eh = new PowerModeChangedEventHandler(delegate(object sender, PowerModeChangedEventArgs e) { Log("Power " + e.Mode); }); SystemEvents.PowerModeChanged += eh; } try { do { try { Form.Log("Starting"); Form.Sets = Sets = new Dictionary <string, Set>(); Form.AltSets = AltSets = new Dictionary <string, Set>(); Form.ClearFolders(); Form.LogError(""); foreach (string folder in Directory.EnumerateDirectories(Settings.Default.Folder)) { if (TaskCancelled()) { return; } Form.Status("Loading folder {0}", folder); Set s = new Set(folder); if (s.Photos.Count > 0) { Match m = alt.Match(s.Folder.Name); if (m.Success) { // The folder name starts with a date string key = m.Groups[1].Value; if (key == "") { Form.Error("Ignored folder {0} with no description", folder); continue; } // Index set by name part only, in case date has been removed from Flickr set name AltSets[key] = s; } Sets[s.Folder.Name] = s; Form.AddFolder(s); } } // Have finished building local folder list Form.Status(""); // Now compute hashes foreach (Set s in Sets.Values) { if (TaskCancelled()) { return; } Form.Status("Checking folder {0}", s.Folder.Name); int i = 0; foreach (PhotoInfo p in s.Photos) { if (TaskCancelled()) { return; } p.LoadHash(); Form.UpdatePhoto(s, i++); } } Form.Status(""); if (Settings.Default.AccessToken == null) { Form.Log("Not logged in"); return; } // Wait until start time (if a start time is specified) if (Wait(false)) { return; } // Prevent computer going to sleep during upload uint executionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED); if (executionState == 0) { Form.LogError("SetThreadExecutionState failed."); } try { Form.Status("Downloading Flickr sets"); // Get Flickr sets PhotosetCollection sets = Flickr.PhotosetsGetList(1, 1000); if (TaskCancelled()) { return; } foreach (Photoset set in sets) { Set ours; // Match Flickr sets to ours if (Sets.TryGetValue(set.Title, out ours) || AltSets.TryGetValue(set.Title, out ours)) { ours.Id = set.PhotosetId; } } foreach (Set s in Sets.Values) { if (TaskCancelled()) { return; } // Photos matched in the Flickr set HashSet <PhotoInfo> matched = new HashSet <PhotoInfo>(); bool changed = false; if (s.Id != null) { // There is a Flickr set - get Flickr's list of photos in the set Form.Status("Downloading photo details for set {0}", s.Folder.Name); foreach (Photo p in Flickr.PhotosetsGetPhotos(s.Id, PhotoSearchExtras.DateUploaded, 1, 1000)) { if (TaskCancelled()) { return; } // Match by Id, name, file name or description PhotoInfo info = s.Photos.FirstOrDefault(pf => pf.Flickr.Id == p.PhotoId || pf.Name == p.Title || pf.FileName == p.Title || pf.Description == p.Description); if (info != null) { matched.Add(info); if (info.Flickr.Uploaded != p.DateUploaded) { changed = true; } info.Flickr.Id = p.PhotoId; info.Flickr.Uploaded = p.DateUploaded; } } } // Now look for local photos not in the Flickr set foreach (PhotoInfo p in s.Photos) { if (!matched.Contains(p)) { if (p.Flickr.Id != null) { // Photo is already on Flickr, but is not in this set if (TaskCancelled()) { return; } if (DateTime.Now >= stop) { Form.Log("Reached stop time"); return; } try { Flickr.PhotosetsAddPhoto(s.Id, p.Flickr.Id); } catch { // Cannot add photo - perhaps it was deleted from Flickr - mark it as missing p.Flickr.Id = null; p.Flickr.Uploaded = null; changed = true; } } } } if (changed) { Form.UpdateSet(s); } // Upload remaining photos foreach (PhotoInfo p in s.Photos) { if (TaskCancelled()) { return; } if (p.Flickr.Id != null) { continue; // Photo is already on Flickr } if (DateTime.Now >= stop) { Form.Log("Reached stop time"); return; } Form.Log("Uploading {0}", p.FullName); try { if (TaskCancelled()) { return; } p.Flickr.Id = Flickr.UploadPicture(p.FullName, p.Name, p.Description, "", false, true, false); p.Flickr.Uploaded = DateTime.Now; if (changed) { Form.UpdatePhoto(s, s.Photos.IndexOf(p)); } if (s.Id == null) { Form.Log("Creating set {0}", s.Folder.Name); s.Id = Flickr.PhotosetsCreate(s.Folder.Name, p.Flickr.Id).PhotosetId; } else { Flickr.PhotosetsAddPhoto(s.Id, p.Flickr.Id); } } catch (Exception ex) { Form.LogError("Exception: {0}", ex); } } } Form.Status(""); Form.LogError("Completed"); } finally { if (executionState != 0) { NativeMethods.SetThreadExecutionState(executionState); } } } catch (Exception ex) { try { Form.LogError("Exception: {0}", ex); } catch { } } } while (!Wait(true)); } finally { if (eh != null) { Log("Removing power handler"); SystemEvents.PowerModeChanged -= eh; } } }
public static void add_PowerModeChanged(PowerModeChangedEventHandler value) { }