gpt4 book ai didi

c# - 不支持 sta 线程上多个句柄的 waitall

转载 作者:行者123 更新时间:2023-11-30 17:05:52 25 4
gpt4 key购买 nike

<分区>

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

waitall for multiple handles on sta thread is not supported

这是代码:-

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

如果你能帮助我,我会感谢你的支持

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com