2017-01-14 40 views
-4

我寫了一個帶有6個線程的程序,在我的CORE I7中運行良好,但是當我在CELERON中運行它時不起作用。看來,線程停止工作。如何在VB.NET 2013中改進我的程序以獲得更好的性能?

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
    Timer1.Enabled = True 
    CheckForIllegalCrossThreadCalls = False 

    trd = New Thread(AddressOf ThreadTask) 
    barreras_trd = New Thread(AddressOf barreras) 
    sensores_trd = New Thread(AddressOf sensores) 
    convierte_trd = New Thread(AddressOf convierte) 
    emite_tiquet_trd = New Thread(AddressOf tiquet) 
    actualiza_trd = New Thread(AddressOf actualiza_grilla) 

    trd.IsBackground = True 
    barreras_trd.IsBackground = True 
    sensores_trd.IsBackground = True 
    convierte_trd.IsBackground = True 
    emite_tiquet_trd.IsBackground = True 
    actualiza_trd.IsBackground = True 

    trd.Start() 
    barreras_trd.Start() 
    sensores_trd.Start() 
    convierte_trd.Start() 
    emite_tiquet_trd.Start() 
    actualiza_trd.Start() 

    Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1 

    'Select the last row. 
    Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True 
End Sub 
+4

'CheckF orIllegalCrossThreadCalls = False'是一個非常糟糕的做法,研究如何在vb.net中使用線程以及如何從另一個線程更新GUI。您可以從這裏開始:https://msdn.microsoft.com/en-us/library/aa289496(v=vs.71).aspx – jonathana

+0

請勿爲每個威脅使用單獨的線程。只需使用一個線程進行威脅,然後循環遍歷每個線程並以某種方式進行更新。沒有看到你的代碼對我們來說是有幫助的! –

+0

您可能有同步死鎖問題,但不在您發佈的代碼部分。不用這種方式來回答。 –

回答

0

,因爲我們已經不知道你的螺紋的方法做,我們不能提供多少,但你至少可以簡化爲這些類型之一:

選項1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
    Timer1.Enabled = True 

    ' Runs all in parallel (give or take the scheduler and duration) 
    Task.Run(Sub() ThreadTask()) 
    Task.Run(Sub() barreras()) 
    Task.Run(Sub() sensores()) 
    Task.Run(Sub() convierte()) 
    Task.Run(Sub() tiquet()) 
    Task.Run(Sub() actualiza_grilla()) 

    Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1 

    'Select the last row. 
    Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True 
End Sub 

選項2

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
    Timer1.Enabled = True 

    Task.Run(Sub() AllThreads()) 

    Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1 

    'Select the last row. 
    Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True 
End Sub 

Sub AllThreads() 
    ' Runs each in series 
    ThreadTask() 
    barreras() 
    sensores() 
    convierte() 
    tiquet() 
    actualiza_grilla() 
End Sub 
+0

非常感謝您的回答。它指導着我必須去的地方。解決方案是TPL。 (任務並行庫) – ElCalefactordeManchuri

相關問題