1

I am using web browser control and trying to call function in Javascript using document.InvokeScript.

It works everywhere else in my code except at the part where i call event handler for Process.Exited.

 //this function is being called from Javascript code.
public void Open()
{

    //****FROM HERE TO......****

    Object[] param = new Object[2];
    param[0] = "play";
    param[1] = "playDisabled";
    webBrowser1.Document.InvokeScript("disable", param);

    param[0] = "playDisabled";
    param[1] = "Playing";
    webBrowser1.Document.InvokeScript("setText", param);

    //****THERE, INVOKESCRIPT WORKS....****

    //there i open exe file...
    Process GameLaunch = new Process();
    GameLaunch.StartInfo.FileName = ExtractPath + gameFile;
    GameLaunch.StartInfo.Arguments = ExtractPath + gameFile;
    GameLaunch.EnableRaisingEvents = true;

    //after its closed i call GameLaunch_Exited method.
    GameLaunch.Exited += new EventHandler(GameLaunch_Exited);
    GameLaunch.Start();

}
void GameLaunch_Exited(object sender, EventArgs e)
{

   InvokeScript("btnReset", "#playDisabled");
}
private object InvokeScript(string name, params object[] args)
{
    //here it fires an error.
    return webBrowser1.Document.InvokeScript(name, args);
}
        private void button1_Click_1(object sender, EventArgs e)
        {
        InvokeScript("btnReset", "#playDisabled");
        string output = "";
        int i = 0;
        foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("a"))
        {
            i++;
            output += i+". Element: id: "+ element.Id + ", inner html: "+ element.InnerHtml + Environment.NewLine;
        }
       MessageBox.Show(output);
    }

all i get from this code is: Specified cast is not valid.

Here are some screens:

Btw method InvokeScript works if i call it from button click handler. And doesn't if called from Exited Handler.

I'm really confused.

enter image description here

And the error itself:

enter image description here

For those who want to see my JS.

function btnReset(id){
  $(document).ready(function(){
     $(id).removeClass("disabled");
     $(id).html("Start");
     $(id).attr("id", "play");
 });
 }

EDIT: Exception Message:

An unhandled exception of type 'System.InvalidCastException' occurred in System.Windows.Forms.dll {"Specified cast is not valid."}

Additional information: Specified cast is not valid.

Thanks for Any help.

-Cheers

-Nick.

Nick
  • 455
  • 9
  • 28
  • Screenshots are frowned upon, please paste in the code as text and the exception message/stack trace as text. As for your problem, I searched around and it appears your exited event is firing from a different thread for whatever reasons (would need to see more code I think). See [here](http://stackoverflow.com/questions/9048922/c-sharp-invalidcastexception-when-trying-to-access-webbrowser-control-from-tim) – Quantic Dec 12 '16 at 16:33
  • @Quantic Hi! Thanks for your respond, Which part of code would you like to see? – Nick Dec 12 '16 at 16:44
  • I edited my question. – Nick Dec 12 '16 at 16:52
  • It's just a generic comment, the only tag I have experience with is `c#` and seeing more code won't help *me* per se. Just saying you will have better luck with responses if you remove the screenshots and replace them with text, and it's frowned upon because linked images can go stale and this site wants questions and answers to be usable years later. – Quantic Dec 12 '16 at 17:09
  • oh, ok your right. – Nick Dec 12 '16 at 17:26
  • @Quantic you can move your comment to answer, i will accept it, yes i was just calling it from different thread, **this.Invoke(new Action(() => { webBrowser1.Document.InvokeScript(name,args); }));** worked. Thanks. – Nick Dec 13 '16 at 17:15
  • In that case I think this is a duplicate of the other question (actually of a question linked in the question I linked), I'll just flag it as such. – Quantic Dec 14 '16 at 22:05
  • yeah, I absolutely agree. – Nick Dec 17 '16 at 14:20

0 Answers0