代码之家  ›  专栏  ›  技术社区  ›  Madeleine

导致silverlight应用程序进入“无响应”状态的大型方法

  •  1
  • Madeleine  · 技术社区  · 14 年前

    我正在开发一个通过silverlightmediaellement对象播放视频的应用程序。

    我有一个很大的方法负责

    1. 打开视频本地文件路径上的FileInfo项,并删除文件名以获取文件名的第一部分,我们将其用作许可证获取过程的一部分
    2. 设置MediaElement的源属性

    调用此方法时,它实际上会导致应用程序在几秒钟内进入“不重新发送”状态。我该如何避免?我试着把这些都放到后台工作程序中,但我必须为几乎所有的调用调用UI线程,这似乎并没有帮助它实际使事情变慢。

    我有一个busy框,显示所有这些发生的时间,但实际上在应用程序没有响应的那几秒钟内停止报告进度。我知道为什么会发生这种情况-在主UI线程上会发生很多工作,但是我如何避免这种情况?

    这是导致故障的代码:

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

    编辑:

    2 回复  |  直到 14 年前
        1
  •  2
  •   AnthonyWJones    14 年前

    您应该做一些诊断来确定哪些行一直在真正地花费时间,不太可能在整个函数中平均分配时间。

    将该行(或多行)放在后台线程中(希望该行不必在UI线程上)。

        2
  •  1
  •   Madeleine    14 年前