C# 如何在另一个线程中启动一个线程
我正在探索在另一个线程中启动一个线程的概念。这是我的代码,这是我目前正在开发的另一个程序的淡化版本,但是我发现第二级线程没有成功完成C# 如何在另一个线程中启动一个线程,c#,C#,我正在探索在另一个线程中启动一个线程的概念。这是我的代码,这是我目前正在开发的另一个程序的淡化版本,但是我发现第二级线程没有成功完成 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; namespace Consol
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication4
{
public class SomeClassA
{
public SomeClassA(string display)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(display);
}
}
public class MainSomeClassA
{
public List<SomeClassA> SomeClassaAList;
public List<Thread> ThreadList;
public MainSomeClassA()
{
ThreadList = new List<Thread>();
SomeClassaAList = new List<SomeClassA>();
for (int i = 0; i < 10; i++)
{
ThreadList.Add(new Thread(() => StartThread("Hello")));
}
WaitComplete();
}
public void WaitComplete()
{
bool AllThreadsAlive = true;
while (AllThreadsAlive)
{
AllThreadsAlive = false;
foreach (Thread t in ThreadList)
{
if (t.IsAlive)
{
AllThreadsAlive = true;
}
}
}
}
public void StartThread(string display)
{
SomeClassaAList.Add(new SomeClassA(display));
}
}
class Program
{
public static List<MainSomeClassA> MainSomeClassAList = new List<MainSomeClassA>();
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
MainSomeClassAList = new List<MainSomeClassA>();
List<Thread> ThreadList = new List<Thread>();
bool threadsAlive = true;
sw.Reset();
sw.Start();
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(AddToMainClassAList);
t.Start();
ThreadList.Add(t);
}
while (threadsAlive)
{
threadsAlive = false;
foreach (Thread t in ThreadList)
{
if (t.IsAlive)
{
threadsAlive = true;
}
}
}
sw.Stop();
Console.WriteLine("Elapsed Time: {0}", sw.ElapsedMilliseconds);
Console.ReadKey();
}
public static void AddToMainClassAList()
{
MainSomeClassAList.Add(new MainSomeClassA());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用系统诊断;
命名空间控制台应用程序4
{
公共类
{
公共SomeClassA(字符串显示)
{
系统线程线程睡眠(1000);
控制台写入线(显示);
}
}
公共类
{
公共列表SomeClassaAList;
公开名单;
公共类A()
{
ThreadList=新列表();
SomeClassaAList=新列表();
对于(int i=0;i<10;i++)
{
添加(新线程(()=>StartThread(“Hello”));
}
WaitComplete();
}
public void WaitComplete()
{
bool-AllThreadsAlive=true;
while(AllThreadsAlive)
{
AllThreadsAlive=false;
foreach(线程列表中的线程t)
{
如果(t.IsAlive)
{
AllThreadsAlive=true;
}
}
}
}
public void StartThread(字符串显示)
{
添加(新的SomeClassA(显示));
}
}
班级计划
{
public static List MainSomeClassAList=new List();
静态void Main(字符串[]参数)
{
秒表sw=新秒表();
MainSomeClassAList=新列表();
List ThreadList=新列表();
bool-threadsAlive=true;
sw.Reset();
sw.Start();
对于(int i=0;i<10;i++)
{
线程t=新线程(添加到列表);
t、 Start();
添加(t);
}
while(threadsAlive)
{
threadsAlive=false;
foreach(线程列表中的线程t)
{
如果(t.IsAlive)
{
threadsAlive=true;
}
}
}
sw.Stop();
WriteLine(“经过的时间:{0}”,sw.elapsedmillesons);
Console.ReadKey();
}
公共静态void AddToMainClassAList()
{
添加(新的MainSomeClassA());
}
}
}
上面的代码不会打印出“hello”并在未创建
SomeClassA
列表的情况下退出。代码的问题是,您从未启动内部线程。将构造函数更改为如下所示,它将工作:
public MainSomeClassA()
{
ThreadList = new List<Thread>();
SomeClassaAList = new List<SomeClassA>();
for (int i = 0; i < 10; i++)
{
ThreadList.Add(new Thread(() => StartThread("Hello")));
// Start thread here:
ThreadList[ThreadList.Count - 1].Start();
}
WaitComplete();
}
foreach (Thread thread in ThreadList)
{
thread.Join();
}