xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
process.h
Go to the documentation of this file.
1 #pragma once
5 #include "../core_export.h"
6 #include "../event.h"
7 #include "../event_handler.h"
8 #include "../object.h"
9 #include "../ustring.h"
11 #include "process_priority_class.h"
12 #include "process_start_info.h"
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 #include <optional>
17 #include <thread>
18 
20 struct __init_process_message_box_message__;
22 
24 namespace xtd {
26  namespace diagnostics {
41  class core_export_ process final : public object {
42  struct data;
43 
44  public:
51  friend process;
52  void set_data(data* data) {data_ = data;}
53  public:
56  bool is_empty() const noexcept;
57 
62 
66  xtd::diagnostics::data_received_event_handler& operator+=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
67 
71  template<typename fn_t>
73  data_->error_data_received_callback_+=(function);
74  return xtd::diagnostics::data_received_event_handler::operator+=(function);
75  }
76 
81 
85  xtd::diagnostics::data_received_event_handler& operator-=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
86 
90  template<typename fn_t>
91  xtd::diagnostics::data_received_event_handler& operator -=(fn_t function) noexcept {
92  data_->error_data_received_callback_-=(function);
93  return data_received_event_handler::operator-=(function);
94  }
95 
96  private:
97  data* data_ = nullptr;
98  };
99 
105  class exit_event : protected xtd::event_handler {
106  friend process;
107  void set_data(data* data) {data_ = data;}
108  public:
111  bool is_empty() const noexcept;
112 
116  xtd::event_handler& operator+=(const xtd::event_handler& handler) noexcept;
117 
121  xtd::event_handler& operator+=(const typename xtd::event_handler::function_t& function) noexcept;
122 
126  template<typename fn_t>
127  xtd::event_handler& operator+=(fn_t function) noexcept {
128  data_->exit_callback_+=(function);
129  return xtd::event_handler::operator+=(function);
130  }
131 
135  xtd::event_handler& operator-=(const xtd::event_handler& handler) noexcept;
136 
140  xtd::event_handler& operator-=(const typename xtd::event_handler::function_t& function) noexcept;
141 
145  template<typename fn_t>
146  xtd::event_handler& operator -=(fn_t function) noexcept {
147  data_->exit_callback_-=(function);
148  return xtd::event_handler::operator-=(function);
149  }
150 
151  private:
152  data* data_ = nullptr;
153  };
154 
161  friend process;
162  void set_data(data* data) {data_ = data;}
163  public:
166  bool is_empty() const noexcept;
167 
172 
176  xtd::diagnostics::data_received_event_handler& operator+=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
177 
181  template<typename fn_t>
183  data_->output_data_received_callback_+=(function);
184  return xtd::diagnostics::data_received_event_handler::operator+=(function);
185  }
186 
191 
195  xtd::diagnostics::data_received_event_handler& operator-=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
196 
200  template<typename fn_t>
201  xtd::diagnostics::data_received_event_handler& operator -=(fn_t function) noexcept {
202  data_->output_data_received_callback_-=(function);
203  return xtd::diagnostics::data_received_event_handler::operator-=(function);
204  }
205 
206  private:
207  data* data_ = nullptr;
208  };
209 
211  using time_point = std::chrono::system_clock::time_point;
212 
220  process();
222  process(const process&) = default;
223  process& operator=(const process& value);
224  ~process();
226 
242  int32_t base_priority() const;
243 
251  bool enable_raising_events() const;
259  process& enable_raising_events(bool value);
260 
272  int32_t exit_code() const;
273 
278  time_point exit_time() const;
279 
287  intptr_t handle() const;
288 
296  bool has_exited() const;
297 
305  int32_t id() const;
306 
312  ustring machine_name() const;
313 
329  xtd::diagnostics::process_priority_class priority_class() const;
345  process& priority_class(xtd::diagnostics::process_priority_class value);
346 
352  ustring process_name() const;
353 
462  std::istream& standard_error();
463 
527  std::ostream& standard_input();
528 
652  std::istream& standard_output();
653 
663  const xtd::diagnostics::process_start_info& start_info() const;
683  process& start_info(const xtd::diagnostics::process_start_info& value);
684 
688  time_point start_time() const;
689 
693  void close();
694 
703  void kill();
704 
711  bool start();
725  static process start(const xtd::diagnostics::process_start_info& start_info);
737  static process start(const ustring& file_name);
749  static process start(const ustring& file_name, const ustring& arguments);
750 
758  process& wait_for_exit();
759 
768  process& wait_for_exit(int32_t milliseconds);
769 
775 
782 
788 
789  protected:
793  virtual void on_exited();
794 
795  private:
796  struct data {
798  std::optional<intptr_t> handle_ = 0;
799  int32_t id_ = 0;
800  ustring machine_name_;
802  std::unique_ptr<std::ostream> standard_input_;
803  std::unique_ptr<std::istream> standard_output_;
804  std::unique_ptr<std::istream> standard_error_;
805  std::thread thread_;
806  process::time_point start_time_;
807  process::time_point exit_time_;
808  bool enable_raising_events_ = false;
809  std::optional<int32_t> exit_code_;
810  event_handler exit_callback_;
811  data_received_event_handler error_data_received_callback_;
812  data_received_event_handler output_data_received_callback_;
813  std::exception_ptr exception_pointer_;
814  };
815  std::shared_ptr<data> data_ = std::make_shared<data>();
816  friend __init_process_message_box_message__;
817  inline static xtd::delegate<void(const ustring&)> message_box_message_;
818  };
819  }
820 }
Represents a process output data received event.
Definition: process.h:160
process_priority_class
Indicates the priority that the system associates with a process. This value, together with the prior...
Definition: process_priority_class.h:19
generic_event_handler<> event_handler
Represents the method that will handle an event that has no event data.
Definition: event_handler.h:33
Represents a process error data received event.
Definition: process.h:50
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
output_data_received_event output_data_received
Occurs each time an application writes a line to its redirected xtd::diagnostics::process::standard_o...
Definition: process.h:787
error_data_received_event error_data_received
Occurs when an application writes to its redirected xtd::diagnostics::process::standard_error stream...
Definition: process.h:774
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Specifies that the process has no special scheduling needs.
Contains xtd::diagnostics::process_start_info class.
xtd::delegate< void(object &sender, const xtd::diagnostics::data_received_event_args &e)> data_received_event_handler
Represents the method that will handle the xtd::diagnostics::process::output_data_received and xtd::d...
Definition: data_received_event_handler.h:20
Starting of a logical operation.
Provides access to local and remote processes and enables you to start and stop local system processe...
Definition: process.h:41
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:26
xtd::diagnostics::data_received_event_handler & operator+=(fn_t function) noexcept
Adds a function to the event.
Definition: process.h:182
Contains xtd::diagnostics::process_priority_class enum class.
Represents a process exit event.
Definition: process.h:105
Contains xtd::diagnostics::data_received_event_handler event handler.
xtd::diagnostics::data_received_event_handler & operator+=(fn_t function) noexcept
Adds a function to the event.
Definition: process.h:72
exit_event exited
Occurs when a process exits.
Definition: process.h:781
std::chrono::system_clock::time_point time_point
Represents a point in time.
Definition: process.h:211
xtd::event_handler & operator+=(fn_t function) noexcept
Adds a function to the event.
Definition: process.h:127
Specifies a set of values that are used when you start a process.
Definition: process_start_info.h:31