How can I close a dialog from another thread?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I close a dialog from another thread?

Robert Unguran
Hi evryone,

I would like to display a progress dialog during the TCP connection establishment

    def object_connect_changed( self, item ):
        try:
            mypg = ProgressDialog( message = "Connection to " + item.object.ip_address + ". Please wait...",
                                   title = "Connecting...", can_cancel = False )
            mypg.open()
              
            client_thread = threading.Thread( target = self.__connection_checks, args = ( mypg, item ) )
            client_thread.start()
            client_thread.join( 0 )
        except:
            pass
   
    def __connection_checks( self, mypg, item ):
        tcpconnection = tcpc.TCPHandler( application = item.object.application,
                         ip = item.object.ip_address, port = item.object.port )
        tcpconnection.setup()
       
           
        while tcpconnection.get_status() < 0:               
            time.sleep( 0.1 )
          
        if tcpconnection.get_status() == 1:
            msd = MasterData( ip = item.object.ip_address, port = item.object.port,
                              connection_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
                              connection_instance = tcpconnection )
           
            item.object.masterlist.append( msd )
       
        mypg.close()

How can I update the gui and close the dialog from another thread because I am getting the following error

[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
Aborted (core dumped)


Thank you for your advice.

Regards,
Robert



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How can I close a dialog from another thread?

Robert Kern
On Tue, Oct 1, 2013 at 4:34 PM, Robert Unguran <[hidden email]> wrote:

> Hi evryone,
>
> I would like to display a progress dialog during the TCP connection
> establishment
>
>     def object_connect_changed( self, item ):         try:             mypg
> = ProgressDialog( message = "Connection to " + item.object.ip_address + ".
> Please wait...",                                    title = "Connecting...",
> can_cancel = False )             mypg.open()
> client_thread = threading.Thread( target = self.__connection_checks, args =
> ( mypg, item ) )             client_thread.start()
> client_thread.join( 0 )         except:             pass         def
> __connection_checks( self, mypg, item ):         tcpconnection =
> tcpc.TCPHandler( application = item.object.application,
> ip = item.object.ip_address, port = item.object.port )
> tcpconnection.setup()                             while
> tcpconnection.get_status() < 0:                            time.sleep( 0.1 )
> if tcpconnection.get_status() == 1:             msd = MasterData( ip =
> item.object.ip_address, port = item.object.port,
> connection_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
> connection_instance = tcpconnection )
> item.object.masterlist.append( msd )                 mypg.close()
>
> How can I update the gui and close the dialog from another thread because I
> am getting the following error

Anything touching the UI needs to happen in the UI thread. You can use
GUI.invoke_later() to make a function call on the UI thread. If you
have a Traits UI viewing `item.object.masterlist`, then the `append()`
also needs to happen on the UI thread, too.

from pyface.api import GUI

def _thread_callback(master_list, master_data, progress_dialog):
    if master_data is not None:
        master_list.append(master_data)
    progress_dialog.close()

...

    def __connection_checks( self, mypg, item ):
        tcpconnection = tcpc.TCPHandler( application =
item.object.application, ip = item.object.ip_address, port =
item.object.port )
        tcpconnection.setup()
        while tcpconnection.get_status() < 0:
            time.sleep( 0.1 )
        msd = None
        if tcpconnection.get_status() == 1:
            msd = MasterData( ip = item.object.ip_address, port =
item.object.port,
                              connection_time =
datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
                              connection_instance = tcpconnection )

        GUI.invoke_later(_thread_callback, item.object.masterlist, msd, mypg)

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: How can I close a dialog from another thread?

Robert Unguran
Hi Robert,

Thank you very much for your help, that solved my problem.

Regards
Robert Unguran

On 10/01/2013 05:47 PM, Robert Kern wrote:

> On Tue, Oct 1, 2013 at 4:34 PM, Robert Unguran <[hidden email]> wrote:
>> Hi evryone,
>>
>> I would like to display a progress dialog during the TCP connection
>> establishment
>>
>>      def object_connect_changed( self, item ):         try:             mypg
>> = ProgressDialog( message = "Connection to " + item.object.ip_address + ".
>> Please wait...",                                    title = "Connecting...",
>> can_cancel = False )             mypg.open()
>> client_thread = threading.Thread( target = self.__connection_checks, args =
>> ( mypg, item ) )             client_thread.start()
>> client_thread.join( 0 )         except:             pass         def
>> __connection_checks( self, mypg, item ):         tcpconnection =
>> tcpc.TCPHandler( application = item.object.application,
>> ip = item.object.ip_address, port = item.object.port )
>> tcpconnection.setup()                             while
>> tcpconnection.get_status() < 0:                            time.sleep( 0.1 )
>> if tcpconnection.get_status() == 1:             msd = MasterData( ip =
>> item.object.ip_address, port = item.object.port,
>> connection_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
>> connection_instance = tcpconnection )
>> item.object.masterlist.append( msd )                 mypg.close()
>>
>> How can I update the gui and close the dialog from another thread because I
>> am getting the following error
> Anything touching the UI needs to happen in the UI thread. You can use
> GUI.invoke_later() to make a function call on the UI thread. If you
> have a Traits UI viewing `item.object.masterlist`, then the `append()`
> also needs to happen on the UI thread, too.
>
> from pyface.api import GUI
>
> def _thread_callback(master_list, master_data, progress_dialog):
>      if master_data is not None:
>          master_list.append(master_data)
>      progress_dialog.close()
>
> ...
>
>      def __connection_checks( self, mypg, item ):
>          tcpconnection = tcpc.TCPHandler( application =
> item.object.application, ip = item.object.ip_address, port =
> item.object.port )
>          tcpconnection.setup()
>          while tcpconnection.get_status() < 0:
>              time.sleep( 0.1 )
>          msd = None
>          if tcpconnection.get_status() == 1:
>              msd = MasterData( ip = item.object.ip_address, port =
> item.object.port,
>                                connection_time =
> datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ),
>                                connection_instance = tcpconnection )
>
>          GUI.invoke_later(_thread_callback, item.object.masterlist, msd, mypg)
>

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev