I create several System.IO.StreamWriter objects for writing data to files when my strategy runs, but by the time I get to the State.Terminated state, all of my stream writer objects are null and I cannot write to them before disposing of the objects properly.
Also, when I open the output files after the strategy finishes running, I get a warning that a process still has it open for editing. This indicates that the stream writer objects were in fact not disposed of properly. This was not a problem in NT7 when I handled disposal of the stream writers in the OnTermination method.
I really need these objects not to be null when State.Terminated state is detected. Or I would need a new state, such as State.BeforeTerminate.
Here are details of my code:
I declare the variables at the class level:
public class cmTimeSliceStatistics : Strategy
{
#region Variables
private System.IO.StreamWriter swStats; // For writing stats to text file
...
protected override void OnBarUpdate()
{
try
{
if (!initialized)
{
swStats = File.AppendText(statsFileName);
initialized = true;
}
...
protected override void OnStateChange()
{
if (State == State.Terminated)
{
if (swStats != null)
{
swStats.WriteLine("Write Final Summary Stats Here.");
swStats.Dispose();
swStats = null;
}
}
}

Comment