|
Problem This is in response to Benjamin Smith's post titled Preventing concurrent processing.
I thought this is an interesting problem. As you start to design more complicated applications using PHP-GTK2, you will soon find that you will face the same problem, i.e. how to process concurrent signals that might be generated by different the widgets?
Before presenting the solution in the next three articles, I wrote this example to illustrate the situation. Suppose you have set two buttons as shown below. Try clicking on each of the button a couple of times. Would you want to guess how PHP-GTK2 will react to these "concurrent" buttons clicks?

p.s. Do run through this example. You will get a deeper understanding of how PHP-GTK2 handles a stream of consecutive signals.
Solution
- For each button click, we run the signal handler
on_button().
- Each button click will start a "long" process which is reflected by the progress bar running from 0% to 100%.
- There are two status line. The first status line shows the total number of clicks, while the second status line shows which click PHP-GTK2 is processing now.
- Try clicking each button a number of times, and notice the order in which the clicks are being processed.
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?php $window = new GtkWindow(); $window->set_size_request(400, 175); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Processing concurrent button clicks"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0); $vbox->pack_start(new GtkLabel(), 0);
$vbox->pack_start($hbox = new GtkHBox(), 0); $hbox->pack_start($vbox1 = new GtkVBox(), 0); $hbox->pack_start(new GtkVBox()); $hbox->pack_start($vbox2 = new GtkVBox(), 0);
// set up button 1
$vbox1->pack_start($button1 = new GtkButton('Button 1'), 0); $vbox1->pack_start($status11 = new GtkLabel(), 0); $vbox1->pack_start($status12 = new GtkLabel(), 0); $vbox1->pack_start($progress1 = new GtkProgressBar(), 0); $progress1->set_orientation(Gtk::PROGRESS_LEFT_TO_RIGHT); $progress1->set_size_request(175, -1); $button1->connect('clicked', 'on_button', 1, $progress1, $status11, $status12); $num_clicks[1] = 0;
// set up button 2
$vbox2->pack_start($button2 = new GtkButton('Button 2'), 0); $vbox2->pack_start($status21 = new GtkLabel(), 0); $vbox2->pack_start($status22 = new GtkLabel(), 0); $vbox2->pack_start($progress2 = new GtkProgressBar(), 0); $progress2->set_orientation(Gtk::PROGRESS_LEFT_TO_RIGHT); $progress2->set_size_request(175, -1); $button2->connect('clicked', 'on_button', 2, $progress2, $status21, $status22); $num_clicks[2] = 0;
$window->show_all(); Gtk::main();
function on_button($button, $id, $progress, $status1, $status2) { global $num_clicks, $progress_count; ++$num_clicks[$id]; // note 1
$progress_count[$id][$num_clicks[$id]] = 0; // note 2
$status1->set_text("Total number of clicks ($id): {$num_clicks[$id]}"); Gtk::idle_add('update_progressbar', $progress, $id, $num_clicks[$id], $status1, $status2); // note 3
}
function update_progressbar($progress, $id, $num_clicks, $status1, $status2) { $max = 1000;
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation You might want to take a look at How to display progress bar while processing long task - Part 1? that gives the details of setting up a progress bar.
What's new here:
- Queue the clicks.
- Start from zero for each click.
- Start processing each click.
- Update the progress bar.
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |