2016-11-14 47 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

這個問題太廣泛了。你需要縮小你正在做的事情並提出具體的問題。例如,「DO SOMETHING」方法中的代碼可以極大地影響正確的解決方案。 – Enigmativity

+0

你應該使用上面的所有那些+'Task.Run'。但它的確取決於最終目標 –

+0

以下是需要加載到線程中的代碼。 – rerat

回答

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