title: Running ImageMagick from C# with scheduled tasks
body: <p>I have a program that converts PDF-s to images. While running the program manually, everything works. But whenever I try to run the program as a scheduled task, ImageMagick or cmd (not sure) refuses to start and it jumps the step, causing the program to stop.</p>
  
  <p>I have tried running it under different users (both scheduled task and starting the process as a user with privileges), but nothing seems to work and I am left at not understanding or knowing where to start looking next.</p>
  
  <p>My best guess is that is has something to do with user security, and not being allowed to to run imagemagick from a process/cmd in C#.</p>
  
  <p>Code:</p>
  
  <pre><code>string args = string.Format("/k convert -density 200 \"{0}\" -quality 40 \"{1}\\{2}\"", file.FullName, imageFolder, file.Name.Replace("pdf", "png"));
  using (Process proc = new Process {
      StartInfo = {
          Arguments = args,
          FileName = @"CMD.exe",
          UseShellExecute = false,
          CreateNoWindow = true,
          RedirectStandardOutput = true,
          RedirectStandardError = true
      }
  }) {
      proc.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
      proc.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
      proc.Start();
      proc.WaitForExit(5000);
      proc.Kill();
  }
  static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
      using (StreamWriter sw = new StreamWriter(Program.BaseFolder + "\\log.txt")) {
          sw.WriteLine(e.Data);
      }
  }
  static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
      using (StreamWriter sw = new StreamWriter(Program.BaseFolder + "\\log.txt")) {
          sw.WriteLine(e.Data);
      }
  }
  </code></pre>