我有一个应用多线程的类。我希望一次只允许一个线程"startSpeaking()"。这是我的尝试:
class VoiceEffect
{
SpeechSynthesizer reader = new SpeechSynthesizer();
static readonly object _locker = new object();
public void createVoiceThread(string str)
{
Thread voicethread = new Thread(() => startSpeaking(str)); // Lambda Process
voicethread.IsBackground = true;
voicethread.Start();
}
public void startSpeaking(string str)
{
lock (_locker)
{
reader.Rate = -2; // Voice effects.
reader.Volume = 100;
reader.Speak(str);
}
}
}
我还在另一个类中调用createVoiceThread()
方法。它是由另一个类中的类似约定调用的。例如
class Program
{
static void Main(string[] args)
{
VoiceEffect ve = new VoiceEffect();
string text = "Hello world, how are you today? I am super-duper!!";
for( int i=0 ; i < 10 ; i++ )
{
ve.createVoiceThread(text);
ve.startSpeaking(text);
Thread.Sleep(1000);
}
}
}
我的问题是,我如何修改这个程序,以便当任何线程调用startSpeaking()
时,它一次只播放一个语音模式。
我知道这个问题很老,但如果我正确理解你的问题(你希望所有的演讲都按顺序进行,就像在一个线程上进行一样),你可以做这样的事情:
static class VoiceEffect
{
SpeechSynthesizer reader = new SpeechSynthesizer();
private volatile bool _isCurrentlySpeaking = false;
/// <summary>Event handler. Fired when the SpeechSynthesizer object starts speaking asynchronously.</summary>
private void StartedSpeaking(object sender, SpeakStartedEventArgs e)
{ _isCurrentlySpeaking = true; }
/// <summary>Event handler. Fired when the SpeechSynthesizer object finishes speaking asynchronously.</summary>
private void FinishedSpeaking(object sender, SpeakCompletedEventArgs e)
{ _isCurrentlySpeaking = false; }
private VoiceEffect _instance;
/// <summary>Gets the singleton instance of the VoiceEffect class.</summary>
/// <returns>A unique shared instance of the VoiceEffect class.</returns>
public VoiceEffect GetInstance()
{
if(_instance == null)
{ _instance = new VoiceEffect(); }
return _instance;
}
/// <summary>
/// Constructor. Initializes the class assigning event handlers for the
/// SpeechSynthesizer object.
/// </summary>
private VoiceEffect()
{
reader.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(StartedSpeaking);
reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(FinishedSpeaking);
}
/// <summary>Speaks stuff.</summary>
/// <param name="str">The stuff to speak.</param>
public void startSpeaking(string str)
{
reader.Rate = -2; // Voice effects.
reader.Volume = 100;
// if the reader's currently speaking anything,
// don't let any incoming prompts overlap
while(_isCurrentlySpeaking)
{ continue; }
reader.SpeakAsync(str);
}
/// <summary>Creates a new thread to speak stuff into.</summary>
/// <param name="str">The stuff to read.</param>
public void createVoiceThread(string str)
{
Thread voicethread = new Thread(() => startSpeaking(str)); // Lambda Process
voicethread.IsBackground = true;
voicethread.Start();
}
}
这为您提供了一个将管理所有线程的singleton类,并且所有线程都将共享_isCurrentlySpeaking
变量,这意味着语音提示永远不会相互重叠,因为它们都必须等到变量被清除后才能说话。我不能保证的是,如果您向队列提交了多个提示,而已经有一个提示被大声说出,那么提示的读取顺序(即,控制消息处理队列)。不管怎样,这应该很有效。
您的问题还不清楚,但您有一个静态锁变量(_locker
),这意味着一次只有一个线程可以执行startSpeaking
。目前尚不清楚您是否试图让线程相互等待,或者您的问题是否是因为不希望它们相互等待。
无论哪种方式,像这样使用单个静态锁都是非常可疑的,IMO。如果你真的只能有效地拥有这个类的一个有用实例,那么可以考虑将其作为singleton。(通常在设计方面不太好。)如果有多个独立的实例是可以的,那么通过将_locker
变量设置为实例变量来使独立。
(我也强烈建议您开始遵循.NET命名约定。)