I created a mip plugin for Xprotect smart client and I want to catch the close of the application in my plugin to do some cleaning
I have a class that inherits from BackgroundPlugin, there I override Close method and dispose my plugin but it doesn’t always get there or maybe it gets there but the application is closing too fast for me to clean
in addition, I have a class that inherits from PluginDefinition, there I also override the Close method and dispose my plugin and clean what I need, but here is the same, it doesn’t always get there or maybe it gets there but the application is closing too fast for me to clean
is there any other way to make sure I can clean up my code before the process die? or is there a way to prevent the process from dyeing till I finish cleaning up?
I am sorry for the delayed reply. I have asked experts in Milestone Development. They said that they cannot reproduce the problem. Please see below -
I have created the following Background MIP Plugin:
internal class MyBackgroundPlugin : BackgroundPlugin
{
public override Guid Id { get; } = new Guid("{6D847C52-F6B3-4F99-A21E-569B8A50038A}");
public override string Name { get; } = "My Background Plugin";
public override List<EnvironmentType> TargetEnvironments { get; } = new List<EnvironmentType>
{
EnvironmentType.SmartClient
};
public override void Init()
{
System.Diagnostics.Trace.WriteLine("####### MyBackgroundPlugin.Init");
}
public override void Close()
{
System.Diagnostics.Trace.WriteLine("####### MyBackgroundPlugin.Close");
for (int i = 0; i < 10; i++)
{
System.Diagnostics.Trace.WriteLine($"####### Closing MIP plugin {i} ...");
Thread.Sleep(1000);
}
System.Diagnostics.Trace.WriteLine("####### MIP plugin closed!");
}
}
That spends 10 seconds in the Close method writing out trace messages. I have tried closing the Smart Client using the top-right ‘X’, ALT-F4 and also logging out the user. I all cases i get the expected behaviour and the following is writtend in debug output:
####### MyBackgroundPlugin.Close
####### Closing MIP plugin down 0 ...
####### Closing MIP plugin down 1 ...
####### Closing MIP plugin down 2 ...
####### Closing MIP plugin down 3 ...
####### Closing MIP plugin down 4 ...
####### Closing MIP plugin down 5 ...
####### Closing MIP plugin down 6 ...
####### Closing MIP plugin down 7 ...
####### Closing MIP plugin down 8 ...
####### Closing MIP plugin down 9 ...
####### MIP plugin closed!
If you still have the problem, can you please provide a source code project with a minimal implementation that can be used to reproduce and debug?
here is my code, this is the close method on the “MilestoneSmartClientPluginBackgroundPlugin : BackgroundPlugin” class
public override void Close()
{
_plugin.Dispose();
_stop = true;
for (int i = 0; i < 10; i++)
{
Logger.Instance.Info($"####### Closing MIP plugin {i} ...");
Thread.Sleep(1000);
}
}
and here is the dispose of my plugin (we have different logic for different projects):
public override void Dispose()
{
Logger.Instance.Info($"StopRecording");
mdControls.StopRecording();
mdControls.Pause();
Logger.Instance.Info($"Unsubscribing from messages");
UnsubscribeDDSMessage(typeof(ChangeStateMessage), HandleChangeStateMessage, _partition);
UnsubscribeDDSMessage(typeof(BookmarkMessage), HandleBookmarkCreateMessage, _partition);
}
and still, here is my log
2025-07-29 17:14:11.5275|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin.ReadAllConfigFiles|T-1|Logger Initialized
2025-07-29 17:14:11.5586|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin.ReadAllConfigFiles|T-1|Reading Config folder
2025-07-29 17:14:11.7995|WARN|Erezd921-Lap|Client|Bagira.ConfigurationManager.JsonWrapper.HandleIfFileExists|T-1|Configuration File Deleted.
2025-07-29 17:14:11.8585|INFO|Erezd921-Lap|Client|Bagira.ConfigurationManager.JsonWrapper.WriteToFile|T-1|File C:\Program Files\Milestone\XProtect Smart Client\MIPPlugins\MilestoneSmartClientPlugin\Config\LoggerConfiguration.json Created.
2025-07-29 17:14:11.8768|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin.GetGeneralConfig|T-1|Reading General Configuration...
2025-07-29 17:14:11.8768|DEBUG|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin+PluginFactory.GetAllPluginTypes|T-1|Assembly: MilestoneSmartClientPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
2025-07-29 17:14:11.8768|DEBUG|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin+PluginFactory.GetAllPluginTypes|T-1|Found 58 types, searching plugins that is subclass of BagiraPluginBase
2025-07-29 17:14:11.8768|DEBUG|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin+PluginFactory.GetAllPluginTypes|T-1|Found 4 plugins
2025-07-29 17:14:11.8768|DEBUG|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin.CreatePluginsInstances|T-1|looking for desired plugin: spikeplugin
2025-07-29 17:14:11.8768|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.LoadConfiguration|T-1|Reading Spike Configuration...
2025-07-29 17:14:11.9033|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.LoadConfiguration|T-1|[SUCCESS] - Configuration loaded.
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5901 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5902 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5903 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5904 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5905 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5915 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 5916 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 1 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.Spike.SpikePlugin.ExtractStationsFromConfiguration|T-1|station 2 found in configuration
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin.CreatePluginsInstances|T-1|Instance of SpikePlugin created.
2025-07-29 17:14:11.9133|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin..ctor|T-1|Initializing SpikePlugin
2025-07-29 17:14:12.3889|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Communicator.DDSCommunicator..ctor|T-1|DDS Communication initialized.
2025-07-29 17:14:12.3889|TRACE|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.BagiraPluginBase.SubscribeMessageToDDS|T-1|Subscribed to messageType Bagira.DDS.DM.Management.ChangeStateMessage in partition -
2025-07-29 17:14:12.4054|TRACE|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Plugins.BagiraPluginBase.SubscribeMessageToDDS|T-1|Subscribed to messageType Bagira.DDS.DM.IOS.BookmarkMessage in partition -
2025-07-29 17:14:12.4054|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Bagira.Managment.ComponentManager.GetStationId|T-1|Component Manager Loaded with StationId - 4401
2025-07-29 17:14:12.4054|INFO|Erezd921-Lap|Client|MilestoneSmartClientPlugin.Background.MilestoneSmartClientPluginBackgroundPlugin..ctor|T-1|Plugin is now running.
from loading the client.exe till killing it
BTW, if I implement the close like this:
public override void Close()
{
for (int i = 0; i < 10; i++)
{
Logger.Instance.Info($"####### Closing MIP plugin {i} ...");
Thread.Sleep(1000);
}
_plugin.Dispose();
_stop = true;
}
then the "closing mip’ log is there but "“StopRecording” is not… so it just not finishing everything probably