English 中文(简体)
• 在多端口,无重叠 In VB.NET 2
原标题:Running Different Processes In Multiple Threads Without Overlapping In VB.NET 2

I have a sub-procedure which I want to run a different process, depending on what is currently running. I thought the easiest way to do this was by using an ArrayList of each of the campaign details & adding an Inuse field to check to see if the Inuse field is set to 0 or 1. The problem that I have is that when running the process it is all happening at once & the integer hasn t been changed before the next thread kicks in so my threads are running the same campaigns.

我试图通过增加一个路口来避免这一问题。 睡觉(100)推迟了从胎面开始的胎面,但这完全造成了同样的问题。

这方面的一个例子是我试图做的事情:

    Imports System.Threading

Public Class Form1

    Private Campaigns As New ArrayList
    Private ProcessRunning As Boolean = False
    Friend StopProcess As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i = 0 To Campaigns.Count - 1
            Dim objNewThread As New Thread(AddressOf RunProcess)
            objNewThread.IsBackground = True
            objNewThread.Start()
        Next
    End Sub

    Private Sub UpdateCells(ByVal CampID As Integer, ByVal Column As String, ByVal newText As String)
        Dim CellItemNum As Integer
        If Column = "Status" Then CellItemNum = 4
        DataGridView2.Rows(CampID).Cells.Item(CellItemNum).Value = newText
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 10
            Campaigns.Add({Campaigns.Count(), "Campaign " & i, "Keywords " & i, "Link " & i, 5, True, 0, 0})
        Next
        DataGridView2.Rows.Clear()
        For Each Campaign In Campaigns
            DataGridView2.Rows.Add(New String() {Campaign(1), Campaign(2), Campaign(3), Campaign(6), ""})
        Next
    End Sub

    Private Sub RunProcess()
          Set Variables
        Dim CampID As Integer
        Dim CampName As String
        Dim Keywords As String
        Dim Link As String
        Dim CheckEvery As Integer
        Dim OkToUse As Boolean
        Dim Sent As Integer
          Find A Free Campaign
        For i As Integer = 0 To Campaigns.Count - 1
              Check If Inuse
            If Campaigns(i)(7) = 1 Then Continue For Else Campaigns(i)(7) = 1   This Line Sets Campaign To Inuse
              Most of the time only campaign(0) and campaign(1) are selected & multiple threads are running them instead of choosing unique campaigns
              Set Campaign Details
            CampID = Campaigns(i)(0)
            CampName = Campaigns(i)(1)
            Keywords = Campaigns(i)(2)
            Link = Campaigns(i)(3)
            CheckEvery = Campaigns(i)(4)
            OkToUse = Campaigns(i)(5)
            Sent = Campaigns(i)(6)
              Start Process
            UpdateCells(CampID, "Status", "Looking Up New Links (" & CampID & ")")
            Exit For
        Next
        While StopProcess = False
            Thread.Sleep(1000)
            UpdateCells(CampID, "Status", "Running Process (" & CampID & ")")
            Thread.Sleep(1000)
            For i = 0 To CheckEvery
                UpdateCells(CampID, "Status", "Re-Checking In " & (CheckEvery - i) & " Seconds")
                Thread.Sleep(1000)
            Next
        End While
          Closing Processes
        Campaigns(CampID)(7) = 0
    End Sub
End Class
最佳回答

你们可以利用SyncLock强迫你的read子等待。

class level so all threads access same lock

private myLock as new Object

然后,当你开始工作并结束工作时,就使用yn。

SyncLock myLock
   process code here
End SyncLock

http://msdn.microsoft.com/en-us/library/1c9txz50%28v=VS.80%29.aspx#Y0"rel=“nofollow”

问题回答

Try looking into QueueUserWorkItem():

Private Sub Button1_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button1.Click
    For i = 0 To Campaigns.Count - 1
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf RunProcess), Campaigns[i])
    Next
End Sub

接着修改了RunProcess()方法,将运动标的列入工作项目:

Private Sub RunProcess(ByVal o As System.Object)
      Process the Campaign
    Dim campaign As Campaign = Ctype(o, Campaign)
End Sub

无需使用,read子将由管理的ThreadPool管理!





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签