@@ -17,10 +17,10 @@ namespace TE.FileWatcher.Configuration.Commands
1717 public class Command : RunnableBase
1818 {
1919 // The process that will run the command
20- private Process _process ;
20+ private Process ? _process ;
2121
2222 // A queue containing the information to start the command process
23- private ConcurrentQueue < ProcessStartInfo > _processInfo ;
23+ private ConcurrentQueue < ProcessStartInfo > ? _processInfo ;
2424
2525 // Flag indicating that a process is running
2626 private bool _isProcessRunning = false ;
@@ -29,13 +29,13 @@ public class Command : RunnableBase
2929 /// Gets or sets the arguments associated with the file to execute.
3030 /// </summary>
3131 [ XmlElement ( "arguments" ) ]
32- public string Arguments { get ; set ; }
32+ public string ? Arguments { get ; set ; }
3333
3434 /// <summary>
3535 /// Gets or sets the full path to the file to executed.
3636 /// </summary>
3737 [ XmlElement ( "path" ) ]
38- public string Path { get ; set ; }
38+ public string ? Path { get ; set ; }
3939
4040 /// <summary>
4141 /// Gets or sets the triggers of the action.
@@ -72,8 +72,14 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
7272 return ;
7373 }
7474
75- string commandPath = GetCommand ( watchPath , fullPath ) ;
76- string arguments = GetArguments ( watchPath , fullPath ) ;
75+ string ? commandPath = GetCommand ( watchPath , fullPath ) ;
76+ string ? arguments = GetArguments ( watchPath , fullPath ) ;
77+
78+ if ( string . IsNullOrWhiteSpace ( commandPath ) )
79+ {
80+ Logger . WriteLine ( $ "The command was not provided. Command was not run.") ;
81+ return ;
82+ }
7783
7884 if ( ! File . Exists ( commandPath ) )
7985 {
@@ -88,9 +94,16 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
8894 _processInfo = new ConcurrentQueue < ProcessStartInfo > ( ) ;
8995 }
9096
91- ProcessStartInfo startInfo = new ProcessStartInfo ( ) ;
92- startInfo . FileName = commandPath ;
93- startInfo . Arguments = arguments ;
97+ ProcessStartInfo startInfo = new ( )
98+ {
99+ FileName = commandPath
100+ } ;
101+
102+ if ( arguments != null )
103+ {
104+ startInfo . Arguments = arguments ;
105+ }
106+
94107 _processInfo . Enqueue ( startInfo ) ;
95108
96109 // Execute the next process in the queue
@@ -119,12 +132,14 @@ private void Execute()
119132
120133 try
121134 {
122- if ( _processInfo . TryDequeue ( out ProcessStartInfo startInfo ) )
135+ if ( _processInfo . TryDequeue ( out ProcessStartInfo ? startInfo ) )
123136 {
124137 if ( File . Exists ( startInfo . FileName ) )
125138 {
126- _process = new Process ( ) ;
127- _process . StartInfo = startInfo ;
139+ _process = new Process
140+ {
141+ StartInfo = startInfo
142+ } ;
128143 _process . StartInfo . CreateNoWindow = true ;
129144 _process . StartInfo . UseShellExecute = false ;
130145 _process . EnableRaisingEvents = true ;
@@ -142,7 +157,14 @@ private void Execute()
142157 }
143158 catch ( Exception ex )
144159 {
145- Logger . WriteLine ( $ "Could not run the command '{ _process . StartInfo . FileName } { _process . StartInfo . Arguments } '. Reason: { ex . Message } ") ;
160+ if ( _process != null )
161+ {
162+ Logger . WriteLine ( $ "Could not run the command '{ _process . StartInfo . FileName } { _process . StartInfo . Arguments } '. Reason: { ex . Message } ") ;
163+ }
164+ else
165+ {
166+ Logger . WriteLine ( $ "Could not run the command. Reason: { ex . Message } ") ;
167+ }
146168 }
147169 }
148170
@@ -159,7 +181,7 @@ private void Execute()
159181 /// <returns>
160182 /// The command path string value.
161183 /// </returns>
162- private string GetArguments ( string watchPath , string fullPath )
184+ private string ? GetArguments ( string watchPath , string fullPath )
163185 {
164186 if ( string . IsNullOrWhiteSpace ( Arguments ) )
165187 {
@@ -182,7 +204,7 @@ private string GetArguments(string watchPath, string fullPath)
182204 /// <returns>
183205 /// The command path string value.
184206 /// </returns>
185- private string GetCommand ( string watchPath , string fullPath )
207+ private string ? GetCommand ( string watchPath , string fullPath )
186208 {
187209 if ( string . IsNullOrWhiteSpace ( Path ) )
188210 {
@@ -201,7 +223,7 @@ private string GetCommand(string watchPath, string fullPath)
201223 /// <param name="args">
202224 /// The event arguments.
203225 /// </param>
204- private void OnProcessExit ( object sender , EventArgs args )
226+ private void OnProcessExit ( object ? sender , EventArgs args )
205227 {
206228 _isProcessRunning = false ;
207229
0 commit comments