2016-11-14 9 views
0

私はVB.Net 2010を使用しています。データベースの要求をチェックし、要求の中には完了までに数分かかることがあります。要求が処理されている間にキューをキューに入れたくないので、新しいスレッドを動的に作成して、新しい要求を同時に実行して、同時に実行できるようにします。スレッド、スレッドプール、バックグラウンドワーカーなどを使用するかどうかはわかりませんでした。どんな提案やサンプルコードも素晴らしいでしょう。私は簡単な例で作業することができます。これはこれまで私が持っていたものすべてです。VB.Netで動的にスレッドを作成するのに役立つ

ありがとうございます。

Imports System.Threading 
Module Module1 
Private trd As Thread 
Sub Main() 
    'START THREAD 
    trd = New Thread(AddressOf command_loop) 
    trd.IsBackground = True 
    trd.Start() 

    Console.ReadLine() 
End Sub 
Function command_loop() 
     'CREATE EXPORT FILE 
    Try 
     Dim f As FileInfo 
     Dim export As System.IO.StreamWriter 
     Dim arr_comment 

     'DELETE OLD EXPORT FILE 
     If File.Exists(export_path) Then 
      File.Delete(export_path) 
     End If 

     'VERIFY OLD EXPORT FILE WAS DELETED 
     If File.Exists(export_path) Then 
      writeToLog("ERROR: Old export file for " & username & " was not able to be deleted") 
     End If 

     'GET USER FOLDER CRC 
     Dim di As New DirectoryInfo(user_folder) 
     Dim fiArr As FileInfo() = di.GetFiles() 
     export = My.Computer.FileSystem.OpenTextFileWriter(export_path, True) 
     export.WriteLine("filepath, size, esize, date_mod_timestamp, timestamp, remote_filename, filestatus") 

     For Each f In fiArr 
      Try 
       Using zip As ZipFile = ZipFile.Read(user_folder & "\" & f.Name) 
        For Each e As ZipEntry In zip 
         If (zip.Comment IsNot Nothing) AndAlso (zip.Comment <> "") Then 
          arr_comment = Split(zip.Comment, ":") 
         End If 

         If arr_comment.length <> 3 Then 
          'FILE HAS AN INVALID COMMENT, SO DELETE IT. 
          zip.Dispose() 

          Try 
           writeToLog("Invalid zip file comment, deleting file " & user_folder & "\" & f.Name) 
           File.Delete(user_folder & "\" & f.Name) 
           writeToLog("Zip file successfully deleted") 
          Catch exx As Exception 
           writeToLog("Unable to delete invalid zip file") 
           writeToLog(exx.Message) 
          End Try 
          Continue For 
         End If 

         'CORRECT FILE PATH 
         Dim filepath = Replace(e.FileName, "/", "\\") 

         export.WriteLine(Chr(34) & filepath & Chr(34) & "," & e.UncompressedSize & "," & e.CompressedSize & "," & arr_comment(1) & "," & arr_comment(2) & "," & f.Name & "," & arr_comment(0)) 
         Exit For 
        Next 
       End Using 
      Catch ex As IOException 
       Console.WriteLine(ex.Message) 
      Catch ex As ZipException 
       Console.WriteLine(ex.Message) 
       Try 
        writeToLog("Invalid zip file, deleting file " & user_folder & "\" & f.Name) 
        File.Delete(user_folder & "\" & f.Name) 
        writeToLog("Zip file successfully delete") 
       Catch e As Exception 
        writeToLog("Unable to delete invalid zip file") 
        writeToLog(e.Message) 
       End Try 
      End Try 
     Next f 
     export.Close() 
    Catch ex As Exception 
     Console.WriteLine(ex.Message) 
    End Try 

End Function 
End Module 
+1

この質問は広すぎます。あなたはあなたがやっていることを絞って、特定の質問をする必要があります。たとえば、 ''DOM SOMETHING'メソッドのコードは、正しい解決策に大きく影響する可能性があります。 – Enigmativity

+0

上記のすべてのものを 'Task.Run'で使うべきです。しかし実際には最終目標に依存しています –

+0

スレッドにロードする必要のあるコードは次のとおりです。 – rerat

答えて

1

私はこのタイプのタスクにスレッドプールを使用します。新しいスレッドを生成するよりもはるかに軽量で、バックグラウンドワーカーはワーカー1つにつき1つのスレッドしかサポートしません。

Private Class MyArgs 
    'add things for cancellation/storing results in here 
End Class 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim args As New MyArgs() 
    Threading.ThreadPool.QueueUserWorkItem(AddressOf DoThisSingleThing, New MyArgs) 
End Sub 

Private Sub DoThisSingleThing(state As Object) 
    Dim args = CType(state, MyArgs) 

    While Not args.Cancel 

    End While 

    args.Result = ... 

    'If you need to update the UI afterward then invoke onto its thread. 
    Me.BeginInvoke(Sub() OnComplete(args)) 
End Sub 


Private Sub OnComplete(args As MyArgs) 

End Sub 
+0

これは私のために働くようです。関数が完了すると、スレッドはそれ自身で終了するのですか、それとも終了する必要がありますか? – rerat

+0

スレッドはスレッドプールの一部であるため、プールに戻って次の作業で使用されます。スレッドプールスレッドを終了または変更しないでください。 – FloatingKiwi

関連する問題