views:

45

answers:

3

Can I wait for a signal from a event so that , when I recievce 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 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 mutex or something similar.

A: 

u can undo ur action in media failed event. (until when u want to listen to change event may be never happens).

SaeedAlg
Is not making any sense
Subhen
A: 

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); 
}; 
Michael S. Scherotter
I want a signal from event to proceed with the code.
Subhen
A: 

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);
                        };
RonaldV