开发者

Large method causing silverlight application to go into "Not responding" state

I am working on an application that plays videos through the silverlight MediaElement object.

I have a large method which is responsible for the following

  1. Opens a FileInfo item on the local file path of the video and strips the file name to get the first portion of the filename which we use as part of the license acquisition process
  2. Sets the LicenseAcquirer on the MediaElement
  3. Sets the Source property of the MediaElement

When this method is called, it actually causes the application to go into a "Not reponding" state for a couple of seconds. How do I avoid this? I have tried putting this all into a background worker but I have to invoke the UI thread for almost all of the calls and this didnt help it seemed to actually make things slower.

I have a busy box that shows while this all happens but that actually stops reporting progress in those seconds where the application is not responding. I understand why开发者_开发知识库 this is happening - a lot of work happening on the main UI thread, but how do I avoid this?

This is the code that is causing the trouble:

    private void SetupMediaElement(String mediaElementType)
    {
        Messenger.Default.Send("Loading video...", "SetNowWatchingVideoBusyBoxText");
        Messenger.Default.Send(true, "SetNowWatchingVideoBusyBox");
        try
        {
            if (_mainMediaElement != null)
            {
                VideoItem vi = CurrentSession.NowPlayingVideoItem;

                if (vi != null)
                {
                    CurrentVideoItem = vi;
                    MustShowImage = true;

                    if (vi.ID != string.Empty)
                    {
                        String mediaId = String.Empty;
                        if (vi.LocalFilePath != DEMOVIDEOPATH)
                        {
                            if (vi.LocalFilePath != String.Empty)
                            {
                                var fi =
                                    new FileInfo(vi.LocalFilePath);
                                if (fi.Exists)
                                {
                                    mediaId = fi.Name.Substring(fi.Name.LastIndexOf('-') + 1,
                                                                (fi.Name.LastIndexOf('.') -
                                                                 (fi.Name.LastIndexOf('-') + 1)));
                                }
                            }
                            else
                            {
                                Debug.WriteLine("localFilePath is empty");
                            }

                            Debug.WriteLine("MediaId = " + mediaId +
                                            ", SessionId = " +
                                            CurrentSession.LoggedOnUser.SessionId +
                                            ",Ticket = " +
                                            CurrentSession.LoggedOnUser.Ticket);

                            string licenseURL = GetLicenseURL(mediaId, CurrentSession.LoggedOnUser.SessionId,
                                                              CurrentSession.LoggedOnUser.Ticket);
                            if (licenseURL != string.Empty)
                            {
                                var la = new LicenseAcquirer
                                             {
                                                 LicenseServerUriOverride
                                                     =
                                                     new Uri(
                                                     licenseURL)
                                             };

                                la.AcquireLicenseCompleted += la_AcquireLicenseCompleted;
                                _mainMediaElement.LicenseAcquirer = la;
                            }

                            var fileInfo = new FileInfo(vi.LocalFilePath);
                            string playURL = @"file://" +
                                             Path.Combine(CoreConfig.HOME_FULL_PATH, fileInfo.Name);
                            playURL = playURL.Replace("\\", @"/");
                            VideoURL = playURL;
                        }
                        else
                        {
                            VideoURL = vi.LocalFilePath;
                            Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
                        }

                        _totalDurationSet = false;
                        TotalTime = FormatTextHoursMinutesSecond(_mainMediaElement.NaturalDuration.TimeSpan);
                        SetSliderPosition();
                    }
                }
                else
                {
                    Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
                }
            }
            else
            {
                Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
            VideoURL = DEMOVIDEOPATH;
            Messenger.Default.Send(false, "SetNowWatchingVideoBusyBox");
        }
    }

Thanks

EDIT: So it turns out that the method posted above is NOT the cause of the delay - that code executes in under a second. The problem comes in when the media element's source is set and it reads the file to the end - large files take time and this is the delay. Am opening a new question based on this.


You should do some diagnostics to determine which line(s) are truely costing all that time, its unlikely that amount of time is spread evenly across the whole function.

Place that line (or lines) in a background thread (hopefully that line doesn't need to be on the UI thread).


So it turns out that the method posted above is NOT the cause of the delay - that code executes in under a second. The problem comes in when the media element's source is set and it reads the file to the end - large files take time and this is the delay. Am opening a new question based on this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜