用c#语言,结合循环语句和timer类,每0.1秒,打一行字“我爱你”,控制台应用程序就行。

来源:百度知道 编辑:UC知道 时间:2024/06/08 15:54:30
多谢各位,其实我想举一反三,我在做一个串口通信的项目,需要读取串口数据,以前一直是使用timer控件,然后设定的timer的频率是0.1,然后每隔0.1秒读取一次数据并保存,但是有人建议我用timer控件不好,所以我才想到提这个问题,来找出解决方法,我想办的是每0.1秒保存一次串口读取到的数据。 同时这个串口的数据每0.4秒还要用于波形显示,好像还需要多线程,希望达人帮忙指导下。

using System;
public class MyClass
{
public static void Main()
{
System.Threading.Timer timer = new System.Threading.Timer(delegate(object state){Console.WriteLine("I love you");},null,0,100);
while(true)
Console.ReadLine();
}
}

static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer(100);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = false;
aTimer.Enabled = true;
}
static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("我爱你");
}

using System;
using System.Timers;

namespace cstest
{
public class Timer1
{
private static System.Timers.Timer aTimer;

public static void Main()
{
// Create a timer with a 0.1 second interval.
aTimer = new System.Timers.Timer(