[英]Firing a sound with a timer in a backgroundworker, wp7 phone app
I am hitting a wall and have come here hoping you guys can help me out with the following: 我撞墙了,到这里来是希望你们能为我提供以下帮助:
I am designing a wp7 app where I am having sounds fire off periodically (every 700 ms). 我正在设计一个wp7应用程序,在该应用程序中,我会定期(每700毫秒)触发一次声音。 I want these sounds to play on another thread than the UI thread.
我希望这些声音在UI线程之外的其他线程上播放。 Thus, I cannot use a
dispatch timer
because the UI thread will be heavily used in the meantime while these sounds fire. 因此,我不能使用
dispatch timer
因为在发出这些声音的同时,UI线程将大量使用。
In looking at this problem, I made a backgroundworker
with a Thread.Sleep(700)
command. 在研究此问题时,我使用
Thread.Sleep(700)
命令创建了一个backgroundworker
。 This works, but as one may expect it can take longer than 700 ms to fire off a sound. 这行得通,但是正如人们可能期望的那样,发出声音可能要花费超过700毫秒的时间。 So sometimes I hear delays.
所以有时候我听到延迟。
So I turn to you guys -- How can I get as close to a firing of a sound every 700 ms in a backgroundworker
or another thread? 所以,我转向你们-我怎样才能在
backgroundworker
或其他线程中每700毫秒发出一次声音? Is a Timer
a wise idea? Timer
是一个明智的主意吗?
Here is some code to better illustrate: 这是一些代码,可以更好地说明:
private void RunBackgroundWorker()
{
backgroundWorker = new BackgroundWorker ();
backgroundWorker.WorkerSupportsCancellation = true;
backgroundWorker.DoWork += new DoWorkEventHandler(StartSounds);
backgroundWorker.RunWorkerAsync();
}
public void StartSounds(object sender, EventArgs e)
{
for (currentsoundchoice = 0; currentsoundchoice <= max; currentsoundchoice ++)
{
if (backgroundWorker.CancellationPending == true)
{
backgroundWorker.CancelAsync();
currentsoundchoice = max + 1; //just to ensure it stops.
}
else
{
Time_Tick();
if (max == 12)
currentsoundchoice = -1; //repeats the loop when it reaches max.
}
}
}
void Time_Tick()
{
Thread.Sleep(700);
FrameworkDispatcher.Update();
switch (sounds)
case 0: //code
break;
// ETC, So forth.......
}
I heard that using a Timer is also a good way to go about this, but is it possible to run a timer in a backgroundworker, and if so, how would it be structured? 我听说使用计时器也是解决此问题的好方法,但是可以在后台工作程序中运行计时器吗?如果是这样,它的结构如何? More importantly, would it allow me to get as close to the 700 ms firing time I desire without any audible delays?
更重要的是,它可以让我尽可能接近我希望的700 ms触发时间,而没有任何可听见的延迟吗?
Thank you. 谢谢。
BackgroundWorker
is the wrong choice here. BackgroundWorker
是错误的选择。 It is designed to allow you to perform expensive work on a background thread and then synchronize the results back to the UI. 它旨在允许您在后台线程上执行昂贵的工作,然后将结果同步回UI。 You do not need any synchronization.
您不需要任何同步。 Moreover, you need the work to occur at a regular interval - not as a one-off task.
此外,您需要定期进行工作-而不是一次性任务。
A Timer
would be much more suited. Timer
会更适合。 It would allow you to run your logic every 700ms and to remove the nasty Thread.Sleep()
call you have in your current code. 这将使您每700ms运行一次逻辑,并删除当前代码中讨厌的
Thread.Sleep()
调用。 The Timer
will use a background thread just like BackgroundWorker
does. Timer
将像BackgroundWorker
一样使用后台线程。
Note, however, that it can only guarantee that it won't run before 700ms has elapsed. 但是请注意,它只能保证它不会在 700毫秒过去之前运行。 If you're absolutely hammering the device, for example, it may struggle to run your code on a regular interval and you may notice lag.
例如,如果您绝对要锤打设备,则可能难以按固定的时间间隔运行代码,并且可能会发现延迟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.