Click here to Skip to main content
16,018,057 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everyone i am again please someone can help me today i need this so muhc , i made this i can open and close cd/rw with button but ı want to make this with timer, the timer run every 5 second opening and closing please help!

C#
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.Management;
using System.Runtime.InteropServices;
namespace cd_surucu_acma  
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
public static extern int cdKapakAc(string icerik, StringBuilder durum, int deger, IntPtr neKadar);
private void btnAc_Click(object sender, EventArgs e)
{
cdKapakAc("Set cdaudio door open wait ", null, 0, IntPtr.Zero);
}
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
public static extern int cdKapakKapat(string icerik, StringBuilder durum, int deger, IntPtr neKadar); //CD kapat

private void btnKapat_Click(object sender, EventArgs e)

{

cdKapakKapat("Set cdaudio door Closed wait ", null, 0, IntPtr.Zero);

}

}

}//thank you so much codeprojct family
Posted

Here is a link to a tutorial on timers in c#
http://www.dotnetperls.com/timer[^]

This code sort of works (based on your original) - it doesn't open my CD draw though
C#
public Form1()
{
    InitializeComponent();
    System.Timers.Timer t = new System.Timers.Timer();
    t.Interval = 5000;
    t.Elapsed += new ElapsedEventHandler(telapsed);
    t.Enabled = true;

}
public static void telapsed(object sender, ElapsedEventArgs e)
{
    cdKapakKapat("Set cdaudio door Closed wait ", null, 0, IntPtr.Zero);
}
 
Share this answer
 
Comments
Ahmet Yön 25-Feb-14 15:22pm    
thank you so much :)
Ahmet Yön 25-Feb-14 15:37pm    
what is the telapsed?
CHill60 25-Feb-14 15:43pm    
That's just a name I made up for the function that handles the Elapsed event on the timer - see t.Elapsed += new ElapsedEventHandler(telapsed); ... that's telling C# that when the 5 seconds has elapsed it should "run" the function called "telapsed". I could have given it a more meaningful name but I was being a bit lazy :-)
Ahmet Yön 25-Feb-14 15:48pm    
okay the problem solve thans alot
I refined your code.

I added two timer and I named it to tmrOpen and tmrClose and set the both property into;

Enabled = false
Interval = 5000

because 1000 is equivalent to 1 second
I also add Start button to start the tmrOpen and I let tmrOpen to start the tmrClose and vice versa.

Hope it helps to you.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Runtime.InteropServices;

namespace OpenCloseCD_Device
{
    public partial class frmCDDrives : Form
    {
        public frmCDDrives()
        {
            InitializeComponent();
        }

        //********* Declare Variables etc. *****************
        [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
        public static extern int cdKapakAc(string icerik, StringBuilder durum, int deger, IntPtr neKadar);

        [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
        public static extern int cdKapakKapat(string icerik, StringBuilder durum, int deger, IntPtr neKadar);
        //**************************>>>>>>>>>>>>>>>>>>>>>>>>>


        private void btnStart_Click(object sender, EventArgs e)
        {
            tmrOpen.Enabled = true;
        }



        private void tmrOpenClose_Tick(object sender, EventArgs e)
        {
            cdKapakAc("Set cdaudio door open wait ", null, 0, IntPtr.Zero);
            tmrClose.Enabled = true;
            tmrOpen.Enabled = false;

        }

        private void tmrClose_Tick(object sender, EventArgs e)
        {
            cdKapakKapat("Set cdaudio door Closed wait ", null, 0, IntPtr.Zero);
            tmrOpen.Enabled = true;
            tmrClose.Enabled = false;
        }
    }
}
 
Share this answer
 
v3
See also this CodeProject article: The Multimedia Timer for the .NET Framework[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900