2016-05-17 7 views
0

Ich brauche eine Javascript-Funktion in einer lokalen Webseite im Webbrowser In WPF-Formular geladen rief eine Thread-SchleifeInvoke JavaScript-Funktion in einer Fadenschlinge

Das ist mein C# Code:

public partial class MainWindow : Window 
    { 


     Thread myThread; 

     public MainWindow() 
     { 
      InitializeComponent(); 
      string WebPage = "file:///C:/Users/BARI/Desktop/JSThread/JavaScriptWithThread/JavaScriptWithThread/LocaWebPage.html"; 
      webBrowser.Navigate(WebPage); 

     } 

     private void ThreadLoop() 
     { 
      int i=0; 
      while (Thread.CurrentThread.IsAlive) 
      { 
       Thread.Sleep(1000); 
       try 
       { 
        Console.WriteLine(i); 
        label.Dispatcher.Invoke(new Action(() => label.Content = i)); 
        this.webBrowser.Invoke(new Action(() => this.webBrowser.InvokeScript("thread", i))); //Doesn't work 


       } 
       catch (InvalidOperationException) 
       { 
        Console.WriteLine("Error!"); 
       } 
       i++; 
      } 
     } 

     private void StatrtThread_Button(object sender, RoutedEventArgs e) 
     { 
      this.webBrowser.InvokeScript("thread", "100"); //This works fine 
      myThread = new Thread(new ThreadStart(ThreadLoop)); 
      myThread.Start(); 

     } 

     private void StopThread_Button(object sender, RoutedEventArgs e) 
     { 
      myThread.Suspend(); 
     } 
    } 

mein Html-Code:

<html> 
    <head> 
    <meta charset="utf-8"> 
    <title></title> 
    </head> 
<body onload=start()> 
<script> 

function start(){ 
var t=0; 
document.getElementById("valeur").innerHTML = t; 
} 
var i = 0; 
function thread(i){ 

document.getElementById("valeur").innerHTML = i; 
} 


</script> 

Valeur: <span id="valeur"></span> 

</body> 
</html> 

ich bin in dem this.webBrowser.Invoke(new Action(() => this.webBrowser.InvokeScript("thread", i))); WebBrowser mit einem Fehler keine Definition für Invokeenthaltenandere Lösungen?

+0

Schauen Sie diese Antwort http://stackoverflow.com/a/22518132/1298308 – Aminul

+0

Ich denke, dass für die asp.net ist –

Antwort

0

Tun Sie dasselbe, was Sie mit dem Label in der Zeile darüber tun, verwenden Sie den Dispatcher.

this.webBrowser.Dispatcher.Invoke(new Action(() => this.webBrowser.InvokeScript("thread", i))); 
+0

Danke Problem gelöst –