110,534
社区成员
发帖
与我相关
我的任务
分享
static async Task a()
{
}
static async Task b()
{
}
static async Task c()
{
}
static async void main()
{
await a();
await b();
await c();
}
而你的思维方式是static async Task a()
{
}
static async Task b()
{
}
static async Task c()
{
}
static async void main()
{
a();
b();
c();
}
这样的。
所以会抄代码的形式不行,要懂得分析实际流程,否则从一开始就入坑了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
namespace taskAndAwait
{
public class TimerEventArgs : EventArgs
{
private long clockFrequency;
public long ClockFrequency
{
get { return clockFrequency; }
}
private long previousTickCount;
public long PreviousTickOunt
{
get { return previousTickCount; }
}
private long currentTickCount;
public long CurrentTickCount
{
get { return currentTickCount; }
}
public TimerEventArgs(long clockFreq, long prevTick, long currTick)
{
this.clockFrequency = clockFreq;
this.previousTickCount = prevTick;
this.currentTickCount = currTick;
}
}
/// <summary>
/// 高精度定时器事件委托
/// </summary>
public delegate void HighTimerEventHandler(object sender, TimerEventArgs e);
/// <summary>高精度定时器类</summary>
public class HighAccurateTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
public event HighTimerEventHandler Elapsed;
Thread thread;
private object threadLock = new object();
private long clockFrequency = 0;
private long intevalTicks = 0;
private long nextTriggerTime = 0;
private double intervalMs;
private bool AskExitThread = false;
/// <summary>
/// 定时器间隔
/// </summary>
public double Interval
{
get
{
return intervalMs;
}
set
{
intervalMs = value;
}
}
private bool enable;
/// <summary>
/// 启动定时器标志
/// </summary>
public bool Enabled
{
get
{
return enable;
}
set
{
if (value && enable == false)
Start(); //启动线程
else if (value == false && enable)
Destroy(); //关闭定时器
enable = value;
if (value == true)
{
intevalTicks = (long)(((double)intervalMs / (double)1000) * (double)clockFrequency);
long currTick = 0;
GetTick(out currTick);
nextTriggerTime = currTick + intevalTicks;
}
}
}
/// <summary>
/// 构造函数
/// </summary>
public HighAccurateTimer()
{
/*
if (QueryPerformanceFrequency(out clockFrequency) == false)
{
return;
}
this.intervalMs = 1000;
this.enable = false;
thread = new Thread(new ThreadStart(ThreadProc));
thread.Name = "HighAccuracyTimer";
thread.Priority = ThreadPriority.Highest;
thread.Start();
*/
intervalMs = 1000;
enable = false;
QueryPerformanceFrequency(out clockFrequency);
}
/// <summary>
/// 启动线程
/// </summary>
public void Start()
{
AskExitThread = false;
if (QueryPerformanceFrequency(out clockFrequency) == false)
{
return;
}
if (intervalMs == 0)
this.intervalMs = 1000;
this.enable = false;
thread = new Thread(new ThreadStart(ThreadProc));
thread.Name = "HighAccuracyTimer";
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
/// <summary>
/// 进程主程序
/// </summary>
private void ThreadProc()
{
long currTime;
GetTick(out currTime);
nextTriggerTime = currTime + intevalTicks;
while (true)
{
while (currTime < nextTriggerTime)
{
GetTick(out currTime); //决定时钟的精度
if (AskExitThread)
break;
}
nextTriggerTime = currTime + intevalTicks;
if (Elapsed != null && enable == true)
{
Elapsed(this, new TimerEventArgs(clockFrequency, currTime - intevalTicks, currTime));
}
if (AskExitThread)
break;
}
}
/// <summary>
/// 获得当前时钟计数
/// </summary>
/// <param name="currentTickCount">时钟计数</param>
/// <returns>获得是否成功</returns>
public bool GetTick(out long currentTickCount)
{
if (QueryPerformanceCounter(out currentTickCount) == false)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 注销定时器
/// </summary>
public void Destroy()
{
enable = false;
AskExitThread = true;
try
{
thread.Abort();
}
catch { }
}
}
}
program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace taskAndAwait
{
class Program
{
static HighAccurateTimer timer1 = new HighAccurateTimer();
static void Main(string[] args)
{
timer1.Interval = 0.2;
timer1.Elapsed += show;
timer1.Enabled = true;
Console.ReadKey();
}
static int a = 0;
static async void show(object sender, TimerEventArgs e)
{
while (a < 10)
{
a++;
int teamp = a;
int t = await Task.Run(() => show1(teamp));
}
}
static int show1(int data)
{
Console.WriteLine(data);
return 1;
}
}
}
全部代码如上,希望知道解决方法的朋友能为我解惑,谢谢大家!