62,074
社区成员
发帖
与我相关
我的任务
分享
需求是BC各自执行各自,无需等待对方完成,C需要每隔固定时间执行一次。 C执行的时候当前a的值 历史 值不 需要 。谢谢!
using System;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
while (true)
{
for (int i = 0; i < 100; i++)
{
A(i.ToString());
}
}
}
public static void A(string a)
{
B(a);
C(a); //需求BC各自执行各自,无需等待对方完成,C需要每隔固定时间执行一次
}
public static void B(string a)
{
Console.WriteLine(a);
System.Threading.Thread.Sleep(1000);
}
public static void C(string a)
{
Console.WriteLine(a);
System.Threading.Thread.Sleep(3000);
}
}
}
C函数是3s执行一次,但不是当前a的值, 是0、1、2、3顺序执行的。
B函数我要求是1s执行一次,但实际一秒钟执行了多次。
@正怒月神 我之前尝试了,没成功,不是我想要的结果。
```c#
using System;
namespace ConsoleApp1
{
internal class asfssss1234
{
static void Main(string[] args)
{
while (true)
{
for (int i = 0; i < 100; i++)
{
A(i.ToString());
}
}
}
public static void A(string a)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Interval = 1000;
timer.AutoReset = true;
timer.Start();
timer.Elapsed += new System.Timers.ElapsedEventHandler(B);
C(a); //需求BC各自执行各自,无需等待对方完成,C需要每隔固定时间执行一次
}
public static void B(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine(DateTime.Now.Second.ToString() + ","+"我是B");
}
public static void C(string a)
{
Console.WriteLine(DateTime.Now.Second.ToString() + ","+a);
System.Threading.Thread.Sleep(3000);
}
}
}
```
定时器,Timer去看一下