Results 1 to 6 of 6
  1. #1
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Calling a method on mainthread when another thread finishes

    Guys, I have a problem. I am currently working on a Email client in QT Jambi just for learning. The GUI of QT only works on the main thread.

    My main problem is that while I fetch the emails from the server, I perform this operation on a different thread to avoid blocking of the UI.
    but when the thread finishes fetching the mails I want to display all of them as a list on a Table. this Table can only be accessed by the main thread only, otherwise the program will crash.

    So, how can I call a method on main thread when the other thread finishes or I can notify the main thread to call that method.

    Plz help...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calling a method on mainthread when another thread finishes

    Can you make a small, simple program that compiles, executes and shows the problem? Use a dummy method to simulate getting mail on another thread, perhaps with calls to sleep() to pass time.

    What is the "main" thread doing while the background task is running? Is the "main" thread the same as the Swing EDT?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calling a method on mainthread when another thread finishes

    Quote Originally Posted by gauravfzd View Post
    So, how can I call a method on main thread when the other thread finishes or I can notify the main thread to call that method.
    You can notify objects by using listeners such as a PropertyChangeListener.

  4. #4
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: Calling a method on mainthread when another thread finishes

    Quote Originally Posted by Norm View Post
    What is the "main" thread doing while the background task is running? Is the "main" thread the same as the Swing EDT?
    By main thread I mean the original thread on which the actual program execution starts. In other words, the thread on which main(String[]args) runs.

    While the background task runs, main thread allows the application to respond to the user's action. Otherwise the application will hang while the mail fetching is underway.

    I need a way to know when the background task ends and then I'll refresh the User interface. This refreshing of UI must be done on the main thread otherwise the app will crash.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calling a method on mainthread when another thread finishes

    Quote Originally Posted by gauravfzd View Post
    By main thread I mean the original thread on which the actual program execution starts. In other words, the thread on which main(String[]args) runs.

    While the background task runs, main thread allows the application to respond to the user's action. Otherwise the application will hang while the mail fetching is underway.

    I need a way to know when the background task ends and then I'll refresh the User interface. This refreshing of UI must be done on the main thread otherwise the app will crash.
    My answer must have been written in invisible ink as it applies to just this situation.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calling a method on mainthread when another thread finishes

    Not sure how you are using the thread that the JVM uses to call the main() method. In many programs, the main() method creates the GUI, does some init work and returns. That original thread is not executing anywhere in the program after main() exits.
    When the user creates an event, the JVM calls a listener on another thread.

    What is the thread used when the main() method is initially called by the JVM doing while the background task is executing?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Block thread until callback method has been called by other thread
    By James2000 in forum Threads and Synchronization
    Replies: 4
    Last Post: 05-11-2012, 09:46 PM
  2. Replies: 10
    Last Post: 11-29-2010, 02:02 PM
  3. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  4. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 02:13 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •