Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:17:44+00:00 2026-05-28T04:17:44+00:00

I have a sub-procedure which I want to run a different process, depending on

  • 0

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.

I tried to avoid the problem by adding a Thread.Sleep(100) delay inbetween starting threads but this led to exactly the same problem.

Here’s an example of what I am trying to do:

    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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:17:44+00:00Added an answer on May 28, 2026 at 4:17 am

    You can use SyncLock to force your threads to wait.

    class level so all threads access same lock

    private myLock as new Object
    

    Then use syncLock when you start your process and end it when you are done.

    SyncLock myLock
      'process code here
    End SyncLock
    

    More MSDN info on the subject

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat complex query with multiple (nested) sub-queries, which I want to
I have a stored procedure in which I'm using sub query, but the issue
This is the code I have: Sub Main() Dim dts As New DataSet Dim
i have a sub with this signature Public Sub Save(ByVal obj As IPta) now
I want to do the following: The user should have his own sub site
I have programmed a sub procedure that will be called in the main procedure
If I have an algorithm which is comprised of (let's say) three sub-algorithms, all
i have this procedure for inserting rows in tables(sql server 2005) CREATE PROCEDURE ans_insert
Very simple problem: I have a Public Sub (in a module) that I want
I have an MSSQL2005 stored procedure here, which is supposed to take an XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.