先上效果图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GoldRootManage20230505
{
internal class Program
{
public static int count = 0;
public static System.Timers.Timer timer;
public static void Main(string[] args)
{
// 定义俩条子线程
Thread thread1 = new Thread(Run1);
Thread thread2 = new Thread(Run2);
// 开始线程
thread1.Start();
thread2.Start();
// 主线程结束时不关闭控制台
Console.WriteLine("我也不知道我是多少号线程了");
Console.ReadLine();
}
// 线程1
public static void Run1()
{
// 使用全局定时器,不手动stop就无限执行
timer = new System.Timers.Timer(1000);
// Run11是定时执行方法
timer.Elapsed += new System.Timers.ElapsedEventHandler(Run11);
// 是否无限执行
timer.AutoReset = true;
// 是否开启
timer.Enabled = true;
}
public static void Run11(object source, System.Timers.ElapsedEventArgs e)
{
count++;
if (count == 5)
{
// 关闭并释放定时器
timer.Stop();
timer.Dispose();
}
Console.WriteLine("我也有可能是三号线程");
}
//线程2
public static void Run2()
{
Console.WriteLine("我是二号线程");
}
}
}