2

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.

Westy92
  • 19,087
  • 4
  • 72
  • 54

1 Answers1

0

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.

Arno
  • 4,994
  • 3
  • 39
  • 63