none
Management of Windows power management RRS feed

  • Question

  • Hi,

    I have developped a vsto plugin for Outlook in C#. This plugin uses a Timer in order to analyze regulary some email content and log counter (sended mails by day, number of recipients - including count of mailing list ...), ...

    But when the computer go on sleep mode (ex 20 minutes after session lock), I have some problem with Outlook when it restarts/wakes up (not systematic) :

    The computer wake up, I logon, but Outlook stay blocked (Not responding). I must kill the process and restart Outlook. When I delete the plugin, there is no problem.

    I have added some logs on my plugin, and I can see that when the computer go on sleep mode, all logs are stoped (logic !), and when it restarts and Outlook works, my plugin create new log entries. When Outlook is not responding, I have no logs from my plugin.

    Is there a best practice about plugin development to manage this use case (Windows sleeps and wakes up) in order to be sure that Outlook will be able to work nicely and be sure that the wake up will be done without error ?

    Best regards,

    Wednesday, June 19, 2019 8:17 AM

Answers

  • Hello Mathieu,

    Looks like you need to handle the SystemEvents.PowerModeChanged event which is fired when the user suspends or resumes the system. 

    Be aware that .NET includes four classes named Timer, each of which offers different functionality:

    • System.Timers.Timer (this topic): fires an event at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
    • System.Threading.Timer: executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
    • System.Windows.Forms.Timer (.NET Framework only): a Windows Forms component that fires an event at regular intervals. The component has no user interface and is designed for use in a single-threaded environment.
    • System.Web.UI.Timer (.NET Framework only): an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.

    Please remember to use the System.Windows.Forms.Timer . Outlook uses a single-threaded apartment model so you should access the Outlook object model from the main thread only.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    Wednesday, June 19, 2019 2:31 PM

All replies

  • Hello Mathieu,

    Looks like you need to handle the SystemEvents.PowerModeChanged event which is fired when the user suspends or resumes the system. 

    Be aware that .NET includes four classes named Timer, each of which offers different functionality:

    • System.Timers.Timer (this topic): fires an event at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
    • System.Threading.Timer: executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
    • System.Windows.Forms.Timer (.NET Framework only): a Windows Forms component that fires an event at regular intervals. The component has no user interface and is designed for use in a single-threaded environment.
    • System.Web.UI.Timer (.NET Framework only): an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval.

    Please remember to use the System.Windows.Forms.Timer . Outlook uses a single-threaded apartment model so you should access the Outlook object model from the main thread only.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    Wednesday, June 19, 2019 2:31 PM
  • Hi,

    Thank you very much for your answer (and sorry for the delay I did'nt receive any notification).

    I will change the timer class used, and also try to detect power changed to enable or disable my add-in (and also try to stop current process).

    I will update this thread in few days with results.

    Best regards,

    MACK Mathieu

    Monday, July 8, 2019 11:37 AM
  • Hi,

    The PowerModechangedEvent must be unregistered unless the application is closed or disposed.

    But in an add-in vsto, where can I detach the event Handler as the ThisAddIn_Shutdown will not raised this event.

    Thanks

    Monday, July 8, 2019 12:15 PM
  • Ok, I will do it in the finalizer of my class and add also the unregister statement in the dispose.
    Monday, July 8, 2019 12:19 PM
  • Hi,

    Detecting power management seem to work. I'have also changed the Timer class.

    I've deployed the solution on my Customer infrastructure and I will check if it correct the problem in his environment.

    Thanks for your help.

    Thursday, July 11, 2019 11:08 AM