Modify controls/objects from a subthread

Hi!

Formerly I programmed in Delphi, and I learned well the problems of threading.
In Delphi the VCL library is not thread safe. Because of that the threaded process codes must be splitted to two parts.

The main thread is handle all of visual controls, user events (key, clicks, etc.), and receive the informations from subthread (with mutex/lock protected memory areas; or with messages: like Synchronize).
The subthread never access the controls directly (because of hard detectable VLC errors).

For example:
If I write an Email processor utility, I must define the main window, and it’s controller functions.
The subthread is separated from that, it have input memory area, output memory area, and it have locks.

The main thread periodically checks the outputs, and if needs, it feed the thread with the next parameters.

I wanna ask that:

Are wx objects thread safe?
Can I access them directly from the subthread?

If not, and I must use the separated modell, how can I send a special message to the Main Frame from the subthread?
Like:
PostMessage(MainFrame.Handle, WM_MYSPECIALID, lParam, wParam);

Thanks for your help and for your informations!

dd

Durumdara wrote:

Hi!

Formerly I programmed in Delphi, and I learned well the problems of threading.
In Delphi the VCL library is not thread safe. Because of that the threaded process codes must be splitted to two parts.

The main thread is handle all of visual controls, user events (key, clicks, etc.), and receive the informations from subthread (with mutex/lock protected memory areas; or with messages: like Synchronize).
The subthread never access the controls directly (because of hard detectable VLC errors).

For example:
If I write an Email processor utility, I must define the main window, and it's controller functions.
The subthread is separated from that, it have input memory area, output memory area, and it have locks.
The main thread periodically checks the outputs, and if needs, it feed the thread with the next parameters.

I wanna ask that:

Are wx objects thread safe?
Can I access them directly from the subthread?

If not, and I must use the separated modell, how can I send a special message to the Main Frame from the subthread?
Like:
PostMessage(MainFrame.Handle, WM_MYSPECIALID, lParam, wParam);

Thanks for your help and for your informations!
    dd

The best place to start is to troll the archives a bit as this question gets asked about once a month. You cannot call much of wx from threads in a non-blocking manner, with a few exceptions. Mainly what you want to do is use wx.CallAfter, wx.PostEvent and a few others...which I don't recall, but someone else will. You can use the pubsub module in conjunction with the CallAfter method to send messages between threads too.

That should get you going anyway.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Durumdara wrote:

I wanna ask that:

Are wx objects thread safe?

No.

Can I access them directly from the subthread?

No. All UI creation and manipulation needs to be done in the context of the main thread.

If not, and I must use the separated modell, how can I send a special message to the Main Frame from the subthread?
Like:
PostMessage(MainFrame.Handle, WM_MYSPECIALID, lParam, wParam);

You can do it with custom events and wx.PostEvent, as shown in the Thread sample in the demo. There is also the wx.CallAfter function which takes a callable object and any parameters to be passed to it, and then posts an event that will cause the function to be called in the main gui thread's context.

See also: LongRunningTasks - wxPyWiki

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!