Question

I am building a program in visual studio 2010 using C# .Net4.0

I have a function that takes in a url and try to get a response. Inside the function, I write to a log file stating successes and errors.

public void getResponse(string url, System.IO.StreamWriter writer)
{
    try
    {
        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();

        // Display the status.
        Console.WriteLine(response.Headers);
        writeToLog("Success: " + url, writer);
    }
    catch (WebException ex)
    {
        writeToLog("Error: " + ex.message, writer);
    }
}

I have a list of urls, and I use Parallel.ForEach to run multiple urls at once.

System.IO.StreamWriter writer = new System.IO.StreamWriter("program.log", true);
string[] urls = {url1, url2, url3, url4, etc}; //have around 30 urls
Parallel.ForEach(urls, url => getResponse(url, writer));

When I run the program, sometime I will get an error saying that program.log is being used by another program. This doesn't happen all the time, but once a while it will happen and the program will crash.

Question: What should I do to avoid this error? Or, is there an alternative way of doing it?

Was it helpful?

Solution

Because StreamWriter isn't thread-safe use a lock statement to lock the writing procedure so the other threads can't enter that line and cause a conflict: http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.100).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top