Page 1 of 6 1234 ... LastLast
Results 1 to 10 of 54
  1. #1

    Default [SOLVED] Can .NET WebBrowser.Document.InvokeScript be executed in another thread?


    I have a windows forms based .net application. I also have a webbrowser component on that form.

    What I want is to have browser.Document.InvokeScript(...) execute in another thread and not on the main thread so my main form will not appear to freeze while the script runs.

    Can this be done? How?

    So far I have tried these:

    1. Use delegate with ThreadStart and do this.invoke(method_calling_invoke_script) = works but executes in main thread thus freezing my main form

    2. Use BackgroundWorker and call browser.Document.InvokeScript(...) in the worker.DoWork handler = invokescript does not work but does not freeze my main form

    Any more ideas?

  2. #2
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Can you give some few codes to work around with? are you trying to access some webbrowsers controls here? or downloading some resources?

    Considering that you mentioned about webbrowser components in your first approach it is possible that it may not run, or FREEZES, especially if you call webbrowser's control from other threads without MARSHALING the data.

    In your second approach you used BackGroundWorker, I guess you wanted to make it sure that it works on as background process, but unluckily it didn't worked. you said that it does not freeze but you feel it running, well, perhaps the reason why it was not running at all... if it did, there was a COM errors behind.

    If you can show some code to work out, this will interest me to help you more… I can't guess whether you are properly putting some try catch finally blocks, locking and waiting the threads.

  3. #3
    maybe browser.InvokedRequired is needed.
    ill try to simulate this one

  4. #4
    in the do work of backgroundworker, do something like this

    if (webBrowser1.InvokeRequired)
    {
    CallScript cs = new CallScript(scriptCaller);
    this.Invoke(cs);
    }
    else
    {
    object test = webBrowser1.Document.InvokeScript("<scriptname>");
    }

    pag create lang og delegate para mo tawag sa InvokeScript if true ang InvokeRequired
    I hope this helps

  5. #5
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    We need to know exactly how he did the threading thing in windows forms wether he is call from inside or outside the UI thread.

    I saw two things to remember here:

    1. Do Not invoke any method or property to any control created on another thread. EXCEPT Invoke, BeginInvoke, InvokeRequired and EndInvoke.

    2. Avoid calling long-running process in UI Thread., instead create a new thread but make it sure it does not call any updates to UI, and if it is running on UI Thread, you won't receive any EVENTS, it means it won't be repainted at all, more likely it is FREEZING.


    Invoke vs BeginInvoke

    if you need a synchornous way in invoking method under UI thread use "Invoke"
    if you need asynchronous way, use "BeginInvoke"

    WARNING BE CAREFUL!!!!

    if you use Invoke, the current thread will be block, not until the delegate is already executed. if you use BeginInvoke, it will just call immediately.

    just in case you need to get the value in your delegate via asynchronous(BeginInvoke) you can code like this:

    IAsyncResult asyncResult = method1.BeginInvoke(null, null);
    ..
    ..
    ..
    if (asyncResult.IsCompleted)
    ..
    ..
    ..
    Val = method1.EndInvoke(asyncResult);



    delegate is helpful in passing parameter like eventhandler delegates which has a two parameters but no return value...there's another solution to that anyway,

    just don't mess around changing states in your thread just to get back and return information in your UI.

  6. #6
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    okay...try to do it this way, hope you will get what I mean to say here

    Here's what I did.

    1. Make a Form, add WebBrowser control and 1 button.
    2. I add some Eventhandler in my button1 so aside from

    this.button1.Click += new System.EventHandler(this.button1_Click);

    I have like this.

    this.button1.Click += new System.EventHandler(StartingThread);

    3. on my StartingThread Method, I put some locking, create a thread of course, and setting the "IsBackground = true" and of course trigger the "Start()" method.

    something like this:

    Thread t1 = new Thread(new ThreadStart(AThreadedFunction));
    t1.IsBackground = true;
    t.start();
    4. use BeginInvoke(), you can use a statement like this:

    if(InvokeRequired){...}
    else{ GOTCHA! you must be in the UI Thread! }

    I will just assumed that you have this similar way in displaying HTML in your webbrowser component.

    if (webBrowser1.Document != null)
    {
    webBrowser1.Document.OpenNew(true);
    webBrowser1.Document.Write("First Display");
    }
    else
    {
    webBrowser1.DocumentText = "Another Display and more to come";
    }

    Please understand also the AllowNavigation properties and DocumentCompleted Event.

    Have Fun...

  7. #7
    By the way, did you read the MSDN regarding multi-threading and resources issues?

    What is the use of you're webrowser control? I think it is better to use webclient.

  8. #8
    @TS: You can call WebBrowser.Document.InvokeScript in a background thread but through a delegate function.

    I tried to simulate your problem by creating a website that has a simple javascript function that displays an alert message.

    i tried your solution number 2 by using a background thread.

    in the background thread i called a delegate function to execute WebBrowser.Document.InvokeScript.
    this way you are calling the InvokeScript in a thread safe manner.

    bali combination xa sa imo solution number 1 and 2.

  9. #9
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    I tried to simulate it.

    here's my bin file: RapidShare: 1-CLICK Web hosting - Easy Filehosting

    there got 3 buttons.

    W/out BGWorker Button = form freezes at it loads.
    Via BGWorker Button = runs separate thread.
    Application.DoEvents Button = just an addition knowledge of this method.

    The problem is seems not on the invoking the script, is on the construction of the backgroundworker or threading. just refer my post above too...


    can you give me some script artoy? jscript, javascript or anything that can be invoke?

  10. #10
    mao ni ako gbutang sa sample site na ako gi create.

    function sampleScript()
    {
    alert('From Javascript');
    }

    the message should be displayed when the button that calls the backgroundworker.DoWork();

    hehehehe.

  11.    Advertisement

Page 1 of 6 1234 ... LastLast

Similar Threads

 
  1. love can be expressed in various ways
    By Meganda in forum "Love is..."
    Replies: 6
    Last Post: 08-27-2012, 04:53 AM
  2. Replies: 36
    Last Post: 04-11-2011, 09:13 PM
  3. Bidding Script - Would it be Possible in istorya.net
    By salbahis in forum Support Center
    Replies: 1
    Last Post: 02-02-2009, 11:49 AM
  4. how can i prevent from being spied in yahoo messenger?
    By boyq in forum Networking & Internet
    Replies: 21
    Last Post: 06-06-2008, 12:42 PM
  5. Replies: 3
    Last Post: 01-14-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top