- Open a windows forms application c# project
- In design page take two timers set the time intervals to 10 sec
- In cs page cut (ctrl+x) and paste (ctrl+v) the below code.
- Write the two events for timer1 and timer2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
[DllImport("winmm.dll")]
//The
mciSendString function sends a command string to an MCI device. The device that
the command is sent to is specified in the command string.
static extern Int32
mciSendString(String command, StringBuilder buffer, Int32
bufferSize, IntPtr hwndCallback);
Boolean
cdtrayopen = true;
Boolean
cdtrayclosed = false;
private
void Form1_Load(object
sender, EventArgs e)
{
{
if
(DateTime.Now.Hour >= 1 && DateTime.Now.Hour <= 23)
{
timer1.Enabled = true;
}
else
{
MessageBox.Show("better u open at a correct time");
}
}
}
private
void timer1_Tick(object
sender, EventArgs e)
{
if
(cdtrayopen == true)
{
//Inptr.Zero=A
read-only field that represents a pointer or handle that has been initialized
to zero.
//function
to open the CD tray door
mciSendString("set CDAudio door open", null, 0, IntPtr.Zero);
cdtrayopen = true;
}
else
{
//function
to close the CD tray door
mciSendString("set CDAudio door closed", null, 0, IntPtr.Zero);
cdtrayopen = false;
}
timer1.Enabled = false;
timer2.Enabled = true;
}
private
void timer2_Tick(object
sender, EventArgs e)
{
if
(cdtrayclosed == false)
{
mciSendString("set CDAudio door closed", null, 0, IntPtr.Zero);
cdtrayclosed = false;
}
else
{
mciSendString("set CDAudio door open", null, 0, IntPtr.Zero);
cdtrayclosed = true;
}
timer2.Enabled = false;
timer1.Enabled = true;
}
}
}
NOTE: This works only for desktops....it doesn't work for laptops.happy coding....