简体   繁体   English

我如何获得这些if语句以在C#中很好地播放

[英]How do I get these if statements to play nicely in c#

I've modified my code to the point where it will execute, but it's linked to a timer, and will attempt to execute over and over till the timed condition is lost. 我已经修改了我的代码,使其可以执行,但是它已链接到计时器,并且将尝试一遍又一遍地执行,直到失去定时条件为止。 I tried putting a "lock" on the system with a variable I named "intLimiter", to force the sequence to stop after one execution, but the lock doesn't seem to be working. 我尝试用一​​个名为“ intLimiter”的变量在系统上放置一个“锁”,以强制序列在执行一次后停止,但是该锁似乎不起作用。

if (DateTime.Now.Hour == intAlarmHour && DateTime.Now.Minute == intAlarmMinute)
{
    if (intLimiter == 1)                    
    {
        intLimiter = 2;
        if (radTG.Checked)
        {
            System.Media.SoundPlayer sound = new System.Media.SoundPlayer(Properties.Resources.Celldweller___Tough_Guy);
            sound.Play();                       
        }
               ...
    }
}

As of what I understand from your question, you want to separate the if statements to work all at the same time... What you can do is create new 'Threads' for each statement which would make you able to run all of then at once... 据我对您的问题的了解,您希望将if语句同时运行起来...您可以做的是为每个语句创建新的“线程”,这将使您能够在一旦...

For thread, make sure you include the namespace System.Threading. 对于线程,请确保包括名称空间System.Threading。

using System.Threading;

Now what you should do with your code: 现在,您应该对代码执行以下操作:

...

if (DateTime.Now.Hour == intAlarmHour && DateTime.Now.Minute == intAlarmMinute)
{


    if (radTG.Checked == true)
    {
        Thread radTG = new Thread(radTGChecked);
        radTG.Start();
    }
    ...
}

...

public void radTGChecked() {
    SoundPlayer sound = new SoundPlayer(Properties.Resources.Celldweller);
    sound.PlaySync();
}

Do this to all the if statements following, and they will all run together, each on a thread... Forgive me if that was not the answer of your question, but the title corresponding to the content does seem a little blurry to me... But if it did, here 's an article that will help you understand Multi-Threading. 对下面的所有if语句执行此操作,它们将一起在线程上一起运行...如果这不是您问题的答案,请原谅我,但是与内容相对应的标题对我来说似乎有点模糊。 ..但是,如果这样做, 是一篇文章,可以帮助您了解多线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM