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 8491891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:33:44+00:00 2026-06-10T22:33:44+00:00

Before I begin, I have to apologize for two things. One is that it

  • 0

Before I begin, I have to apologize for two things. One is that it is very difficult for me to explain things in a concise manner. Two is that I need to be somewhat vague due to the nature of the company I work for.

I am working on enhancing the functionality of an application that I’ve inherited. It is a very intensive application that runs a good portion of my company’s day to day business. Because of this I am limited to the scope of what I can change–otherwise I’d probably rewrite it from scratch. Anyways, here is what I need to do:

I have several threads that all perform the same task but on different data input streams. Each thread interacts through an API from another software system we pay licensing on to write out to what is called channels. Unfortunately we have only licensed a certain number of concurrently running channels, so this application is supposed to turn them on an off as needed.

Each thread should wait until there is an available channel, lock the channel for itself and perform its processing and then release the channel. Unfortunately, I don’t know how to do this, especially across multiple threads. I also don’t really know what to search Google or this site for, or I’d probably have my answer. This was my thought:

A class that handles the distribution of channel numbers. Each thread makes a call to a member of this class. When it does this it would enter a queue and block until the channel handling class recognizes that we have a channel, signals the waiting thread that a channel is available and passing it the channel id. I have no idea where to begin even looking this up. Below I have some horribly written PsuedoCode of how in my mind I would think it would work.

Public Class ChannelHandler

    Private Shared WaitQueue as New Queue(of Thread)
   '// calling thread adds itself to the queue
    Public Shared Sub WaitForChannel(byref t as thread) 
            WaitQueue.enqueue(t)
    End Sub

    Public Shared Sub ReleaseChannel(chanNum as integer)
        '// my own processing to make the chan num available again
    End Sub

    '// this would be running on a separate thread, polling my database
    '// for an available channel, when it finds one, somehow signal
    '// the first thread in the queue that its got a channel and here's the id
     Public Shared Sub ChannelLoop()
           while true
               if WaitQueue.length > 0 then 
                   if thereIsAChannelAvailable then '//i can figure this out my own
                        dim t as thread = ctype(WaitQueue.dequeue(), Thread)
                         lockTheChannel(TheAvailableChannelNumber) 'performed by me
                       '// signal the thread, passing it the channel number
                        t => SignalReady(theAvailableChannelNumber) '// how to signal?
                    end if
                end if
           end while


    End Sub

End Class

and then

'// this inside the function that is doing the processing:
ChannelHandler.requestChannel(CurrentThread)
while (waitingForSignal) '// how?
    block                '// how?

dim channelNumber as int => getChannelNumberThatWasSignaledBack

'// perform processing with channelNumber

ChannelHandler.ReleaseChannel(channelNumber)

I am working with the .NET Framework 3.5 in VB.NET. I am sure there has got to be some sort of mechanism already built for this, but as I said I have no idea exactly what keywords I should be searching for. Any input pointing me in the right direction (ie specific .NET framework classes to use or code samples) would be greatly appreciated. If I need to elaborate on anything, please let me know and I will to the best of my ability.

Edit: The other problem that I have is that these channels can be turned on/off from outside of this application, manually by the user (or as a result of a user initiated event). I am not concerned with a channel be shut down while a thread is using it (it would throw an exception and then pick back up next time it came through. But the issue is that there are not a constant number of threads fighting over a constant number of channels (if a user turns one on manually, the count is reduced, etc). Both items are variable, so I cant rely on the fact that there are no external forces (ie, something outside this set of threads, which is why I do some processing via my DB to determine an available channel number)

  • 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-06-10T22:33:46+00:00Added an answer on June 10, 2026 at 10:33 pm

    What I would do:

    • Switch the System.Threading.Thread by the System.Threading.Tasks.Task class.
    • If a new Task needs to be created, but the List(Of Task) (or, in your example, Queue(Of Task) ) count greater than the maximum permitted, use the Task.WaitAny method.

    EDIT:

    As I answered the previous block on my phone (which is pretty challenging for writing code), let now me write an example about how I would do it:

    Imports System.Threading.Tasks
    Imports System.Collections.Generic
    
    Public Class Sample
    
        Private Const MAXIMUM_PERMITTED As Integer = 3
        Private _waitQueue As New Queue(Of Task)
    
        Public Sub AssignChannel()
            Static Dim queueManagerCreated As Boolean
            If Not queueManagerCreated Then
                Task.Factory.StartNew(Sub() ManageQueue())
                queueManagerCreated = True
            End If
    
            Dim newTask As New Task(Sub()
                                        ' Connect to 3rd Party software
                                    End Sub)
            SyncLock (_waitQueue)
                _waitQueue.Enqueue(newTask)
            End SyncLock
        End Sub
    
        Private Sub ManageQueue()
            Dim tasksRunning As New List(Of Task)
    
            While True
                If _waitQueue.Count <= 0 Then
                    Threading.Thread.Sleep(10)
                    Continue While
                End If
    
                If tasksRunning.Count > MAXIMUM_PERMITTED Then
                    Dim endedTaskPos As Integer = Task.WaitAny(tasksRunning.ToArray)
    
                    If endedTaskPos > -1 AndAlso
                        endedTaskPos <= tasksRunning.Count Then
                        tasksRunning.RemoveAt(endedTaskPos)
                    Else
                        Continue While
                    End If
                End If
    
                Dim taskToStart As Task
                SyncLock (_waitQueue)
                    taskToStart = _waitQueue.Dequeue()
                End SyncLock
    
                tasksRunning.Add(taskToStart)
                taskToStart.Start()
            End While
        End Sub
    
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I begin, I would like to point out that I have honestly and
Okay, two alternatives, but before I begin, you need to know this: public abstract
Before I begin, I would like to state that I KNOW the Apple Guidelines
Before I begin, I must preface by stating that I am a novice when
Before I begin: I have spent a long time on many forums (including Stack
I've been asked to create an API for clients. Before I begin I have
Before I begin, I should probably mention that this sort of thing should probably
Before I begin, I must warn you that I'm not much of a web
I have a series of very complex XML files. I need to access these
Before I begin, I have read through the following discussions: XML Parsing Error: not

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.