不支持在一个线程上对多个句柄进行等待

本文关键字:句柄 等待 线程 一个 不支持 | 更新日期: 2023-09-27 18:09:25

大家好,当我运行我的应用程序时,我有这个异常。我在。net 3.5上工作,所以我不能使用Task

不支持在一个线程上执行多个句柄的waitall

这是代码:-

private void ThreadPopFunction(ContactList SelectedContactList, List<User> AllSelectedUsers)
{
        int NodeCount = 0;
        AllSelectedUsers.EachParallel(user =>
        {
            NodeCount++;
            if (user != null)
            {
                if (user.OCSEnable)
                {
                    string messageExciption = string.Empty;
                    if (!string.IsNullOrEmpty(user.SipURI))
                    {
                        //Lync.Lync.Lync lync = new Lync.Lync.Lync(AdObjects.Pools);
                        List<Pool> myPools = AdObjects.Pools;
                        if (new Lync.Lync.Lync(myPools).Populate(user, SelectedContactList, out messageExciption))
                        {
                        }
                    }
                }
            }
        });
}

这是我的扩展方法,我用它来处理多线程

public static void EachParallel<T>(this IEnumerable<T> list, Action<T> action)
{
    // enumerate the list so it can't change during execution
    // TODO: why is this happening?
    list = list.ToArray();
    var count = list.Count();
    if (count == 0)
    {
        return;
    }
    else if (count == 1)
    {
        // if there's only one element, just execute it
        action(list.First());
    }
    else
    {
        // Launch each method in it's own thread
        const int MaxHandles = 64;
        for (var offset = 0; offset <= count/MaxHandles; offset++)
        {
            // break up the list into 64-item chunks because of a limitiation in WaitHandle
            var chunk = list.Skip(offset*MaxHandles).Take(MaxHandles);
            // Initialize the reset events to keep track of completed threads
            var resetEvents = new ManualResetEvent[chunk.Count()];
            // spawn a thread for each item in the chunk
            int i = 0;
            foreach (var item in chunk)
            {
                resetEvents[i] = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(new WaitCallback((object data) =>
                {
                    int methodIndex =
                        (int) ((object[]) data)[0];
                    // Execute the method and pass in the enumerated item
                    action((T) ((object[]) data)[1]);
                    // Tell the calling thread that we're done
                    resetEvents[methodIndex].Set();
                }), new object[] {i, item});
                i++;
            }
            // Wait for all threads to execute
            WaitHandle.WaitAll(resetEvents);
        }
    }
}

如果你能帮助我,我将感激你的支持

不支持在一个线程上对多个句柄进行等待

好吧,因为你使用的是。net 3.5,所以你不能使用。net 4.0引入的TPL。

无论是否STA线程,在您的情况下,有一种比WaitAll更简单/有效的方法。你可以简单地拥有一个计数器和一个唯一的WaitHandle。下面是一些代码(现在不能测试,但应该没问题):
// No MaxHandle limitation ;)
for (var offset = 0; offset <= count; offset++)
{
    // Initialize the reset event
    var resetEvent = new ManualResetEvent();
    // Queue action in thread pool for each item in the list
    int counter = count;
    foreach (var item in list)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback((object data) =>
                      {
                          int methodIndex =
                              (int) ((object[]) data)[0];
                          // Execute the method and pass in the enumerated item
                          action((T) ((object[]) data)[1]);
                          // Decrements counter atomically
                          Interlocked.Decrement(ref counter);
                          // If we're at 0, then last action was executed
                          if (Interlocked.Read(ref counter) == 0)
                          {
                              resetEvent.Set();
                          }
                      }), new object[] {i, item});
    }
    // Wait for the single WaitHandle
    // which is only set when the last action executed
    resetEvent.WaitOne();
}

还供参考,ThreadPool.QueueUserWorkItem不会在每次调用时生成一个线程(我说的是,因为评论"为块中的每个项目生成一个线程")。它使用一个线程池,所以它主要重用现有的线程。

对于像我这样需要使用示例的人。Ken2k的解决方案是伟大的,它的工作,但有一些修正(他说他没有测试它)。以下是ken2k的工作示例(对我有用):

// No MaxHandle limitation ;)
for (var offset = 0; offset <= count; offset++)
{
    // Initialize the reset event
    var resetEvent = new ManualResetEvent(false);
    // Queue action in thread pool for each item in the list
    long counter = count;
    // use a thread for each item in the chunk
    int i = 0;
    foreach (var item in list)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback((object data) =>
                      {
                          int methodIndex =
                              (int) ((object[]) data)[0];
                          // Execute the method and pass in the enumerated item
                          action((T) ((object[]) data)[1]);
                          // Decrements counter atomically
                          Interlocked.Decrement(ref counter);
                          // If we're at 0, then last action was executed
                          if (Interlocked.Read(ref counter) == 0)
                          {
                              resetEvent.Set();
                          }
                      }), new object[] {i, item});
    }
    // Wait for the single WaitHandle
    // which is only set when the last action executed
    resetEvent.WaitOne();
}

实际上有一种方法可以在。net 3.5中使用(至少是很好的一部分)TPL。有一个为rx项目做的后端。

你可以在这里找到:http://www.nuget.org/packages/TaskParallelLibrary