我如何在C#中定时 [英] How Do I Timer In C#
本文介绍了我如何在C#中定时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我再次请有人可以帮助我今天我需要这个muhc,我做了这个我可以用按钮打开和关闭cd / rw但是我想用计时器做这个,计时器运行每5秒开一次关闭请帮忙!
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!
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
推荐答案
以下是c#中定时器教程的链接/>
http://www.dotnetperls.com/timer [ ^ ]
这段代码有效(基于你的原创) - 它没有打开我的CD抽奖
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
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);
}
我改进了你的代码。
我添加了两个计时器和我将它命名为tmrOpen和tmrClose并将两者属性设置为;
启用= false
间隔= 5000
因为1000相当于1秒
我还添加了开始按钮来启动tmrOpen我让tmrOpen启动tmrClose,反之亦然。
希望对您有所帮助。
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.
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;
}
}
}
另请参阅此CodeProject文章: .NET Framework的多媒体计时器 [ ^ ]。
-SA
See also this CodeProject article: The Multimedia Timer for the .NET Framework[^].
—SA
这篇关于我如何在C#中定时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文