xtd - Reference Guide  0.1.1
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 
61  xtd::diagnostics::data_received_event_handler& operator+=(const xtd::diagnostics::data_received_event_handler& handler) noexcept;
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>
72  xtd::diagnostics::data_received_event_handler& operator+=(fn_t function) noexcept {
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 
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 
171  xtd::diagnostics::data_received_event_handler& operator+=(const xtd::diagnostics::data_received_event_handler& handler) noexcept;
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>
182  xtd::diagnostics::data_received_event_handler& operator+=(fn_t function) noexcept {
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 
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;
260 
272  int32_t exit_code() const;
273 
279 
287  intptr_t handle() const;
288 
296  bool has_exited() const;
297 
305  int32_t id() const;
306 
313 
346 
353 
462  std::istream& standard_error();
463 
527  std::ostream& standard_input();
528 
652  std::istream& standard_output();
653 
684 
689 
693  void close();
694 
703  void kill();
704 
711  bool start();
737  static process start(const ustring& file_name);
749  static process start(const ustring& file_name, const ustring& arguments);
750 
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 error data received event.
Definition: process.h:50
xtd::diagnostics::data_received_event_handler & operator-=(const typename xtd::diagnostics::data_received_event_handler::function_t &function) noexcept
Removes a function to the event.
bool is_empty() const noexcept
Gets a value indicate if the event is empty.
xtd::diagnostics::data_received_event_handler & operator-=(const xtd::diagnostics::data_received_event_handler &handler) noexcept
Removes an handler to the event.
Represents a process exit event.
Definition: process.h:105
bool is_empty() const noexcept
Gets a value indicate if the event is empty.
xtd::event_handler & operator-=(const xtd::event_handler &handler) noexcept
Removes an handler to the event.
xtd::event_handler & operator-=(const typename xtd::event_handler::function_t &function) noexcept
Removes a function to the event.
Represents a process output data received event.
Definition: process.h:160
xtd::diagnostics::data_received_event_handler & operator-=(const typename xtd::diagnostics::data_received_event_handler::function_t &function) noexcept
Removes a function to the event.
bool is_empty() const noexcept
Gets a value indicate if the event is empty.
xtd::diagnostics::data_received_event_handler & operator-=(const xtd::diagnostics::data_received_event_handler &handler) noexcept
Removes an handler to the event.
Specifies a set of values that are used when you start a process.
Definition: process_start_info.h:31
Provides access to local and remote processes and enables you to start and stop local system processe...
Definition: process.h:41
xtd::diagnostics::process_start_info & start_info()
Gets the properties to pass to the xtd::diagnostics::process::start() method of the xtd::diagnostics:...
const xtd::diagnostics::process_start_info & start_info() const
Gets the properties to pass to the xtd::diagnostics::process::start() method of the xtd::diagnostics:...
std::ostream & standard_input()
Gets a stream used to write the input of the application.
std::istream & standard_output()
Gets a stream used to read the textual output of the application.
process & enable_raising_events(bool value)
Sets whether the xtd::diagnostics::process::exited event should be raised when the process terminates...
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
xtd::diagnostics::process_priority_class priority_class() const
Gets the overall priority category for the associated process.
std::istream & standard_error()
Gets a stream used to read the error output of the application.
virtual void on_exited()
Raises the xtd::diagnostics::process::exited event.
ustring process_name() const
Gets the name of the process.
void close()
Frees all the resources that are associated with this component.
bool enable_raising_events() const
Gets whether the xtd::diagnostics::process::exited event should be raised when the process terminates...
static process start(const xtd::diagnostics::process_start_info &start_info)
Starts the process resource that is specified by the parameter containing process start information (...
int32_t base_priority() const
Gets the base priority xof the associated process.
process & start_info(const xtd::diagnostics::process_start_info &value)
SGets the properties to pass to the xtd::diagnostics::process::start() method of the xtd::diagnostics...
void kill()
Immediately stops the associated process.
process()
Initializes a new instance of the xtd::diagnostics::process class.
std::chrono::system_clock::time_point time_point
Represents a point in time.
Definition: process.h:211
int32_t id() const
Gets the unique identifier for the associated process.
bool has_exited() const
Gets a value indicating whether the associated process has been terminated.
static process start(const ustring &file_name, const ustring &arguments)
Starts a process resource by specifying the name of an application and a set of command-line argument...
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
time_point start_time() const
Gets the time that the associated process was started.
time_point exit_time() const
Gets the time that the associated process exited.
intptr_t handle() const
Gets the native handle of the associated process.
process & wait_for_exit(int32_t milliseconds)
Instructs the Process component to wait the specified number of milliseconds for the associated proce...
ustring machine_name() const
Gets the name of the computer the associated process is running on.
bool start()
Starts (or reuses) the process resource that is specified by the xtd::diagnostics::process::start_inf...
process & wait_for_exit()
Instructs the xtd::diagnostics::process component to wait indefinitely for the associated process to ...
process & priority_class(xtd::diagnostics::process_priority_class value)
Sets the overall priority category for the associated process.
exit_event exited
Occurs when a process exits.
Definition: process.h:781
static process start(const ustring &file_name)
Starts a process resource by specifying the name of a document or application file and associates the...
int32_t exit_code() const
Gets the value that the associated process specified when it terminated.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Contains xtd::diagnostics::data_received_event_handler event handler.
generic_event_handler<> event_handler
Represents the method that will handle an event that has no event data.
Definition: event_handler.h:33
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
#define core_export_
Define shared library export.
Definition: core_export.h:13
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
@ normal
Specifies that the process has no special scheduling needs.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::diagnostics::process_priority_class enum class.
Contains xtd::diagnostics::process_start_info class.