PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Tasks/Cache.vb

#
Visual Basic | 267 lines | 129 code | 55 blank | 83 comment | 0 complexity | 0367eeb5dc579c75af8ce240626e4e86 MD5 | raw file
  1Imports System.Threading
  2Imports System.ComponentModel
  3
  4Public Class Common
  5    Public Shared Tasks As New TaskCache
  6End Class
  7
  8Public Class TaskCache
  9    Implements IList(Of ITask), System.ComponentModel.INotifyPropertyChanged, IListSource
 10
 11    Private Tasks As New List(Of ITask)
 12
 13    Public MainThread As New Thread(New ThreadStart(AddressOf RunTasks))
 14    Public Threads As New List(Of Threading.Thread)
 15    Public Property MaxThreads As Integer = 16
 16
 17    Public Property Done As Boolean
 18
 19    Public Sub StartTaskEngine()
 20        MainThread.Start()
 21    End Sub
 22
 23    Private Sub RunTasks()
 24        Do Until Done
 25            Try 'This Try block lets me be really lazy and ignore the fact that if a new task is created it breaks the enumeration and starts the whole process
 26                For Each Task In Tasks
 27                    If Task.State = TaskState.NotStarted Then
 28                        If Not Threads.Count > MaxThreads Then
 29                            Dim NewThread As Thread = New Thread(New ThreadStart(AddressOf Task.Run))
 30
 31                            Threads.Add(NewThread)
 32
 33                            NewThread.Start()
 34                            Me.NotifyPropertyChanged("UnrunTaskCount")
 35                            Me.NotifyPropertyChanged("UserWaitTaskCount")
 36                        End If
 37                    End If
 38                Next
 39
 40                Dim I As Integer = 0
 41                Do
 42                    If I > Threads.Count - 1 Then Exit Do
 43
 44                    If Threads(I).ThreadState = ThreadState.Stopped Then
 45                        Threads.Remove(Threads(I))
 46                        Continue Do
 47                        Me.NotifyPropertyChanged("CompletedTaskCount")
 48                    End If
 49                    I += 1
 50                Loop
 51            Catch
 52
 53            Finally
 54                Thread.Sleep(1)
 55            End Try
 56
 57        Loop
 58    End Sub
 59
 60    Public Sub Add(item As ITask) Implements System.Collections.Generic.ICollection(Of ITask).Add
 61        Tasks.Add(item)
 62    End Sub
 63
 64    Public Sub Clear() Implements System.Collections.Generic.ICollection(Of ITask).Clear
 65        Tasks.Clear()
 66        Threads.Clear()
 67    End Sub
 68
 69    Public Function Contains(item As ITask) As Boolean Implements System.Collections.Generic.ICollection(Of ITask).Contains
 70        Return Tasks.Contains(item)
 71    End Function
 72
 73    Public Sub CopyTo(array() As ITask, arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of ITask).CopyTo
 74        Tasks.CopyTo(array)
 75    End Sub
 76
 77    Public ReadOnly Property Count As Integer Implements System.Collections.Generic.ICollection(Of ITask).Count
 78        Get
 79            Return Tasks.Count
 80        End Get
 81    End Property
 82
 83    Public ReadOnly Property IsReadOnly As Boolean Implements System.Collections.Generic.ICollection(Of ITask).IsReadOnly
 84        Get
 85            Return False
 86        End Get
 87    End Property
 88
 89    Public Function Remove(item As ITask) As Boolean Implements System.Collections.Generic.ICollection(Of ITask).Remove
 90        Return Tasks.Remove(item)
 91    End Function
 92
 93    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of ITask) Implements System.Collections.Generic.IEnumerable(Of ITask).GetEnumerator
 94        Return Tasks.GetEnumerator
 95    End Function
 96
 97    Public Function IndexOf(item As ITask) As Integer Implements System.Collections.Generic.IList(Of ITask).IndexOf
 98        Return Tasks.IndexOf(item)
 99    End Function
100
101    Public Sub Insert(index As Integer, item As ITask) Implements System.Collections.Generic.IList(Of ITask).Insert
102        Tasks.Insert(index, item)
103    End Sub
104
105    Default Public Property Item(index As Integer) As ITask Implements System.Collections.Generic.IList(Of ITask).Item
106        Get
107            Return Tasks.Item(index)
108        End Get
109        Set(value As ITask)
110            Tasks.Item(index) = value
111        End Set
112    End Property
113
114    Public Sub RemoveAt(index As Integer) Implements System.Collections.Generic.IList(Of ITask).RemoveAt
115        Tasks.RemoveAt(index)
116    End Sub
117
118    Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
119        Return Me.GetEnumerator
120    End Function
121
122    Public ReadOnly Property UnrunTaskCount
123        Get
124            Dim Count = (From Task As ITask In Tasks Where Task.State = TaskState.NotStarted Or Task.State = TaskState.WaitingForDependancies).Count
125            Return Count
126        End Get
127    End Property
128
129    Public ReadOnly Property UserWaitTaskCount
130        Get
131            Dim Count = (From Task As ITask In Tasks Where Task.State = TaskState.WaitingForUserInput).Count
132            Return Count
133        End Get
134    End Property
135
136    Public ReadOnly Property CompletedTaskCount
137        Get
138            Dim Count = (From Task As ITask In Tasks Where Task.State = TaskState.Completed Or Task.State = TaskState.CriticalFault Or Task.State = TaskState.Fault).Count
139            Return Count
140        End Get
141    End Property
142
143    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
144    Private Sub NotifyPropertyChanged(ByVal info As String)
145        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
146    End Sub
147
148    Public ReadOnly Property ContainsListCollection As Boolean Implements System.ComponentModel.IListSource.ContainsListCollection
149        Get
150            If Me.Count > 0 Then
151                Return True
152            Else
153                Return False
154            End If
155        End Get
156    End Property
157
158    Public Function GetList() As System.Collections.IList Implements System.ComponentModel.IListSource.GetList
159        Return Me.Tasks
160    End Function
161End Class
162
163'Private Sub cmdTasks_Refresh_Click(sender As System.Object, e As System.EventArgs) Handles cmdTasks_Refresh.Click
164'    lstTasks.Items.Clear()
165'    tv_MissingArtDownload(tv_ShowSelectedCurrently)
166
167'    For Each Item As ITask In TaskCache.Tasks
168'        lstTasks.Items.Add(Item)
169'    Next
170'End Sub
171
172'Private Sub lstTasks_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstTasks.SelectedIndexChanged
173'    If lstTasks.SelectedItem Is Nothing Then Exit Sub
174
175'    Dim SelectedTask As ITask
176
177'    SelectedTask = lstTasks.SelectedItem
178
179'    Select Case SelectedTask.State
180'        Case TaskState.Completed
181'            lblTask_State.Text = "Completed"
182'        Case TaskState.BackgroundWorkComplete
183'            lblTask_State.Text = "Background Completed"
184'        Case TaskState.CriticalFault
185'            lblTask_State.Text = "Critial Fault"
186'        Case TaskState.Fault
187'            lblTask_State.Text = "Fault"
188'        Case TaskState.Halted
189'            lblTask_State.Text = "Halted"
190'        Case TaskState.NotStarted
191'            lblTask_State.Text = "Not Started"
192'        Case TaskState.WaitingForUserInput
193'            lblTask_State.Text = "Waiting For Input"
194'        Case TaskState.Running
195'            lblTask_State.Text = "Running"
196'    End Select
197
198'    lblTask_Attempts.Text = SelectedTask.Attempts
199
200'    cmbTasks_Arguments.Items.Clear()
201'    cmbTasks_Arguments.Text = ""
202'    For Each Item In SelectedTask.Arguments
203'        cmbTasks_Arguments.Items.Add(Item)
204
205'    Next
206'    If cmbTasks_Arguments.Items.Count > 0 Then cmbTasks_Arguments.SelectedIndex = 0
207
208'    lstTasks_Dependancies.Items.Clear()
209'    For Each Item In SelectedTask.Dependancies
210'        lstTasks_Dependancies.Items.Add(Item)
211'    Next
212
213'    lstTasks_Messages.Items.Clear()
214'    For Each Item In SelectedTask.Messages
215'        lstTasks_Messages.Items.Add(Item)
216'    Next
217'End Sub
218
219'Private Sub lstTasks_Messages_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstTasks_Messages.SelectedIndexChanged
220'    If lstTasks_Messages.SelectedItem Is Nothing Then Exit Sub
221
222'    txtTasks_SelectedMessage.Text = lstTasks_Messages.SelectedItem
223'End Sub
224
225'Private Sub cmbTasks_Arguments_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbTasks_Arguments.SelectedIndexChanged
226'    If cmbTasks_Arguments.SelectedItem Is Nothing Then Exit Sub
227
228'    pnlTasks_Arguments.Controls.Clear()
229
230'    Dim Item As KeyValuePair(Of String, Object) = cmbTasks_Arguments.SelectedItem
231'    If TypeOf Item.Value Is String Then
232'        pnlTasks_Arguments.Controls.Add(New TextBox() With {.Text = Item.Value, .Dock = DockStyle.Fill, .Multiline = True, .ScrollBars = ScrollBars.Both})
233'    ElseIf TypeOf Item.Value Is TvShow Then
234'        Dim TempShow As TvShow = Item.Value
235'        pnlTasks_Arguments.Controls.Add(New TextBox() With {.Text = TempShow.Id.Value & " - " & TempShow.Title.Value, .Dock = DockStyle.Fill, .ScrollBars = ScrollBars.Both})
236'    ElseIf TypeOf Item.Value Is TvEpisode Then
237'        Dim TempEpisode As TvEpisode = Item.Value
238'        pnlTasks_Arguments.Controls.Add(New TextBox() With {.Text = TempEpisode.Id.Value & " - " & TempEpisode.Title.Value, .Dock = DockStyle.Fill, .ScrollBars = ScrollBars.Both})
239'    ElseIf TypeOf Item.Value Is Image Then
240'        pnlTasks_Arguments.Controls.Add(New PictureBox() With {.Image = Item.Value, .Dock = DockStyle.Fill})
241'    End If
242'End Sub
243
244
245'Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
246'    Try
247'        For Each Task In TaskCache.Tasks
248'            If Task.State = TaskState.BackgroundWorkComplete Then
249'                Task.FinishWork()
250'            End If
251'            Windows.Forms.Application.DoEvents()
252'        Next
253'    Catch
254
255'    End Try
256'End Sub
257
258'Private Sub ContextMenuStrip1_Opening(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
259
260'End Sub
261
262'Private Sub TaskListUpdater_Tick(sender As System.Object, e As System.EventArgs) Handles TaskListUpdater.Tick
263'    'cmdTasks_Refresh_Click(Nothing, Nothing)
264'    TaskListUpdater.Enabled = False
265'    lstTasks.ResetText()
266
267'End Sub