Page 2 of 6 FirstFirst 12345 ... LastLast
Results 11 to 20 of 54
  1. #11
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1

    would it be nice, that it does a little bit longer or something that it waits...? so we could add some event to delay the other thread.

    something like this.


    UI THREAD ---> BACKGROUNDWORKER ----> THREADPOOL ----> CHILD THREAD.

    while on child thread the UI is still accessible, like cancel, or starting another JOB. hehehe

    however the TS is nowhere to be found til now ^___________^

  2. #12
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    if we have multiple scripts to call, as it really possible to have multiple scripts under 1 page., so we can spawn this as child thread,

    the threadpool will act like a proxy, or act as main thread (which is not obviously) just to monitor if all scripts are done properly.

    BackGroundWorker of course connects to our UI THREAD.. so we will not break the RULES to modify the UI outside of its own thread. Coz Windows UI runs a single thread... what will happen if you will modify it outside from its own thread? windows will go crazy thinking what happen to the previous job he did.

    I got another way, but I don't want to make it more complicated this time, for this will led us to another topic like ThreadSafe, & Locking. but for simple understanding, if you LOCK the resources of your UI then it is impossible for another thread to alter, but then again, think how to unlock that's it for now. cheersss....

  3. #13
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    recompiled download here -> hxxp://rapidshare.com/files/315981718/Debug.rar

  4. #14
    @junkfactory: musta na ni?

  5. #15
    @junkfactory: musta na ni?

  6. #16
    WOhoo!! Sorry sa dugay reply mga bro! Party time man gud diri mao lipong2x pirmi.

    Background lang ni sa ako little project. Some istoryans may know who I am so please hush hush . I also think some of you might be interested in this little project and could use the ideas as well.

    The idea is to create a scriptable windows form based application.

    Scripting language chosen is javascript. The application exposes an API for script usage.

    The script can control all and any aspect of the form. Scripts needs to download any other resources from the INET, read local files, prevent closing of the form, display form messageboxes (not js alert()), report progress ... etc so these are done through the API.

    There can be one or more scripts that can interact with the form or interact with its fellow scripts.

    A webbrowser control is needed because the scripts can/needs to create html elements (forms and all web element stuff).

    So back to the problem.

    Right now, I scrapped the threading and sticked to a single thread momentarily. I have traced down to the very first call of Document.InvokeScript. Further down, a script calls one of my API functions "GetContents(url)" to download a resource from the NET which is using WebClient.DownloadStringAsync(url).

    Code:
          
    public string GetContents(string url)
    {
                    //snip .......
                    WebClient client = new WebClient();
                    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                     client.DownloadStringAsync(new Uri(url), this); <-- Main form freeze momentarily here but only of very first call!!!!
                     Application.UseWaitCursor = true;
                    while (IsDownloadCompleted == false)
                    {
                                    Application.DoEvents();
                    }
                    Application.UseWaitCursor = false;
                    client.Dispose();
                    return this.Contents.Context(ctx);
    }
    Now examining the thread executing WebClient.DownloadStringAsync(url), obviously, it is the main UI thread that's why I used the asynchronous method. But why does it freeze there? (marked in red). It is supposed to continue to the next instruction right? Or am I missing something that the framework needs to initialize some stuff?

    @Artoy, thanks for the idea, but using a delegate will cause the delegated function to be called on main thread which I think is the same as directly calling the Document.InvokeScript from the main form in this particular situation.

    @Mark, thanks I tried your test app. It appears the background worker works but I'm not totally sure because it completes very fast.Maybe you can try the real scenario I mentioned above? If you're up to it

    P.S. I'm just learning .NET so pardon my ignorance in some matters.

  7. #17
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Can you give some steps by steps process? when this method triggered? and how you triggered this? Is it right after the user click the button? I need also to see your method client_DownloadStringCompleted, and client_DownloadProgressChanged Inorder for me to simulate closer to your approach.

    also when you called that client.DownloadStringAsync(new Uri(url), this), although I can figure it out that "this" refers to the object token, as it only have 2 overloads out there. I still don't know how the class is being constructed. The class itself is the object you want to pass for this call? above on this post you said that want to download the resource by calling WebClient.DownloadStringAsync(url). So which is which?

    another thing I want to ask, what kind of resource you want to download? FILE? BYTES?

    I don't know if you agree with this, though I still not yet sure, the keyword "using" seems useful on this scene.
    You are calling client.dispatch() explicitly, I feel not safe on this manner especially if you work with threads, this may lead to dispose your object while the thread is running.

    I suggest you to have implement like this:

    using (WebClient client = new WebClient())
    {
    try
    {
    //implement you work here
    }
    catch(WebException we)
    {
    //notify user, thread or events or return to previous call
    }
    }
    I'm also new in .NET, and haven't done any serious Web App. Your suggestion and feedbacks are very much appreciated Hope I could give u some li'l help.

  8. #18
    i tried the delegate. it does not hang the form even if naa pa sleep ang sulod sa delegate. pero if naa sleep sa javascript mo hang na xa. hehehe.

    anyways we'll try to see if i can get webclient.DownloadStringAsync to work.

  9. #19
    Guess I was not very clear so here is what's going on.

    Quote Originally Posted by MarkCueing
    Can you give some steps by steps process? when this method triggered? and how you triggered this? Is it right after the user click the button?
    Let's consider my prototype to make things a bit simpler.

    1. The main form has a button control. On form load an html file in the local FS is loaded to the webbrowser control. Here is the contents of the html file :

    Code:
    <html>
    <body>
    <div id='contents'></div>
    </body>
    </html>
    2. On the DocumentCompleted event of the WebBrowser control. A JS file (still on the local FS) is injected to the current Document of the WebBrowser control. Here is the contents of the JS file

    Code:
    function displayCNN() {
          //GetContents is an API call
          var contents = GetContents('http://cnn.com');
          $('contents').innerHTML = contents;
    }
    4. In the OnClick handler of the button control on the form.

    Code:
    public void button_OnClick(object sender, EventArgs e)
    {
           //this calls the displayCNN JS function 
           this.browser.Document.InvokeScript('displayCNN', null);
    }
    So basically that is how the "GetContents" is triggered.

    Quote Originally Posted by MarkCueing
    I need also to see your method client_DownloadStringCompleted, and client_DownloadProgressChanged Inorder for me to simulate closer to your approach.
    Just set some IVAR that will contain the contents to be returned by the "GetContents(...)" method to the script. e.g. this.contents = e.Result.

    In the code I posted earlier I returned this.Contents.Context[ctx], just change this to the IVAR you used in client_DownloadStringCompleted. (this.contents). For client_DownloadProgressChanged just leave that blank or omit setting that event, it doesn't matter.

    Quote Originally Posted by MarkCueing
    also when you called that client.DownloadStringAsync(new Uri(url), this), although I can figure it out that "this" refers to the object token, as it only have 2 overloads out there. I still don't know how the class is being constructed. The class itself is the object you want to pass for this call? above on this post you said that want to download the resource by calling WebClient.DownloadStringAsync(url). So which is which?
    Just base on the code I posted earlier

    Quote Originally Posted by MarkCueing
    another thing I want to ask, what kind of resource you want to download? FILE? BYTES?
    The resource can be any plain text resource the script needs. I have another API for downloading binary resources and that's another story.

    Quote Originally Posted by MarkCueing
    I don't know if you agree with this, though I still not yet sure, the keyword "using" seems useful on this scene.
    You are calling client.dispatch() explicitly, I feel not safe on this manner especially if you work with threads, this may lead to dispose your object while the thread is running.
    I don't like using language specific constructs. I prefer to keep a coding standard across all progamming languages I use, but that's just me.

    Quote Originally Posted by ArToy
    i tried the delegate. it does not hang the form even if naa pa sleep ang sulod sa delegate. pero if naa sleep sa javascript mo hang na xa. hehehe.
    Try it with the code I posted earlier. I enumerated the steps above

    My question is why does my main form momentarily freeze on the client.DownloadStringAsync() as this is supposed to be asynchchronous? And why does it ONLY occur on the very first call?

    Oh and I also have a theory that whatever thread that executes browser.Document.InvokeScript(...) is the thread executing the JS function?

  10. #20
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Bro, is there any other thread running aside from UI Thread? The WebClient uses Events and not AsyncCallbacks, so therefore the entire model is not asynchronous.

    Can you try also WebRequest , WebResponse then use the AsyncCallback method?

    On my machine it runs so quick, both script and html files are in the local system. I just implement it by using another thread with Sleep() call, yet it’s still never froze my UI.

    Is there any online API you can suggest?

  11.    Advertisement

Page 2 of 6 FirstFirst 12345 ... 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