WPF Secondary UI Thread MediaElement 不会在 FIRST 运行 上加载

WPF Secondary UI Thread MediaElement wont load on FIRST run

我有一个程序可以测试 windows 图像中的各种设置。每个测试都标记为 'plugin'。现在我所有的插件都在 运行 工作线程中使用 ThreadPool.QueueUserWorkItem.

当我们想要使用用户输入进行测试时出现问题,其中 he/she 必须测试设备的 Sound/Video 和触摸屏。为了对此进行测试,我在我的一个插件 classes 中创建了一个辅助 WPF window,因此该插件会打开 window,等待用户输入,然后关闭。 window 由一个标签、3 个单选按钮、一个 MediaElement 和几个按钮组成。

当我打开我的应用程序时,它加载了我所有的 .dll 文件,然后我可以选择按下我的 "Run Tests" 按钮,该按钮 运行 通过我的插件列表和运行他们。

现在因为我们需要这个用户交互线程,所以我需要它不同于 ThreadPool 线程。下面的代码显示了我如何启动所有线程,第一部分是用户交互插件。代码很乱,因为我现在测试了几个解决方案,一些我试图合并到其他的..

if (availablePlugin.RequirementNumber.Equals("13996"))
          {
            try
            {
              plugin.Status = VerdictEnum.InProgress;
//              var uiThread = new Thread(plugin.TestMethod);

              Action startUIThread = () =>
              {
                Thread uiThread = new Thread(new ThreadStart(() =>
                {
                  SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));
                  plugin.TestMethod();
                }));

                uiThread.SetApartmentState(ApartmentState.STA);
                uiThread.IsBackground = true;
                uiThread.Start();
                uiThread.Join();
              };
              Dispatcher.CurrentDispatcher.Invoke(startUIThread);
            }
            catch (Exception ex)
            {
              uiContext.Post(
                (o) => plugin.ErrorDescription += "Test case threw an unexpected exception: " + ex.Message, null);
              uiContext.Post((o) => plugin.Status = VerdictEnum.Inconclusive, null);
            }
            finally
            {
              //Once the thread has finished it sets its personal ManualResetEvent to true, indicating it has finished its job
              numberOfRunningThreads--;
              eventlist[localCounter].Set();
              Debug.Print("TEST ENDED");
            }
          }
          //All other tests are created as worker threads using the ThreadPool.
          else
          {
            ThreadPool.QueueUserWorkItem(
              new WaitCallback(s =>
              {
                try
                {
                  plugin.Status = VerdictEnum.InProgress;
                  plugin.TestMethod();
                }
                catch (Exception ex)
                {
                  uiContext.Post(
                    (o) => plugin.ErrorDescription += "Test case threw an unexpected exception: " + ex.Message, null);
                  uiContext.Post((o) => plugin.Status = VerdictEnum.Inconclusive, null);
                }
                finally
                {
                  //Once the thread has finished it sets its personal ManualResetEvent to true, indicating it has finished its job
                  numberOfRunningThreads--;
                  eventlist[localCounter].Set();
                  Debug.Print("TEST ENDED");

                }
              }));
          }

看一下我如何在插件中展示我的 window class:

        var videoAudioTest = new VideoAudioPopup();
        videoAudioTest.Show();
        videoAudioTest.ResultReady += videoAudioTest_ResultReady;

        //        videoAudioTest.Closed += (s, e) => System.Windows.Threading.Dispatcher.ExitAllFrames();
        //        videoAudioTest.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
        videoAudioTest.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);


        Dispatcher.Run();

有趣的部分来了。当我 运行 在我的计算机上进行此测试时,一切正常,没有任何问题。我从来没有加载我的 MediaElement 失败。

当我 运行 我的应用程序在需要测试的设备上时,它(几乎,它有几次)永远不会在 FIRST 运行 (我第一次按下'Run tests' 按钮)。如果我保持应用程序打开,并再次按下 'Run Tests' 按钮,它总是有效。

它为 MediaElement 加载的视频是 W7 标准 afaik 'Wildlife.wmv'。媒体播放器本身在外部 运行 时不会出现任何问题。

我假设发生这种情况是因为我的线程出了问题? - 如前所述,代码现在有点混乱,我已经尝试了一些在我的计算机上有效的其他方法,但它从来没有像设备上预期的那样工作,它们几乎总是无法在新的 [=48] 中加载 MediaElement =].

'numberofrunningthreads' 仅用于 debug.print 目的。

非常感谢任何帮助,如果您需要更多代码片段,或者如果我有任何不清楚的地方,请询问,我会尽快回复。

终于知道哪里出了问题。我一直过分关注这样一个事实,即我认为我在控制线程的方式上存在错误。这似乎是测试需要 运行 的设备上的 CPU 问题。似乎有时如果 MediaElement 没有足够的 CPU 可用,就不会创建它,所以我使用的测试视频质量太高,所以它只在某些时候有效,而且出于某种原因CPU 第二次测试 运行s.

使用率总是低一点

我会留下问题,以防其他人碰巧遇到 MediaElement 未加载的问题。我使用这个程序来强调我的 CPU,这样我就可以可靠地使我的程序 'crash' 成为 MediaElement,以验证它是否在 CPU 负载过高时不会创建元素。 (取决于视频的质量)

http://alax.info/blog/1342/comment-page-1

这有助于我得出结论,当用户使用多个 MediaElements 和 运行 遇到与我相同的问题时,上述软件也被另一个用户链接 'Roman R':Black video using multiple instances VMR 9