summaryrefslogtreecommitdiffstats
path: root/win/Handbrake/frmQueue.vb
blob: bc1090343d2a2758afab661074576baa9982f42e (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
Imports System.IO
Imports System
Imports System.Diagnostics
Imports System.Threading
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class frmQueue

    
    Delegate Sub SetTextCallback(ByVal [text] As Integer)

    '#
    '# Buttons
    '#
    Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
        list_queue.Items.Remove(list_queue.SelectedItem)
    End Sub

    Private Sub btn_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Close.Click
        Me.Hide()
    End Sub

    '# STAGE 1
    Private Sub btn_q_encoder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_q_encoder.Click

        Dim ApplicationPath As String = Application.StartupPath ' The applications start parth
        Dim encodeItems As Integer = list_queue.Items.Count ' Amount of items to encode

        encodeItems = encodeItems - 1

        'Start the encode process
        Try
            Shell("""" + ApplicationPath + "\hbcli.exe""" + list_queue.Items.Item(encodeItems))
        Catch ex As Exception
            MessageBox.Show("ERROR: No items left on the queue. Code: 01")
        End Try

        ' Lets start the process monitor
        hbcliMonitor = New ProcessMonitor()
        Dim t = New Thread(AddressOf hbcliMonitor.tmrProcCheck)
        t.Start()

        Try
            ' When this task is finished, remove it from the queue
            list_queue.Items.RemoveAt(encodeItems)
        Catch ex As Exception
            ' quietly ignore the error should the user try to encode with nothing on the queue by mistake.
        End Try
        
    End Sub



    '# STAGE 2
    Sub TheadCompletedMonitor(ByVal isRunning As Integer) Handles hbcliMonitor.ThreadComplete

        Dim ApplicationPath As String = Application.StartupPath ' The applications start parth
        Dim encodeItems As Integer = list_queue.Items.Count ' Amount of items to encode

        encodeItems = encodeItems - 1


        If encodeItems = -1 Then
            MessageBox.Show("Status: Queue completed!")
        Else
            'Start the encode process
            Try
                Shell("""" + ApplicationPath + "\hbcli.exe""" + list_queue.Items.Item(encodeItems))
            Catch ex As Exception
                MessageBox.Show("Error Code: q1, Please report this error.")
            End Try

            ' Lets start the process monitor
            hbcliMonitor = New ProcessMonitor()
            Dim t = New Thread(AddressOf hbcliMonitor.tmrProcCheck)
            t.Start()

            ' When this task is finished, remove it from the queue
            Me.SetText(encodeItems)
            'MsgBox("1 Enocde Process Finished: ", isRunning)
        End If

    End Sub


    '#
    '# Trying to safely alter stuff on the worker thread here.
    '#
    Private Sub SetText(ByVal [text] As Integer)
        If Me.list_queue.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            Me.list_queue.Items.RemoveAt([text])
        End If
    End Sub

    '#
    '# Process Monitoring Stuff Here
    '#

    Dim WithEvents hbcliMonitor As ProcessMonitor

    Public Class ProcessMonitor
        Public isRunning As Integer = 1

        Public Event ThreadComplete(ByVal isRunning As Integer)

        Public Sub tmrProcCheck()
            Dim isRunning As Integer
            Dim process2 As Process = New Process
            Dim running As Boolean = True
            Dim hbProcess As Process() = Process.GetProcesses()

            While running
                Thread.Sleep(1000)
                hbProcess = Process.GetProcesses()
                running = False
                Dim processArr2 As Process() = hbProcess
                Dim i As Integer = 0
                While i < CInt(processArr2.Length)
                    Dim process1 As Process = processArr2(i)
                    If process1.ProcessName.Equals("hbcli") Then
                        running = True
                    End If
                    i = i + 1
                End While
            End While
            isRunning = 0
            RaiseEvent ThreadComplete(isRunning)
        End Sub
    End Class


End Class