多媒体定时器在C#中断(前两个中断是坏的) [英] Multimedia timer interrupts in C# (first two interrupts are bad)

查看:205
本文介绍了多媒体定时器在C#中断(前两个中断是坏的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用多媒体计时器的1毫秒的分辨率,期限为10毫秒。问题是,多媒体计时器中断严重的前两个事件,我得到的1毫秒,这不是我想要什么区别。

I am using Multimedia timer with a resolution of 1 ms and a period of 10 ms. The problem is that the multimedia timer badly interrupts for the first two events as I get a difference of 1 ms , which is not what I want.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.mmtimer.Tick += new System.EventHandler(this.mmtimer_Tick); 
    }

    Multimedia.Timer mmtimer = new Multimedia.Timer();
    private void Form1_Load(object sender, EventArgs e)
    {
        mmtimer.Resolution = 1;
        mmtimer.Mode = Multimedia.TimerMode.Periodic;
        mmtimer.Period = 10;
        mmtimer.SynchronizingObject = this;
    }

    private void S_Click(object sender, EventArgs e)            
    {
        TD.Items.Clear();
        MT.Items.Clear();
        delta_MT.Items.Clear();
        double T = DateTime.Now.Hour * 60 * 60 * 1000 + DateTime.Now.Minute * 60 * 1000 + DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
        Point C = Cursor.Position;
        TD.Items.Add(C.ToString());
        MT.Items.Add(T.ToString());         

        try
        {                              
            mmtimer.Start();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
                MessageBoxIcon.Stop);
        }            
    }

    private void Stop_Click(object sender, EventArgs e)
    {            
        mmtimer.Stop();

        double T = DateTime.Now.Hour * 60 * 60 * 1000 + DateTime.Now.Minute * 60 * 1000 + DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
        Point C = Cursor.Position;

        TD.Items.Add(C.ToString());
        MT.Items.Add(T.ToString());
        M();
    }

    private void mmtimer_Tick(object sender, System.EventArgs e)
    {
        double T = DateTime.Now.Hour * 60 * 60 * 1000 + DateTime.Now.Minute * 60 * 1000 + DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
        Point C = Cursor.Position;

        TD.Items.Add(C.ToString());

        MT.Items.Add(T.ToString());

    }
    void M()
    {
        for (int i = 0; i < MT.Items.Count - 1; i++)
        {
            double A1 = Convert.ToDouble(MT.Items[i + 1]);
            double A2 = Convert.ToDouble(MT.Items[i]);
            double d = A1 - A2; 
            delta_MT.Items.Add(d);
        }
    }
}



你能告诉如何修复前两个中断?如果可能的话。

Could you please tell how to fix the first two interrupts? if it is possible.

推荐答案

在多媒体计时器在运行标准分辨率更改分辨率 1 ,计时器将先完成目前的时期。新的时期,只会成为下一个中断后生效。设置多媒体定时是同步的工作。因此,它可以通过多达20毫秒(在某些系统中断时间)被延迟。
如果你想确保你定时方案从主代码开始工作,你应该对多媒体时间配置2中断面临的主要代码段的呼吁。我用50毫秒倒是你应该确定。

When the multimedia timer is running at standard resolution before you change the resolution to 1, the timer will first complete its current period. The new period will only be effective after the next interrupt. Setting the multimedia timing is a synchronous job. Thus it may be delayed by as much as 20ms (interrupt period on some systems). If you want to make sure that you timing scheme works from the start of your main code you should make the calls to the multimedia time configuration 2 interrupt periods ahead of the main code. I'd with 50 ms you should be OK.

您遵守的第一中断延迟取决于当你的电话被提出的有关系统中断的。

The delay you observe for the first interrupts depends on when your call was made with respect to the systems interrupt.

这篇关于多媒体定时器在C#中断(前两个中断是坏的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆