in reply to Re: Tk and Threads
in thread Tk and Threads
Thank you for your answer. But I think I have to clarify a bit to avoid misunderstandings.
No I'm not starting the thread from a Tk callback. I know that this causes problems. The thread is started at the beginning in "thread_example.pl" with the call createMyThread().
The thread is running before Tk code is executed. I gave the thread two states: STATE_IDLE and STATE_WORK. My idea was to change the states via Events. Initially the thread is in the STATE_IDLE. So it is nothing doing as waiting.
Within the Tk callback I'm calling evWorkStart. This function is generating the event EV_WORK_START and giving data to the thread via a queue. So the state of the thread is changed from STATE_IDLE to STATE_WORK. In STATE_WORK the work function of the "ThreadWorker" is called.
When the work is done the thread is automatically changing its state to STATE_IDLE.
But the user has the possibility to press the cancel-button before the work is finished. This calls the function evWorkCancel and the event EV_WORK_CANCEL is generated. This event causes the thread to go back to the STATE_IDLE.
The problem is now that the work function is in reality very complex and has a lot of code in many modules. To achieve the goal that this work function is returning very fast I inserted function calls shallWorkBeCancelled which check if the event EV_WORK_CANCEL is there, i.e. the user pressed the cancel-button.
Because the work function is complex I had to call shallWorkBeCancelled in several modules which are called from the work-function. This is in my opinion pollution of the code.
I read the article about Tk-with-Worker-Threads. But if I understand it right it is more or less doing the same than me. It uses the shared variable thread_die.
The work-function in the Tk-with-worker-threads is very simple. So you only had to add the line if( $thread_die == 1 ){return} at one place. But if you also would have a complex function there you would have to place this line at several places. And so there would be the same pollution of code.
Please correct me If I am saying something wrong.
Your hint with the signals sound interesting. But I don't know it until now. I first have to learn about and try. And for me it is important that it is working with linux and windows. If signals only work with linux then this solution will not help me unfortunately.
Switching to gtk is also a good hint. But the problem is that I have a lot of GUI-stuff written in Tk. So if possible I want to realize the threading with Tk. But I really think about learning GTK to develop my new code with this gui-environment.
Or perhaps I'm thinking to complex. Do I really need a thread? My scenario is that the user is pressing on a start button. This shall start a function with a lot of work. I want that the user can interrupt this function at any time by pressing the cancel-button.
And I really want to say explicitly thank you to you zentara. Because nearly all my knowledge about "threads and tk" I have from posts of you which I read here in the perlmonks forum. Only with this help I was able to develop my "tk and thread solution".
Greetings,
Dirk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Tk and Threads
by zentara (Cardinal) on Dec 22, 2010 at 12:35 UTC | |
by BrowserUk (Patriarch) on Dec 22, 2010 at 13:23 UTC | |
by zentara (Cardinal) on Dec 22, 2010 at 20:31 UTC |