Conversione a c# + threads

This commit is contained in:
Maddo Scientisto 2021-02-25 11:14:44 +01:00
commit d133917283
24 changed files with 2649 additions and 642 deletions

View file

@ -1,9 +1,14 @@
Imports System.IO
Imports System.Collections.Concurrent
Imports System.IO
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Threading
Imports System.Collections.Generic
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
Imports System.Threading.Tasks
Imports CatalogVbLib
Imports MaddoShared
Public Delegate Sub XyThreadAdd(ByVal Info As String)
@ -97,12 +102,20 @@ Public Class MainForm
ComboBox5.Items.Add("Basso")
ComboBox5.SelectedIndex = 2
End Sub
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function AllocConsole() As Boolean
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Application.EnableVisualStyles()
setDefaults()
#If Not DEBUG Then
AllocConsole()
#End If
Console.WriteLine("Programma avviato")
End Sub
Private Sub FixPaths()
@ -160,6 +173,7 @@ Public Class MainForm
End If
End Sub
Private Sub creaCatalogoThread()
Dim timeStart As Date = TimeOfDay
MyPool.StopThreadPool()
@ -878,7 +892,7 @@ Public Class MainForm
Dim ClsCreaImmagine As New ImageCreator(childFile.Name, SourceDir, DestDir, DestDirStart)
Dim ClsCreaImmagine As New ImageCreatorSharp(childFile.Name, SourceDir, DestDir, DestDirStart)
' ClsCreaImmagine.NomeFileChild = childFile.Name
' ClsCreaImmagine.DestDir = DestDir
' ClsCreaImmagine.SourceDir = SourceDir
@ -898,7 +912,7 @@ Public Class MainForm
End Sub
Private Function getNumerazione() As Integer
dim numerazione As Integer
Dim numerazione As Integer
If rdbNumProgressiva.Checked Then
numerazione = FileHelper.numerazione.Progressiva
Else
@ -907,6 +921,16 @@ Public Class MainForm
Return numerazione
End Function
Private Function GetNumerazioneEnum() As NumerazioneType
Dim numerazioneType As NumerazioneType
If rdbNumProgressiva.Checked Then
numerazioneType = NumerazioneType.Progressiva ' FileHelper.numerazione.Progressiva
Else
numerazioneType = NumerazioneType.Files ' FileHelper.numerazione.Files
End If
Return numerazioneType
End Function
Private Sub creaimmaginiWithThreadDict(ByVal SourcePath As String, ByVal DestPath As String)
Dim dirSourceDest As Dictionary(Of FileInfo, DirectoryInfo) = New Dictionary(Of FileInfo, DirectoryInfo)
If chkAggiornaSottodirectory.Checked And chkCreaSottocartelle.Checked Then
@ -922,12 +946,12 @@ Public Class MainForm
Dim pair As KeyValuePair(Of FileInfo, DirectoryInfo)
For Each pair In dirSourceDest
setLabel10Text("File: " & pair.Key.Name)
Dim b As String = (CType(Label18.Text, Integer) + 1).ToString
Dim ClsCreaImmagine As New ImageCreator(pair.Key, pair.Value)
Dim ClsCreaImmagine As New ImageCreatorSharp(pair.Key, pair.Value)
ContaImmaginiThread += 1
MyPool.InsertWorkItem(pair.Key.Name, New XyThreadAdd(AddressOf ClsCreaImmagine.CreaImmagineThread), New Object(0) {pair.Key.Name}, True)
Next
@ -1312,7 +1336,157 @@ Public Class MainForm
CheckBox18.Checked = False
End Sub
Private Sub Label27_Click(sender As Object, e As EventArgs) Handles Label27.Click
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCreaCatalogoAsync.Click
lockUI()
'Dim timeStart As Date
'Dim timeStop As Date
'timeStart = TimeOfDay
FixPaths()
Label10.Text = "Elaborazione in corso..."
lblFotoTotaliNum.Text = "0"
Label18.Text = "0"
Label43.Text = "-s"
setPicSettings(txtSorgente.Text, txtDestinazione.Text)
ProgressBar1.Minimum = 0
ProgressBar1.Step = 1
ProgressBar1.Value = 0
'Await CreaCatalogoParallel()
Dim imgStf As ImageCreationStuff = New ImageCreationStuff()
Dim imageCreationOptions As ImageCreationStuff.Options = New ImageCreationStuff.Options()
With imageCreationOptions
.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked
.CreaSottocartelle = chkCreaSottocartelle.Checked
.FilePerCartella = CInt(txtFilePerCartella.Text)
.SuffissoCartelle = txtSuffissoCartelle.Text
.CifreContatore = CInt(txtCifreContatore.Text)
.NumerazioneType = GetNumerazioneEnum()
.SourcePath = txtSorgente.Text
.DestinationPath = txtDestinazione.Text
.MaxThreads = CInt(TextBox7.Text)
.ChunksSize = CInt(TextBox8.Text)
.LinearExecution = rdbVecchioMetodo.Checked
End With
Dim time As String = Await imgStf.CreaCatalogoParallel(imageCreationOptions)
Label43.Text = time
Label10.Text = "Finito"
unlockUI()
End Sub
Private Sub UpdateCounter(text As String)
Label10.Invoke(Sub()
Label10.Text = text
End Sub)
End Sub
Private Async Function CreaCatalogoParallel() As Task
Dim timeStart As Date = TimeOfDay
ContaImmaginiThread = 0
setLabel10Text("Elaborazione in corso...")
Dim imgStf As ImageCreationStuff = New ImageCreationStuff()
Dim imageCreationOptions As ImageCreationStuff.Options = New ImageCreationStuff.Options()
With imageCreationOptions
.AggiornaSottodirectory = chkAggiornaSottodirectory.Checked
.CreaSottocartelle = chkCreaSottocartelle.Checked
.FilePerCartella = CInt(txtFilePerCartella.Text)
.SuffissoCartelle = txtSuffissoCartelle.Text
.CifreContatore = CInt(txtCifreContatore.Text)
.NumerazioneType = GetNumerazioneEnum()
.SourcePath = txtSorgente.Text
.DestinationPath = txtDestinazione.Text
End With
Await imgStf.CreaImmaginiParallel(imageCreationOptions)
'Await CreaImmaginiParallel(txtSorgente.Text, txtDestinazione.Text)
setLabel10Text("Finito")
Dim timeStop As Date = TimeOfDay
setLabel43Text(CalcTime(timeStart, timeStop, ContaImmaginiThread))
End Function
'Private Async Function CreaImmaginiParallel(ByVal SourcePath As String, ByVal DestPath As String) As Task
' Dim dataToProcess As List(Of FileData) = New List(Of FileData)
' 'Dim dirSourceDest As Dictionary(Of FileInfo, DirectoryInfo) = New Dictionary(Of FileInfo, DirectoryInfo)
' If chkAggiornaSottodirectory.Checked And chkCreaSottocartelle.Checked Then
' Dim helperSharp As New FileHelperSharp()
' 'Dim helper As New FileHelper(CInt(txtFilePerCartella.Text), txtSuffissoCartelle.Text, CInt(txtCifreContatore.Text), getNumerazione())
' 'getfilesrecursive
' Dim fileHelperOptions As FileHelperOptions = New FileHelperOptions()
' fileHelperOptions.FilesPerFolder = CInt(txtFilePerCartella.Text)
' fileHelperOptions.Suffix = txtSuffissoCartelle.Text
' fileHelperOptions.CounterSize = CInt(txtCifreContatore.Text)
' fileHelperOptions.NumerationType = GetNumerazioneEnum()
' dataToProcess = helperSharp.GetFilesRecursive(New DirectoryInfo(SourcePath), New DirectoryInfo(DestPath), "*.jpg", fileHelperOptions)
' 'dataToProcess = helper.GetFilesRecursiveParallel(New DirectoryInfo(SourcePath), New DirectoryInfo(DestPath), "*.jpg")
' ElseIf chkAggiornaSottodirectory.Checked And Not chkCreaSottocartelle.Checked Then
' ' TODO manca tutto?!?!?!?
' End If
' Dim scheduler As TaskScheduler = New ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, Environment.ProcessorCount * 2).ConcurrentScheduler
' Dim test As IEnumerable(Of Task) = From d In dataToProcess Select Task.Factory.StartNew(Sub()
' 'setLabel10Text("File: " & p.File.Name)
' Dim b As String = (CType(Label18.Text, Integer) + 1).ToString
' Dim clsCreaImmagine As New ImageCreator(d.File, d.Directory)
' clsCreaImmagine.CreaImmagineThread(d.File.Name)
' ContaImmaginiThread += 1
' UpdateCounter(ContaImmaginiThread & " " & d.File.Name)
' End Sub, CancellationToken.None, TaskCreationOptions.LongRunning, scheduler) 'TODO Cancellation Token
' 'ThreadingHelper.StartAndWaitAllThrottled(test, CType(TextBox7.Text, Integer))
' Await Task.WhenAll(test)
' '= getDirsDict(SourcePath, DestPath)
' 'Parallel.ForEach(dataToProcess,
' ' Sub(p, state)
' ' 'setLabel10Text("File: " & p.File.Name)
' ' Dim b As String = (CType(Label18.Text, Integer) + 1).ToString
' ' Dim clsCreaImmagine As New ImageCreator(p.File, p.Directory)
' ' clsCreaImmagine.CreaImmagineThread(p.File.Name)
' ' ContaImmaginiThread += 1
' ' UpdateCounter(ContaImmaginiThread & " " & p.File.Name)
' ' 'MyPool.InsertWorkItem(p.File.Name, New XyThreadAdd(AddressOf ClsCreaImmagine.CreaImmagineThread), New Object(0) {p.File.Name}, True)
' ' ' TODO: BREAK ON STOP state.stop()
' ' End Sub)
' 'Dim pair As KeyValuePair(Of FileInfo, DirectoryInfo)
' 'For Each pair In dirSourceDest
' ' setLabel10Text("File: " & pair.Key.Name)
' ' Dim b As String = (CType(Label18.Text, Integer) + 1).ToString
' ' Dim ClsCreaImmagine As New ImageCreator(pair.Key, pair.Value)
' ' ContaImmaginiThread += 1
' ' MyPool.InsertWorkItem(pair.Key.Name, New XyThreadAdd(AddressOf ClsCreaImmagine.CreaImmagineThread), New Object(0) {pair.Key.Name}, True)
' 'Next
'End Function
End Class
Public Class PicInfo