1

スレッドでプロセスを実行しているクラスがあり、クラスの List プロパティで IEnumerable メソッドを使用しようとすると、リストがゼロになるか、null になります。私の他のクラス プロパティは値を保持します。ConcurrentQueue や ConcurrentStack など、他のタイプの IEnumerable 派生クラスを試してみましたが、すべての派生クラスで同じ問題が発生しているようです。私はこれを何日も調査しようとしましたが、役に立ちませんでした。ここで何か助けていただければ幸いです。

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
}
}

これにより、次のような出力が生成されます。

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
4

2 に答える 2

1

この行が例外をスローしていると思われます:

//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())));

それを try/catch でラップし、例外情報を出力します。

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());
}

または、スレッド プロシージャ全体をその中にラップすることもできます。ただし、注意してください。プロダクション コードで、このようなすべての例外をやみくもにキャッチするべきではありません。デバッグには便利ですが、製品コードに残さないでください。

于 2013-07-24T23:45:06.573 に答える
0

あなたのexecuteProcessスレッドは例外で中止されます。ただ投げ返すだけでは不十分です。たとえば、メインメソッドにこれを追加してみてください:

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.
}
于 2013-07-24T23:32:55.190 に答える