开发者

How can I wait for a signal before proceeding compilation of other code segment

Can I wait for a signal from 开发者_C百科an event so that, when I receive the signal then only I will proceed with next code segment?

For making it clear , I have the follwoing code:

hiddenMediaElement.Source = new Uri(strMediaFileName, UriKind.RelativeOrAbsolute);
hiddenMediaElement.MediaFailed += (obj, Sender) =>
{ 
    bMediaError = true; 
};

if (!bMediaError)
{
    ObjChildMediaPlayer.Visibility = Visibility.Visible;
    ObjChildMediaPlayer._currenTitle = strTitle;
    ObjChildMediaPlayer.Show();
    Content_FullScreenChanged(null, null);
}

The problem here is the if condition is executed before the MediaFailed event. But I want to wait for MediaFailed event to be executed 1st and then the if condition and I do not want to use events here.

How could I wait for the same? Can I use a mutex or something similar?


You can use AutoResetEvent to handle this situation. But I definitly would try to find another way if there is one.

var autoResetEvent = new AutoResetEvent(false);

hiddenMediaElement.Source = new Uri(strMediaFileName, UriKind.RelativeOrAbsolute); hiddenMediaElement.MediaFailed += (obj, Sender) => { bMediaError = true; autoResetEvent.Set(); }; hiddenMediaElement.MediaOpened += (obj, Sender) => {
// I think this occurs when it is successfull. Else put it in the handler that handles a success autoResetEvent.Set(); };

        autoResetEvent.WaitOne(); // set a timeout value
                    if (!bMediaError)
                    {
                        ObjChildMediaPlayer.Visibility = Visibility.Visible;
                        ObjChildMediaPlayer._currenTitle = strTitle;
                        ObjChildMediaPlayer.Show();
                        Content_FullScreenChanged(null, null);
                    }

Or ... I'm not sure this will work, but try it out.

hiddenMediaElement.Source = new Uri(strMediaFileName, UriKind.RelativeOrAbsolute);
        hiddenMediaElement.MediaOpened += (obj, sender) =>
                        {  
            ObjChildMediaPlayer.Visibility = Visibility.Visible;
                            ObjChildMediaPlayer._currenTitle = strTitle;
                            ObjChildMediaPlayer.Show();
                            Content_FullScreenChanged(null, null);
                        };


Put your code in the event handler:

hiddenMediaElement.Source = new Uri(strMediaFileName, UriKind.RelativeOrAbsolute); 
hiddenMediaElement.MediaFailed += (obj, Sender) => 
{  
    ObjChildMediaPlayer.Visibility = Visibility.Visible; 
    ObjChildMediaPlayer._currenTitle = strTitle; 
    ObjChildMediaPlayer.Show(); 
    Content_FullScreenChanged(null, null); 
}; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜