ralphtrent
Programmer
Hello
I have the following situation. I have a windows service that multi threads a process. this process will then do its thing and then call a static class to write to a file. the file writing happens in an other object. In that file writer, I have the code to write call a function to create the stream, then return to the original code, write the data, close the file and destroy the object. For every write, it creates the stream. The problem is when I go to close the file, I get Object Reference not set to an Instance of an object Error .
Here is code flow:
I keep excepting in my finally block when I go to close the file. I think the threads are stepping on themselves and causing this but I have no clue.
I can provide code but it may be too much.
Here is my createStream Code
but here is my code that is excepting:
Any help is appreciated.
Thanks,
RalphTrent
I have the following situation. I have a windows service that multi threads a process. this process will then do its thing and then call a static class to write to a file. the file writing happens in an other object. In that file writer, I have the code to write call a function to create the stream, then return to the original code, write the data, close the file and destroy the object. For every write, it creates the stream. The problem is when I go to close the file, I get Object Reference not set to an Instance of an object Error .
Here is code flow:
Code:
Windows Service
Threaded Code
Transaction that will do the work
Call Static Method with in current object
Call Static Method within current class
Create external object and call method
Call Method in current class
Call Method to create StreamWriter in current class
Write To Stream in try\catch
In finally close file and destroy object
I keep excepting in my finally block when I go to close the file. I think the threads are stepping on themselves and causing this but I have no clue.
I can provide code but it may be too much.
Here is my createStream Code
Code:
private void createStream(string messageLogPath)
{
StreamWriter gswMessageLog;
if (String.Compare(messageLogPath, String.Empty) == 0)
{
throw new FileNotFoundException("Can not save to output log. No output log file was specified.");
}
try
{
gswMessageLog = new StreamWriter(messageLogPath, true);
gtwMessageLog = StreamWriter.Synchronized(gswMessageLog);
}
catch (System.IO.IOException)
{
try { gswMessageLog = new StreamWriter(messageLogPath, true); }
catch { throw; }
}
}
but here is my code that is excepting:
Code:
private void saveMessage(string messageLogPath, string message)
{
try
{
createStream(messageLogPath);
foreach (string lstrLine in message.Split('\n'))
{
if (String.Compare(lstrLine, String.Empty) == 1)
{
gtwMessageLog.Write(dateForStartOfLine());
gtwMessageLog.Write(Convert.ToString('\t'));
gtwMessageLog.Write(string.Format("{0}:{1}", System.Diagnostics.Process.GetCurrentProcess().Id, System.Threading.Thread.CurrentThread.ManagedThreadId));
gtwMessageLog.Write(Convert.ToString('\t'));
gtwMessageLog.WriteLine(lstrLine);
}
}
}
catch { throw; }
finally
{
gtwMessageLog.Close(); [green]// THIS IS WHERE I FAIL[/green]
gtwMessageLog = null;
}
}
Any help is appreciated.
Thanks,
RalphTrent