locked
Wait till all items load RRS feed

  • Question

  • Hi

    i am adding some videos like 10 to 15 videos in a stackpanel

    i want to trigger a function after all the videos is loaded and added,

    Thank you.

    Friday, March 8, 2013 6:48 AM

Answers

  • Attempt this then.

            void LoadMediaElements(string[] items)
            {
                new Thread(() =>
                {
                    items.ToList().ForEach(p =>
                    {
                        panel.Dispatcher.BeginInvoke(new System.Action(() =>
                            {
                                MediaElement element = new MediaElement();
                                element.Height = 200;
                                element.Width = 200;
                                element.Source = new Uri(p, UriKind.RelativeOrAbsolute);
                                element.MediaOpened += (s, e) =>
                                { panel.Children.Add(element); };
                            }));
                    });
                }).Start();
            }

    • Marked as answer by Lisa Zhu Monday, March 25, 2013 5:56 AM
    Monday, March 11, 2013 9:28 AM
  • Hi,

    then in your loop add a handler to the MediaOpened event for each added "media". In that handler you just have to count down from "list333.Length" and trigger whatever you want to trigger after reaching zero. Like this (bold lines added):

    // this is your code anywhere in class x string[] list333 = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"Video"); this.itemCount = list333.Length; // you have to introduce this foreach (string str3 in list333) { MediaElement media = new MediaElement(); media.Source = new Uri(str3, UriKind.RelativeOrAbsolute); media.MediaOpened += this.OnMediaOpened; menu.Children.Add(media); } // add this 'countdown-handler'
    private void OnMediaOpened(object sender, RoutedEventArgs e) { media.MediaOpened -= this.OnMediaOpened; if (--this.itemCount == 0) { // code your trigger here } }

    Cheers
    Jürgen

    • Proposed as answer by noorbakhsh Friday, March 8, 2013 6:13 PM
    • Marked as answer by Lisa Zhu Monday, March 25, 2013 5:55 AM
    Friday, March 8, 2013 12:56 PM

All replies

  • Hi,

    can you tell us, how you load the videos. Is it by just adding some MediaElements in XAML? Then the MediaOpened event might help you (just count until it's fired n-times where "n" is the number of your videos).

    Cheers
    Jürgen

    Friday, March 8, 2013 9:34 AM
  • Thank you for reply

    string[] list333 = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"Video");
                foreach (string str3 in list333)
                {

    MediaElement media = new MediaElement();

    media .Source = new Uri(str3, UriKind.RelativeOrAbsolute);  --> sourcing from list


    menu.Children.add(media);  ---> menu is the stackpanel

    }


    • Edited by alex_prvn Friday, March 8, 2013 9:56 AM
    Friday, March 8, 2013 9:55 AM
  • Hi,

    then in your loop add a handler to the MediaOpened event for each added "media". In that handler you just have to count down from "list333.Length" and trigger whatever you want to trigger after reaching zero. Like this (bold lines added):

    // this is your code anywhere in class x string[] list333 = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"Video"); this.itemCount = list333.Length; // you have to introduce this foreach (string str3 in list333) { MediaElement media = new MediaElement(); media.Source = new Uri(str3, UriKind.RelativeOrAbsolute); media.MediaOpened += this.OnMediaOpened; menu.Children.Add(media); } // add this 'countdown-handler'
    private void OnMediaOpened(object sender, RoutedEventArgs e) { media.MediaOpened -= this.OnMediaOpened; if (--this.itemCount == 0) { // code your trigger here } }

    Cheers
    Jürgen

    • Proposed as answer by noorbakhsh Friday, March 8, 2013 6:13 PM
    • Marked as answer by Lisa Zhu Monday, March 25, 2013 5:55 AM
    Friday, March 8, 2013 12:56 PM
  • There is one line missing in the EventHandler though

    MediaElement media = sender as MediaElement;

    otherwise it should work as suggested.


    noorbakhsh حميد نوربخش

    Friday, March 8, 2013 6:15 PM
  • Hey Alex, you could do it like this.

    It will run without blocking the UI.

            void LoadMediaElements(string[] items)
            {
                new Thread(() =>
                {
                    items.ToList().ForEach(p =>
                        {
                            MediaElement element = new MediaElement();
                            element.Height = 200;
                            element.Width = 200;
                            element.Source = new Uri(p, UriKind.RelativeOrAbsolute);
                            element.MediaOpened += (s, e) => 
                            {
                                panel.Dispatcher.BeginInvoke(new Action(() => 
                                {
                                    panel.Children.Add(element); 
                                }));
                            };
                        });
                }).Start();
            }


    Developing is part of being a developer.


    Friday, March 8, 2013 8:21 PM
  • Hi thank you for replying

    i tried the code where it show "The calling thread must be STA, because many UI components require this."

    what i have to do.

    Monday, March 11, 2013 7:10 AM
  • Attempt this then.

            void LoadMediaElements(string[] items)
            {
                new Thread(() =>
                {
                    items.ToList().ForEach(p =>
                    {
                        panel.Dispatcher.BeginInvoke(new System.Action(() =>
                            {
                                MediaElement element = new MediaElement();
                                element.Height = 200;
                                element.Width = 200;
                                element.Source = new Uri(p, UriKind.RelativeOrAbsolute);
                                element.MediaOpened += (s, e) =>
                                { panel.Children.Add(element); };
                            }));
                    });
                }).Start();
            }

    I think there is something wrong with my account.

    Developing is part of being a developer.

    Monday, March 11, 2013 9:37 AM