gpt4 book ai didi

C# 等待所有线程完成执行

转载 作者:太空狗 更新时间:2023-10-30 00:30:39 24 4
gpt4 key购买 nike

我试过这个...

public ArrayList GetAllObjectAttributes()
{
List<Task> taskList = new List<Task>();
ArrayList allObjectAttributes = new ArrayList();
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder));}));
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)); }));
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)); }));
taskList.Add(Task.Factory.StartNew(() => { allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)); }));
Task.WaitAll(taskList.ToArray());
return allObjectAttributes;
}

还有这个……

public ArrayList GetAllObjectAttributes()
{
Thread[] threads = new Thread[4];
ArrayList allObjectAttributes = new ArrayList();
threads[0] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder)));
threads[1] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)));
threads[2] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)));
threads[3] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)));

foreach(Thread thread in threads)
{
thread.Start();
thread.Join();
}
return allObjectAttributes;
}

还有这个……

public ArrayList GetAllObjectAttributes()
{
Thread[] threads = new Thread[4];
ArrayList allObjectAttributes = new ArrayList();
threads[0] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder)));
threads[1] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)));
threads[2] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)));
threads[3] = new Thread(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)));

foreach(Thread thread in threads)
{
thread.Start();
}
while(threads[0].IsAlive || threads[1].IsAlive || threads[2].IsAlive || threads[3].IsAlive)
{
Thread.Sleep(500);
}
return allObjectAttributes;
}

我也试过 Spawn Multiple Threads for work then wait until all finished

我仍然在 allObjectAttributes 中的一个数组列表项中得到一个 null。

但是,当我这样做的时候

public ArrayList GetAllObjectAttributes()
{
ArrayList allObjectAttributes = new ArrayList();
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder)));
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile)));
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile)));
allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent)));
return allObjectAttributes;
}

我从来没有在 arraylist 项目中得到一个 null 项目。

  1. 等待所有线程完成我做错了什么?
  2. 任何其他建议,以便仅在所有 4 个线程完成执行后返回 arraylist。
private List GetObjectAttributes(TreeViewAttrs tv){    List objectAttributes = new List();    string command = "COMMAND_TO_EXECUTE";    if (command != "")    {        List results = RunCommand(command);        if (results == null) { return null; }        if (results.Count > 0)        {            foreach (string result in results)            {                if (!result.Contains("" + tv + ""))                {                    string[] res = reformatResponseString(result); //reformat the strings as per custom structure                    if (res != null) { objectAttributes.Add(res); }                }            }            return objectAttributes;        }    }    return null;}

最佳答案

通过使用线程安全集合(.NET 4.0 兼容)略有改进:

List<Task> taskList = new List<Task>();
ConcurrentBag<object> allObjectAttributes = new ConcurrentBag<object>();

taskList.Add(Task.Factory.StartNew(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Folder))));
taskList.Add(Task.Factory.StartNew(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.XMLFile))));
taskList.Add(Task.Factory.StartNew(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.TextFile))));
taskList.Add(Task.Factory.StartNew(() => allObjectAttributes.Add(GetObjectAttributes(TreeViewAttrs.Parent))));

Task.WaitAll(taskList.ToArray());

return allObjectAttributes;

另一种方法:在所有任务完成后使用 Task.Result(不再需要线程安全集合,因为只有一个线程修改 ArrayList):

Task<object>[] taskList = {
Task.Factory.StartNew(() => (object)GetObjectAttributes(TreeViewAttrs.Folder)),
Task.Factory.StartNew(() => (object)GetObjectAttributes(TreeViewAttrs.XMLFile)),
Task.Factory.StartNew(() => (object)GetObjectAttributes(TreeViewAttrs.TextFile)),
Task.Factory.StartNew(() => (object)GetObjectAttributes(TreeViewAttrs.Parent))
};

Task.WaitAll(taskList);

ArrayList allObjectAttributes = new ArrayList();

foreach (Task<object> task in taskList) {
allObjectAttributes.Add(task.Result);
}

return allObjectAttributes;

显着通过使用 Task.WhenAll 改进(仅限 .NET 4.5):

object[] allObjectAttributes = await Task.WhenAll(
Task.Run(() => GetObjectAttributes(TreeViewAttrs.Folder)),
Task.Run(() => GetObjectAttributes(TreeViewAttrs.XMLFile)),
Task.Run(() => GetObjectAttributes(TreeViewAttrs.TextFile)),
Task.Run(() => GetObjectAttributes(TreeViewAttrs.Parent))
);

return allObjectAttributes;

*注意:我使用 object 作为通用参数,因为您未指定 GetObjectAttributes 的返回类型。

关于C# 等待所有线程完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171770/

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