summaryrefslogtreecommitdiffstats
path: root/win/C#/CLI/Manager.cs
blob: d6627ba8a5888115338b196a178f6630d098be0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Handbrake.CLI
{
    /// <summary>
    /// Delegate to handle pointers to event subscriptions regarding CLI jobs
    /// </summary>
    /// <param name="Sender">The object which raised the event using this delegate</param>
    /// <param name="Job">The job which caused this delegate to fire</param>
    public delegate void JobStatusHandler(Jobs.Job Job);

    /// <summary>
    /// still workin on this
    /// </summary>
    class Manager
    {
        private static Queue<Process> m_processQueue = new Queue<Process>();

        /// <summary>
        /// Raised upon a job being completed
        /// </summary>
        public static event JobStatusHandler OnJobCompleted;

        /// <summary>
        /// Raised upon a new job starting
        /// </summary>
        public static event JobStatusHandler OnJobStarted;

        /// <summary>
        /// Raised upon any noteable progress during a job
        /// </summary>
        public static event JobStatusHandler OnJobProgress;

        /// <summary>
        /// Used for queueing up a job to be processed later
        /// </summary>
        /// <param name="job">The job to be processed later</param>
        public static void EnqueueJob(Jobs.Job job)
        {
            //TODO: create new Process object from passed 
            m_processQueue.Enqueue(CreateProcess(job));
        }

        /// <summary>
        /// Starts the job queue
        /// </summary>
        public static void StartJobs()
        {
            while (m_processQueue.Count > 0)
            {
                Process proc = m_processQueue.Dequeue();
                proc.Start();
                proc.Close();
            }
        }

        /// <summary>
        /// Creates a new Process object from a Job object
        /// </summary>
        /// <param name="job">The Job object to create a process from</param>
        /// <returns>A Process object based on the requirements of the Job passed</returns>
        private static Process CreateProcess(Jobs.Job job)
        {
            Process hbProc = new Process();
            hbProc.StartInfo.FileName = "hbcli.exe";
            hbProc.StartInfo.Arguments = job.ToString();
            hbProc.StartInfo.RedirectStandardOutput = true;
            hbProc.StartInfo.RedirectStandardError = true;
            hbProc.StartInfo.UseShellExecute = false;
            hbProc.StartInfo.CreateNoWindow = true;

            // Set the process Priority
            switch (Properties.Settings.Default.processPriority)
            {
                case "Realtime":
                    hbProc.PriorityClass = ProcessPriorityClass.RealTime;
                    break;
                case "High":
                    hbProc.PriorityClass = ProcessPriorityClass.High;
                    break;
                case "Above Normal":
                    hbProc.PriorityClass = ProcessPriorityClass.AboveNormal;
                    break;
                case "Normal":
                    hbProc.PriorityClass = ProcessPriorityClass.Normal;
                    break;
                case "Low":
                    hbProc.PriorityClass = ProcessPriorityClass.Idle;
                    break;
                default:
                    hbProc.PriorityClass = ProcessPriorityClass.BelowNormal;
                    break;
            }

            return hbProc;
        }
    }
}