在不同线程上执行2个任务错误C#

时间:2012-05-28 09:38:42

标签: c# threadpool nullreferenceexception

我正在尝试将字符串数组分成2,创建2个线程并在这2个线程上搜索这些数组。我知道我正在正确创建线程,搜索功能正确,但我仍然收到此错误:

System.NullReferenceException was unhandled
Object reference not set to an instance of an object.

以下是代码:

// Create first and second array:
string[] firstarray = pieces.Take(pieces.Length / 2).ToArray();
string[] secondarray = pieces.Skip(pieces.Length / 2).ToArray();
string[] results = new string[pieces.Length];
string inputword = textBox5.Text.Trim();

ManualResetEvent[] calls = new ManualResetEvent[2];
calls[0] = new ManualResetEvent(false);
calls[1] = new ManualResetEvent(false);

// Set 2 new threads:
ThreadPool.QueueUserWorkItem((t) => {
    // SearchThruPieces searches an array for a string and returns the items
    // that contains the string
    // SearchThruPieces(string[] pieces, string word);
    SearchThruPieces(firstarray, inputword).CopyTo(results, 0);
    calls[0].Set();
});

ThreadPool.QueueUserWorkItem((t) => {
    SearchThruPieces(secondarray, inputword).CopyTo(results, firstarray.Length);
    calls[1].Set();
});

WaitHandle.WaitAll(calls);
string firststring = results[0]; // This line throws an exception

现在我知道这个错误不涉及任何线程或SearchThruPieces方法,因为它在我使用时发生:

results[0]

它返回错误。

请帮我解决这个问题,或者告诉我他们是否是一个更好的方式来做我想要完成的事情。

由于

编辑:

以下是SearchThruPieces的代码:

delegate string[] Search(string[] pieces, string word);

        static Search SearchThruPieces = (string[] pieces, string word) =>
        {
            object locker = new object();
            lock (locker)
            {
                int a = 0;
                string[] afterpieces = new string[pieces.Length];
                for (int b = 0; b < (pieces.Length / 4); b++)
                {
                    if (pieces[a].Trim().ToLower().Contains(word.Trim().ToLower()) && pieces[a].Trim().Length > 0)
                    {
                        afterpieces[a] = pieces[a].Trim();
                        afterpieces[a + 1] = pieces[a + 1].Trim();
                        afterpieces[a + 2] = pieces[a + 2].Trim();
                        afterpieces[a + 3] = pieces[a + 3].Trim();
                    }

                    a += 4;
                }
                return afterpieces;
            }
        };

编辑2:

所以我设置了一个断点:

afterpieces[a] = pieces[a].Trim();

要查看它是否被调用,它确实被调用了。我认为错误与将结果[]设置为SearchThruPieces的结果有关。

2 个答案:

答案 0 :(得分:0)

(这在评论中发布时间过长 - 如有必要,稍后会删除)

好的,让我看看我是否做对了,这段代码

// Create first and second array:
string[] firstarray = pieces.Take(pieces.Length / 2).ToArray();
string[] secondarray = pieces.Skip(pieces.Length / 2).ToArray();
string[] results = new string[pieces.Length];
string inputword = textBox5.Text.Trim();

//ManualResetEvent[] calls = new ManualResetEvent[2];
//calls[0] = new ManualResetEvent(false);
//calls[1] = new ManualResetEvent(false);
//
//// Set 2 new threads:
//ThreadPool.QueueUserWorkItem((t) => {
//    // SearchThruPieces searches an array for a string and returns the items
//    // that contains the string
//    // SearchThruPieces(string[] pieces, string word);
//    SearchThruPieces(firstarray, inputword).CopyTo(results, 0);
//    calls[0].Set();
//});
//
//ThreadPool.QueueUserWorkItem((t) => {
//    SearchThruPieces(firstarray, inputword).CopyTo(results, firstarray.Length);
//    calls[1].Set();
//});
//
//WaitHandle.WaitAll(calls);
string firststring = results[0]; // This line throws an exception

仍然抛出异常?

答案 1 :(得分:0)

假设pieces中定义了元素,并假设您正在搜索的字符串实际上在pieces中,并假设搜索字符串在数组划分后实际位于secondarray ,然后results将为空,因为您的第二次ThreadPool来电再次通过firstarray,而不是secondarray