Reputation: 1653
I have the following piece of code for using text to speech feature in Windows Phone 8. I am using ssml, with bookmarks. But when changing any UI element in the Bookmark event called function, raises Unauthorized Exception.
private void Initialise_synthesizer()
{
this.synthesizer = new SpeechSynthesizer();
synthesizer.BookmarkReached += new TypedEventHandler<SpeechSynthesizer, SpeechBookmarkReachedEventArgs>
(BookmarkReached);
}
void BookmarkReached(object sender, SpeechBookmarkReachedEventArgs e)
{
Debugger.Log(1, "Info", e.Bookmark + " mark reached\n");
switch (e.Bookmark)
{
case "START":
cur = start;
break;
case "LINE_BREAK":
cur++;
break;
}
**error here** t1.Text = cur.ToString();
}
But on running it gives the following error
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
Invalid cross-thread access.
Any idea how to fix this error, or any work around.
Upvotes: 2
Views: 710
Reputation: 1653
Just got the answer.
Since the synthesizer.SpeakSsmlAsync()
is an async function, to perform UI operations Dispatcher has to be used, something like this -
Dispatcher.BeginInvoke(() =>
t1.Text = cur.ToString());
Upvotes: 2
Reputation: 15006
It's pretty much unrelated to the speech recognition. It seems that it's related to accessing elements which are on the UI thread from a different thread.
Try this:
Dispatcher.BeginInvoke(() =>
{
t1.Text = cur.ToString();
}
);
Upvotes: 1