现在需要一个精度比较高的计时器,用C#自带的3个TIMER好像精度不够啊所以需要一个精度比较高的计时器~~~由于我是新学的C#,很多东西不懂也不会写,网上的也不是很看的懂哪位高手能不能帮我写一个高精度的计时器啊~~最好就是代码我贴过来就能用的。。谢谢了!~
解决方案 »
- C#中使用FileStream类可以对里面的特定内容进行查找吗?(各位大侠帮忙)
- 怎么用C#做IE工具条...
- sql语句查询问题?
- 在你所熟悉的控件中,你认为,哪些控件适合用于输入数据,并说明适合用于输入什么类型的数据?哪些控件适合用于输出数据,并说明适合用于输出什么类型的数据?
- DataGridView里的列设置ReadOnly怎么不起作用?
- ·····有没有什么方法可以修改诸如MessageBox之类的窗体的外观?·····
- 小心IDE调试陷阱
- 新闻发布系统(增删改页面)控件ID
- ASPX页面如何把参数传递到crystal report ,再在crystal report 如何把参数传递给在报表中用到的存储过程?
- 偶安装vs7,到了stratpage时候,发现所有install不响应鼠标,鼠标悬停不出现可点击的表示,偶该怎么版呢?/牛虻
- 关于asp:FileUpload
- 关于installutil无法创建windows服务的问题,急急急!
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;namespace TestPerformance
{
internal sealed class PerfTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount); [DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency); private long startTime, stopTime;
private long freq; // Constructor
public PerfTimer()
{
startTime = 0;
stopTime = 0; if (QueryPerformanceFrequency(out freq) == false)
{
// high-performance counter not supported
throw new Win32Exception();
}
} // Start the timer
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
} // Stop the timer
public void Stop()
{
QueryPerformanceCounter(out stopTime);
} // Returns the duration of the timer (in seconds)
public double Duration
{
get
{
return (double)(stopTime - startTime) / (double)freq;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to continue");
Console.ReadKey(); PerfTimer pt = new PerfTimer(); // create a new PerfTimer object
pt.Start(); // start the timer for(int i=0; i<10000; i++)
{
string s = string.Format("This is test round {0}", i);//"This is test round "+i.ToString();
Console.WriteLine(s);
} pt.Stop();
Console.WriteLine("Duration: {0} second(s)"n", pt.Duration); // print the duration of the timed code Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}}