/// <summary>
 /// Tries to serialize the state object and save it using SCOM Agent API.
 /// </summary>
 /// <param name="state">Module state object to save.</param>
 /// <returns>Returns true if success, false otherwise.</returns>
 protected bool SavePreviousState()
 {
     if (ModuleState == null)
     {
         return(false);
     }
     lock (shutdownLock)
     {
         using (MemoryStream memoryStream = new MemoryStream())
         {
             BinaryFormatter binaryFormatter = new BinaryFormatter();
             try
             {
                 binaryFormatter.Serialize(memoryStream, ModuleState);
                 ModuleHost.SaveState(memoryStream.GetBuffer(), (int)memoryStream.Length);
                 return(true);
             }
             catch (Exception e)
             {
                 Global.logWriteException(e, this);
                 return(false);
             }
         }
     }
 }