Wxpython uses threads to prevent false positives.
Some time ago, I wrote an industrial control software, and there was always a problem in use, that is, when the software retrieval device, because this function was executed for a long time, causing the GUI to be suspended, let the user know whether the software is still being executed or not. (Although I designed synchronous log display, this is also false)
The procedure is as follows:
The code is parsed as follows:
# -*- coding: utf-8 -*- import timeimport wxfrom threading import Threadfrom wx.lib.pubsub import Publisher
The time library is used to execute scheduled functions and simulate functions that require long execution. Publisher is used to transmit messages between threads.
Class TestThread (Thread): def _ init _ (self): # Start Thread immediately during Thread instantiation. _ init _ (self) self. start () def run (self): # code executed by the thread for I in range (101): time. sleep (0.03) wx. callAfter (Publisher (). sendMessage, "update", I) time. sleep (0.5) wx. callAfter (Publisher (). sendMessage, "update", u "End of thread ")
TestThread's _ init _ function defines that the thread is started when it is instantiated. The run function is a program that needs to be executed for a long time.
Wx. CallAfter and Publisher (). sendMessage are used to send messages to the GUI.
Class MyForm (wx. frame): def _ init _ (self, parent): wx. frame. _ init _ (self, parent, id = wx. ID_ANY, title = u "Leniy, 20140627", pos = wx. defaultPosition, size = wx. size (-1,-1), style = wx. DEFAULT_FRAME_STYLE | wx. TAB_TRAVERSAL) self. setSizeHintsSz (wx. defaultSize, wx. defaultSize) gSizer2 = wx. gridSizer (0, 3, 0, 0) self. m_button2 = wx. button (self, wx. ID_ANY, u "execution thread", wx. defaultPosition, wx. defaultSize, 0) gSizer2.Add (self. m_button2, 0, wx. ALL | wx. ALIGN_CENTER_HORIZONTAL | wx. ALIGN_CENTER_VERTICAL, 5) self. m_staticText2 = wx. staticText (self, wx. ID_ANY, u "MyLabel", wx. defaultPosition, wx. defaultSize, 0) self. m_staticText2.Wrap (-1) gSizer2.Add (self. m_staticText2, 0, wx. ALL | wx. ALIGN_CENTER_HORIZONTAL | wx. ALIGN_CENTER_VERTICAL, 5) self. m_gauge1 = wx. gauge (self, wx. ID_ANY, 100, wx. defaultPosition, wx. defaultSize, wx. GA_HORIZONTAL) self. m_gauge1.SetValue (0) gSizer2.Add (self. m_gauge1, 0, wx. ALL | wx. ALIGN_CENTER_HORIZONTAL | wx. ALIGN_CENTER_VERTICAL, 5) self. setSizer (gSizer2) self. layout () gSizer2.Fit (self) self. centre (wx. BOTH) self. m_button2.Bind (wx. EVT_BUTTON, self. onButton) Publisher (). subscribe (self. updateDisplay, "update") def updateDisplay (self, msg): t = msg. data if isinstance (t, int): # if it is a number, the thread is executing and the number self is displayed. m_staticText2.SetLabel ("% s %" % t) self. m_gauge1.SetValue (t) else: # Otherwise, the thread is not executed and the button is re-enabled. m_staticText2.SetLabel ("% s" % t) self. m_button2.Enable () def onButton (self, event): TestThread () self. m_staticText2.SetLabel (u "thread start") event. getEventObject (). disable ()
This is the main program of the GUI. In __init _, Publisher (). subscribe (self. updateDisplay, "update") is used to declare the method for obtaining thread messages and displaying them.
if __name__ == "__main__": app = wx.PySimpleApp() MyForm(None).Show() app.MainLoop()
Finally, the MyForm (None). Show () window is displayed, and the program is complete.
Delphi uses the thread to display the main thread window and suspends itself. How can this problem be solved?
Var MyAccess: TAccessApplication; // access FileName: WideString; // ************************* var ADOConnection: TADOConnection; // This statement must be added to the adoconnection control or uses adodb; ADOQuery: TADOQuery; temppath: string; // store the temporary directory tb: boolean; // check whether the file exists begin try // first determine whether the database tb: = fileexists (edit1.Text); if tb then exit; // if the database exists, exit MyAccess: = TAccessApplication. create (Self); // myaccess. closecurspondatabase; // close database engin FileName: = edit1.Text; // if the path does not exist, the program will not automatically create MyAccess. newCurrentDatabase (FileName); myaccess. closecurspondatabase; MyAccess. disconnect; MyAccess. free; // After the above databases are created, create tables and fields below
How does delphi use a thread to solve the form false death problem?
Create a thread to run it.