Asynchronous operations
| Chapter 19. Printing |

## Asynchronous operations

By default, `PrintOperation::run()` returns when a print operation is completed. If you need to run a non-blocking print operation, call `PrintOperation::set_allow_async()`. Note that `set_allow_async()` is not supported on all platforms, however the `done` signal will still be emitted.

`run()` may return `PRINT_OPERATION_RESULT_IN_PROGRESS`. To track status and handle the result or error you need to implement signal handlers for the `done` and `status_changed` signals:

For instance,

    // in class ExampleWindow's method...
    Glib::RefPtr<PrintOperation> op = PrintOperation::create();
    // ...set up op...
    op->signal_done().connect(sigc::bind(sigc::mem_fun(*this, &ExampleWindow::on_printoperation_done), op));
    // run the op

Second, check for an error and connect to the `status_changed` signal. For instance:

    void ExampleWindow::on_printoperation_done(Gtk::PrintOperationResult result, const Glib::RefPtr<PrintOperation>& op)
    {
      if (result == Gtk::PRINT_OPERATION_RESULT_ERROR)
        //notify user
      else if (result == Gtk::PRINT_OPERATION_RESULT_APPLY)
        //Update PrintSettings with the ones used in this PrintOperation

      if (! op->is_finished())
        op->signal_status_changed().connect(sigc::bind(sigc::mem_fun(*this, &ExampleWindow::on_printoperation_status_changed), op));
    }

Finally, check the status. For instance,

    void ExampleWindow::on_printoperation_status_changed(const Glib::RefPtr<PrintFormOperation>& op)
    {
      if (op->is_finished())
        //the print job is finished
      else
        //get the status with get_status() or get_status_string()

      //update UI
    }

Rendering text |  | Export to PDF