xtd - Reference Guide 0.2.0
process.h
Go to the documentation of this file.
1 #pragma once
5 #include "../core_export.h"
6 #include "../date_time.h"
7 #include "../event.h"
8 #include "../event_handler.h"
9 #include "../object.h"
10 #include "../ustring.h"
12 #include "process_priority_class.h"
13 #include "process_start_info.h"
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 {
46  class core_export_ process final : public xtd::object {
47  struct data;
48 
49  public:
56  friend process;
57  void set_data(process::data* data) {data_ = data;}
58  public:
60 
64  bool is_empty() const noexcept;
66 
68 
74 
78  xtd::diagnostics::data_received_event_handler& operator+=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
79 
83  template<typename fn_t>
85  data_->error_data_received_callback_ += (function);
86  return xtd::diagnostics::data_received_event_handler::operator+=(function);
87  }
88 
93 
97  xtd::diagnostics::data_received_event_handler& operator-=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
98 
102  template<typename fn_t>
103  xtd::diagnostics::data_received_event_handler& operator -=(fn_t function) noexcept {
104  data_->error_data_received_callback_ -= (function);
105  return data_received_event_handler::operator-=(function);
106  }
108 
109  private:
110  process::data* data_ = nullptr;
111  };
112 
118  class exit_event : protected xtd::event_handler {
119  friend process;
120  void set_data(process::data* data) {data_ = data;}
121  public:
123 
127  bool is_empty() const noexcept;
129 
131 
136  xtd::event_handler& operator+=(const xtd::event_handler& handler) noexcept;
137 
141  xtd::event_handler& operator+=(const typename xtd::event_handler::function_t& function) noexcept;
142 
146  template<typename fn_t>
147  xtd::event_handler& operator+=(fn_t function) noexcept {
148  data_->exit_callback_ += (function);
149  return xtd::event_handler::operator+=(function);
150  }
151 
155  xtd::event_handler& operator-=(const xtd::event_handler& handler) noexcept;
156 
160  xtd::event_handler& operator-=(const typename xtd::event_handler::function_t& function) noexcept;
161 
165  template<typename fn_t>
166  xtd::event_handler& operator -=(fn_t function) noexcept {
167  data_->exit_callback_ -= (function);
168  return xtd::event_handler::operator-=(function);
169  }
171 
172  private:
173  process::data* data_ = nullptr;
174  };
175 
182  friend process;
183  void set_data(process::data* data) {data_ = data;}
184  public:
186 
190  bool is_empty() const noexcept;
192 
194 
200 
204  xtd::diagnostics::data_received_event_handler& operator+=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
205 
209  template<typename fn_t>
211  data_->output_data_received_callback_ += (function);
212  return xtd::diagnostics::data_received_event_handler::operator+=(function);
213  }
214 
219 
223  xtd::diagnostics::data_received_event_handler& operator-=(const typename xtd::diagnostics::data_received_event_handler::function_t& function) noexcept;
224 
228  template<typename fn_t>
229  xtd::diagnostics::data_received_event_handler& operator -=(fn_t function) noexcept {
230  data_->output_data_received_callback_ -= (function);
231  return xtd::diagnostics::data_received_event_handler::operator-=(function);
232  }
234 
235  private:
236  process::data* data_ = nullptr;
237  };
238 
240 
249  process();
251 
253  process(const process&) = default;
254  process& operator=(const process& value);
255  ~process();
257 
259 
276  int32_t base_priority() const;
277 
285  bool enable_raising_events() const;
293  process& enable_raising_events(bool value);
294 
306  int32_t exit_code() const;
307 
312  xtd::date_time exit_time() const;
313 
321  intptr_t handle() const;
322 
330  bool has_exited() const;
331 
339  int32_t id() const;
340 
346  ustring machine_name() const;
347 
363  xtd::diagnostics::process_priority_class priority_class() const;
379  process& priority_class(xtd::diagnostics::process_priority_class value);
380 
386  ustring process_name() const;
387 
496  std::istream& standard_error();
497 
561  std::ostream& standard_input();
562 
686  std::istream& standard_output();
687 
697  const xtd::diagnostics::process_start_info& start_info() const;
717  process& start_info(const xtd::diagnostics::process_start_info& value);
718 
722  xtd::date_time start_time() const;
724 
726 
731  void close();
732 
741  void kill();
742 
749  bool start();
763  static process start(const xtd::diagnostics::process_start_info& start_info);
775  static process start(const ustring& file_name);
787  static process start(const ustring& file_name, const ustring& arguments);
788 
796  process& wait_for_exit();
797 
806  process& wait_for_exit(int32_t milliseconds);
808 
810 
817 
824 
831 
832  protected:
834 
839  virtual void on_exited();
841 
842  private:
843  struct data {
845  std::optional<intptr_t> handle_ = 0;
846  int32_t id_ = 0;
847  ustring machine_name_;
849  std::unique_ptr<std::ostream> standard_input_;
850  std::unique_ptr<std::istream> standard_output_;
851  std::unique_ptr<std::istream> standard_error_;
852  std::thread thread_;
853  xtd::date_time start_time_;
854  xtd::date_time exit_time_;
855  bool enable_raising_events_ = false;
856  std::optional<int32_t> exit_code_;
857  event_handler exit_callback_;
858  data_received_event_handler error_data_received_callback_;
859  data_received_event_handler output_data_received_callback_;
860  std::exception_ptr exception_pointer_;
861  };
862  std::shared_ptr<data> data_ = std::make_shared<data>();
863  friend __init_process_message_box_message__;
864  inline static xtd::delegate<void(const ustring&)> message_box_message_;
865  };
866  }
867 }
Represents a process output data received event.
Definition: process.h:181
process_priority_class
Indicates the priority that the system associates with a process. This value, together with the prior...
Definition: process_priority_class.h:22
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:55
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:829
error_data_received_event error_data_received
Occurs when an application writes to its redirected xtd::diagnostics::process::standard_error stream...
Definition: process.h:816
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
Specifies that the process has no special scheduling needs.
Contains xtd::diagnostics::process_start_info class.
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:46
Represents an instant in time, typically expressed as a date and time of day.
Definition: date_time.h:71
xtd::delegate< void(xtd::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:23
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:30
xtd::diagnostics::data_received_event_handler & operator+=(fn_t function) noexcept
Adds a function to the event.
Definition: process.h:210
Contains xtd::diagnostics::process_priority_class enum class.
Represents a process exit event.
Definition: process.h:118
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:84
exit_event exited
Occurs when a process exits.
Definition: process.h:823
xtd::event_handler & operator+=(fn_t function) noexcept
Adds a function to the event.
Definition: process.h:147
Specifies a set of values that are used when you start a process.
Definition: process_start_info.h:35