用C#做一个毫秒级别的定时器,能准群的实现每40MS调用一个函数?
c#自带的只有55ms的精确度,要想实现准确的,听说需要调用windowsAPI的函数,但我没用过windowsAPI,希望各位能给介绍下。现在发现.net越来越不好,需...
c#自带的只有55ms的精确度,要想实现准确的,听说需要调用windows API的函数,但我没用过windowsAPI,希望各位能给介绍下。现在发现.net越来越不好,需要其他的帮忙,想运行还得有它的环境
展开
3个回答
展开全部
哈哈 尺有所短啊
我用的一个
public class Counter
{
long elapsedCount = 0;
long startCount = 0;
/// <summary>
/// Start timing
/// </summary>
public void Start()
{
startCount = 0;
QueryPerformanceCounter(ref startCount);
}
/// <summary>
/// Stop timing
/// </summary>
public void Stop()
{
long stopCount = 0;
QueryPerformanceCounter(ref stopCount);
elapsedCount += (stopCount - startCount);
}
/// <summary>
/// Clear the timer
/// </summary>
public void Clear()
{
elapsedCount = 0;
}
/// <summary>
/// The elapsed time measured in seconds.
/// </summary>
public float Seconds
{
get
{
long freq = 0;
QueryPerformanceFrequency(ref freq);
return ((float)elapsedCount / (float)freq);
}
}
/// <summary>
/// The elapsed time measured in milliseconds.
/// </summary>
public float Milliseconds
{
get
{
return Seconds * 1000;
}
}
/// <summary>
/// Convert to a string
/// </summary>
/// <returns>The current elapsed time in seconds</returns>
public override string ToString()
{
return String.Format("{0} seconds", Seconds);
}
static long Frequency
{
get
{
long freq = 0;
QueryPerformanceFrequency(ref freq);
return freq;
}
}
static long Value
{
get
{
long count = 0;
QueryPerformanceCounter(ref count);
return count;
}
}
[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter(ref long
lpPerformanceCount);
[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceFrequency(ref long
lpFrequency);
}
我用的一个
public class Counter
{
long elapsedCount = 0;
long startCount = 0;
/// <summary>
/// Start timing
/// </summary>
public void Start()
{
startCount = 0;
QueryPerformanceCounter(ref startCount);
}
/// <summary>
/// Stop timing
/// </summary>
public void Stop()
{
long stopCount = 0;
QueryPerformanceCounter(ref stopCount);
elapsedCount += (stopCount - startCount);
}
/// <summary>
/// Clear the timer
/// </summary>
public void Clear()
{
elapsedCount = 0;
}
/// <summary>
/// The elapsed time measured in seconds.
/// </summary>
public float Seconds
{
get
{
long freq = 0;
QueryPerformanceFrequency(ref freq);
return ((float)elapsedCount / (float)freq);
}
}
/// <summary>
/// The elapsed time measured in milliseconds.
/// </summary>
public float Milliseconds
{
get
{
return Seconds * 1000;
}
}
/// <summary>
/// Convert to a string
/// </summary>
/// <returns>The current elapsed time in seconds</returns>
public override string ToString()
{
return String.Format("{0} seconds", Seconds);
}
static long Frequency
{
get
{
long freq = 0;
QueryPerformanceFrequency(ref freq);
return freq;
}
}
static long Value
{
get
{
long count = 0;
QueryPerformanceCounter(ref count);
return count;
}
}
[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceCounter(ref long
lpPerformanceCount);
[System.Runtime.InteropServices.DllImport("KERNEL32")]
private static extern bool QueryPerformanceFrequency(ref long
lpFrequency);
}
参考资料: 大方吧。出入江湖,多多关照
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果我没记错的话,在System.Thread命名空间下的有一个定时器类,类名叫Timer(注意不要和另一个命名空间下的Timer混淆),里面可以设置毫秒级定时操作。你可以找相关的帮助看看。
在很多.net开始的企业级应用windows服务中,常用到这个类做定时任务。
在很多.net开始的企业级应用windows服务中,常用到这个类做定时任务。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用StopWatch类
不用native API
不用native API
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询