Question

I have a class that is running a process in a thread and whenever I try to use an IEnumerable method on a List property on the class, it zeros out the list or makes it null. My other class properties retain their values. I have tried other types of IEnumerable derived classes including ConcurrentQueue and ConcurrentStack, and all of the derived classes seem to have the same issue. I have tried researching this for days to no avail. Any help here would be greatly appreciated.

namespace ThreadProblem
{
public class TestMyClass
{

    public void StartTest()
    {
        MyClass testX = new MyClass(new List<BuildParam>() { BuildParam.value1, BuildParam.value2, BuildParam.value3 },"These are great comments");
        testX.TestThread();

        Console.WriteLine("testX has {0} members in the Build Parameters list",testX.BuildParameters.Count());
    }


}

public class MyClass
{
    public List<BuildParam> BuildParameters { get; set; }
    public string Comments { get; set; }


    public MyClass(List<BuildParam> parameters, string comments)
    {
        BuildParameters = parameters;
        Comments = comments;
    }

    public void TestThread()
    {
        Thread testThread = new Thread(new ThreadStart(executeProcess));
        testThread.Start();

        Thread testThread2 = new Thread(new ThreadStart(executeProcess2));
        testThread2.Start();
    }

    void executeProcess()
    {
        try
        {
            Console.WriteLine("Calling process containing IEnumerable extension method - Parameter count {0}", BuildParameters.Count());
            Console.WriteLine("Calling process containing IEnumerable extension method - Comments: {0}", Comments);

            //here is what seems to cause the issue....
            string paramsList = String.Format("Calling process containing IEnumerable extension method - These parameters will be used: {0}", String.Join(", ", BuildParameters.Select(t => t.ToString())));

            foreach (var item in BuildParameters)
            {
                //do something
                Console.WriteLine("Calling process containing IEnumerable extension method - Howdy, I am {0}", item.ToString());
            }
            Console.WriteLine("Calling process containing IEnumerable extension method - Comments Again: {0}", Comments);
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }


    void executeProcess2()
    {
        try
        {
            Console.WriteLine("Calling process not containing IEnumerable extension method - Comments: {0}", Comments);
            foreach (var item in BuildParameters)
            {
                //do something
                Console.WriteLine("Calling process not containing IEnumerable extension method - Hi, I am {0}", item.ToString());
            }
            Console.WriteLine("Calling process not containing IEnumerable extension method - Comments again: {0}", Comments);
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

}    

public enum BuildParam
{
    value1,
    value2,
    value3
}
}

This produces output similar to this:

Calling process not containing IEnumerable extension method - Comments: These are great comments
testX has 3 members in the Build Parameters list
Calling process not containing IEnumerable extension method - Hi, I am value1
Calling process not containing IEnumerable extension method - Hi, I am value2
Calling process not containing IEnumerable extension method - Hi, I am value3
Calling process not containing IEnumerable extension method - Comments again: These are great comments
Calling process containing IEnumerable extension method - Parameter count 3
Calling process containing IEnumerable extension method - Comments: These are great commentscomments
Was it helpful?

Solution

I suspect this line is throwing an exception:

//here is what seems to cause the issue....
string paramsList = String.Format("Calling process containing IEnumerable extension method - These parameters will be used: {0}", String.Join(", ", BuildParameters.Select(t => t.ToString())));

Wrap that in a try/catch and output the exception information:

string paramsList;
try
{
    string parms = string.Join(", ",  BuildParameters.Select(t => t.ToString())));
    paramsList = String.Format(
        "Calling process containing IEnumerable extension method - These parameters will be used: {0}",
        parms);
    Console.WriteLine(paramsList);
}
catch (Exception ex)
{
    Console.WriteLine("Exception!");
    Console.WriteLine(ex.ToString());
}

Or you could wrap your entire thread proc in that. Be careful, though. You shouldn't blindly catch all exceptions like this in production code. It's nice for debugging, but don't leave it in the production code.

OTHER TIPS

Your executeProcess thread aborts with an exception. Just rethrowing it is not enough. Try adding this, for example in your main method:

AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e)
{
    // TODO: Here, you can access the exception as a parameter of e.
    // So you can debug it, log it, output it, store it in a file, whatever.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top