I have a big problem with my code i allways get a IO Exception and i don´t know why... I use a StreamWriter...
internal void SaveOwner(Owner o)
{
StreamWriter w = new StreamWriter(path, true);
if (o != null)
w.WriteLine(o.ToFileString());
w.Close();
}
Pleace can anyone help me i don´t konw i have tried everything what i konw..!? It allways says that a other process use the file. IO Exception - file used by another process Befor i call the Method i asked if o != null
the code is in C#
I believe this is caused because of multi-threading due to that I was able to reproduce your issue by that.
You can simply wrap a lock (Use a static object.) and for goodness sake use the using
keyword to wrap your stream.
static object syncRoot = "";
...
void SaveOwner(Owner o)
{
lock (syncRoot)
{
using (StreamWriter w = new StreamWriter(path, true))
{
if (o != null)
w.WriteLine(o.ToFileString());
}
}
}