Results 1 to 7 of 7

Thread: [2005] Update UI from a thread on another class!!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    77

    Unhappy [2005] Update UI from a thread on another class!!!

    Hi,

    This is one great article. Actually the best I have seen yet. I just have one issue if I may raise it here. I am not sure whether it should be on a spearate thread but if this is not the correct place please move it to wherever you see fit.

    Well, I have an issue with invoking my main from to update its UI from an external class rather than the same class like you explained. What I am doing is as follows:

    1. Declare a public delegate on the form
    2. Declare a public procedure on the form
    3. Spawn an instance of my external class and do some work and then update public variables in an external module.
    4. When done, I get a handle on the main form using
    Code:
    Application.OpenForms("mainForm")
    5. Invoke the UpdateUI procedure in the form like this:
    Code:
    Application.OpenForms("mainForm").Invoke(New UpdateGUIDelegate(AddressOfmainForm.UpdateUI))
    The code proceeds with no errors and by stepping through code I can see the textBoxes get assigned the value in the public variables. however, the form text boxes never show the new values!! I tried using application.doevents but no luck. I also tried adding a check on me.invokeRequired at the begining of the UpdateUI form but it always returned FALSE.
    Sample code here: Download 52K
    Please help..

    Thanks confused:
    Last edited by leedo2k; Aug 19th, 2008 at 11:57 AM.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2005] Update UI from a thread on another class!!!


    Try a System.threading.thread.sleep(200) after setting the textbox values and see if they appear.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    77

    Unhappy Re: [2005] Update UI from a thread on another class!!!

    Nope, doesn't work.

  4. #4
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2005] Update UI from a thread on another class!!!

    Did you do it on the thread that is handling the UI?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Update UI from a thread on another class!!!

    I would pass the form instance to the class so that it can update the controls. For instance
    Code:
    Public Property ParentForm() As MainForm
    ...
    Then you can invoke everything from within your class.


    Another option is to pass an ISynchronizeInvoker object to the class and use events in your class. When values are set you invoke the events on the ui thread using the ISynchronizeInvoker Object.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    77

    Re: [2005] Update UI from a thread on another class!!!

    Quote Originally Posted by dolot
    Did you do it on the thread that is handling the UI?
    Yes, I did. But it did not work. Thanks anyway.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    77

    Re: [2005] Update UI from a thread on another class!!!

    Quote Originally Posted by bmahler
    I would pass the form instance to the class so that it can update the controls. For instance
    Code:
    Public Property ParentForm() As MainForm
    ...
    Then you can invoke everything from within your class.
    Yes, that worked. This is abrilliant and at the same time a very clean implementation. Thank very much bmahler

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width