2015-02-23 43 views
1

我試圖在我的應用程序中實現最小示例。在這個應用程序,我稱之爲營銷窗口(即ChromiumWebBrowser)以下方式: 我App.xaml.cs看起來是這樣的:CefSharp的最小示例和多線程

namespace ASA_Videowand 
{ 
    public partial class App 
    { 
     private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      // ... 
       foreach (List<string> myStrings in Screen.AllScreens.Select(myScreen => _xml.GetScreenConfigs(i))) 
       { 
        // ... 
          int i1 = i; 
          List<string> strings = myStrings; 
          var newWindowThread = new Thread(() => 
          { 
           var myMarketing = new Marketing(i1, strings[0]); 
           myMarketing.Show(); 
           Dispatcher.Run(); 
          }); 
          newWindowThread.SetApartmentState(ApartmentState.STA); 
          newWindowThread.IsBackground = true; 
          newWindowThread.Start(); 
         } 
         else 
         { 
          // ... 
         } 
        } 
        i++; 
       } 
      } 
     } 
    } 
} 

這是我Marketing.xaml和Marketing.xaml.cs:

<Window x:Class="ASA_Videowand.Marketing" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:main="clr-namespace:ASA_Videowand.Views" 
     Title="{Binding WebBrowser.Title}"> 
    <Grid> 
     <main:MainView /> 
    </Grid> 
</Window> 

namespace ASA_Videowand 
{ 
    public partial class Marketing 
    { 
     public Marketing(int screenNumber, string myLink) 
     { 
      InitializeComponent(); 
     } 
    } 
} 

這裏說到我MainView.xaml:

<UserControl x:Class="ASA_Videowand.Views.MainView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" 
      mc:Ignorable="d" 
      d:DesignWidth="640" 
      d:DesignHeight="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <cefSharp:ChromiumWebBrowser Grid.Row="0" 
            Address="http://www.google.com" 
            WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}" 
            Title="{Binding Title, Mode=TwoWay}" /> 
     <StatusBar Grid.Row="1"> 
      <ProgressBar HorizontalAlignment="Right" 
         IsIndeterminate="{Binding WebBrowser.IsLoading}" 
         Width="100" 
         Height="16" 
         Margin="3" /> 
      <Separator /> 
      <!-- TODO: Could show hover link URL here --> 
      <TextBlock /> 
     </StatusBar> 
    </Grid> 
</UserControl> 

和我MainView.xaml.cs:

namespace ASA_Videowand.Views 
{ 
    public partial class MainView : UserControl 
    { 
     public MainView() 
     { 
      InitializeComponent(); 
      DataContext = new MainViewModel(); 
     } 
    } 
} 

而且如此下去之前,它與exeption的MainView.xaml停止,即

<cefSharp:ChromiumWebBrowser Grid.Row="0" 
            Address="http://www.google.com" 
            WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}" 
            Title="{Binding Title, Mode=TwoWay}" /> 

將屬於另一個線程因此不能修改。 我是新的多線程。如果我用內置的IE-WebControl嘗試它,它可以很好地工作。我知道如何從代碼隱藏中調用某些東西,但在View中獲取這個異常對我來說是新的。 我該如何做這項工作?我很欣賞任何想法。謝謝。

+0

莫非你的地方確切的錯誤呢? – Bono 2015-02-23 12:53:41

+0

*我該怎麼做這項工作?* ...簡單...不要在後臺線程上做UI工作。 – Sheridan 2015-02-23 13:30:13

+0

在德語中它的意思是:{「Der aufrufende線程可以用來處理對象,而不是用於對象的線程。」}英文翻譯應該是這樣的:「調用線程不能訪問這個對象,因爲對象由另一個線程擁有。「 – 2015-02-23 13:30:29

回答

1

把很簡單:

你不能這樣做在後臺線程UI工作

只需使用UI線程做你的UI工作,而不是:

foreach (List<string> myStrings in Screen.AllScreens.Select(myScreen => _xml.GetScreenConfigs(i))) 
{ 
    // ... 
      int i1 = i; 
      List<string> strings = myStrings; 
      var myMarketing = new Marketing(i1, strings[0]); 
      myMarketing.Show(); 
     } 
     else 
     { 
      // ... 
     } 
    } 
    i++; 
} 
+0

當我調用這個窗口12次(是的,它運行在一個有16個屏幕的服務器上),並通過XAML啓動一個ChromiumWebBrowser,全部12個運行在一個線程中,服務器跪倒在地。我需要在不同的線程中運行WebBrowsers。所以你說,我不能使用XAML來初始化它們?這將很難轉移到我的應用程序。有趣的是,當我使用這種任務,不使用視圖,而是一個簡單的WebBrowser(基於IE的),一切正常。 – 2015-02-23 13:42:14

+0

我沒有說你不能使用XAML,我說你不能在後臺線程上運行UI代碼。如果您的控件沒有與UI相關的代碼,那麼您可以*在後臺線程上運行它,但是與運行一些您不想在UI中看到的UI控件相比,這會有更好的解決方案。 – Sheridan 2015-02-23 13:48:38