Threading: Threads stall, while others run.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DTeCH
    New Member
    • Apr 2011
    • 23

    Threading: Threads stall, while others run.

    When the program starts, it checks for the amount of threads the user wants to use to download files with. The threads are then created. When the user chooses a file to download, the threads begin grabbing the file until it's fully downloaded.

    The problem is, some threads stall right from the start, & just sit there doing nothing.

    The global work variable is Sync-Locked, then quickly released by each instance, & to my eyes, there is nothing wrong.

    They are all instances of the same class, & do the exact same thing, so why is it that some run, & others don't?

    VB.NET is the programming language. They are looping Background workers.

    I would really like to know why some run, & others don't.

    btw... they have quick non-blocking sleeps, as well as blocking sleeps to allow others the chance to run (proprietary non-blocking DLL).

    So all i'm left to think on is that Microsoft's process queuing has some type of bug going on in there someplace.

    Can anyone verify this?

    ...or is there a way to do multiple looping threads where they're guaranteed to run?

    Any help will be awesomely appreciated.
    Attached Files
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Did you debug the working-method, if this method is called at all?

    Can you provide some code?

    Is the amount of running (well working) threads always the same or does it differ when you cloae your program and start it again?

    Comment

    • DTeCH
      New Member
      • Apr 2011
      • 23

      #3
      Code:
          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
              Process.GetCurrentProcess.PriorityClass = ProcessPriorityClass.Idle
      
              Try
                  sServer = GetSetting("Program's Name", "Server Settings", "Server").Trim
              Catch ex As Exception
                  sServer = ""
              End Try
              Try
                  iPort = CInt(Val(GetSetting("Program's Name", "Server Settings", "Port").Trim))
                  If iPort = 0 Then
                      iPort = 119
                  End If
              Catch ex As Exception
                  iPort = 119
              End Try
              Try
                  bIsSSL = GetSetting("Program's Name", "Server Settings", "SSL").Trim
              Catch ex As Exception
                  bIsSSL = False
              End Try
              Try
                  UserConnections = CInt(Val(GetSetting("Program's Name", "Server Settings", "Connections").Trim))
                  If UserConnections = 0 Then
                      UserConnections = 2
                  End If
              Catch ex As Exception
                  UserConnections = 2
              End Try
              Try
                  sUsername = _Decrypt(GetSetting("Program's Name", "Server Settings", "UserName").Trim, GenerateSysKey())
              Catch ex As Exception
                  sUsername = ""
              End Try
              Try
                  sPassword = _Decrypt(GetSetting("Program's Name", "Server Settings", "Password").Trim, GenerateSysKey())
              Catch ex As Exception
                  sPassword = ""
              End Try
              If sServer IsNot "" And sServer IsNot Nothing And UserConnections <> 0 Then
                  _BTCount = UserConnections
                  xBGThread = New Thread(AddressOf ThreadBeginThreads)
                  xBGThread.SetApartmentState(ApartmentState.STA)
                  xBGThread.Priority = ThreadPriority.Lowest
                  xBGThread.IsBackground = True
                  xBGThread.Start()
                  bReadyToConnect = True
                  GoTo 1
              Else
                  If frmSettings.ShowDialog() = DialogResult.OK Then
                      If sServer IsNot "" And sServer IsNot Nothing Then
                          If iPort <> 0 Then
                              bReadyToConnect = True
                          End If
                      End If
                  End If
              End If
              If bReadyToConnect = True Then
                  _BTCount = UserConnections
                  xBGThread = New Thread(AddressOf ThreadBeginThreads)
                  xBGThread.SetApartmentState(ApartmentState.STA)
                  xBGThread.Priority = ThreadPriority.Lowest
                  xBGThread.IsBackground = True
                  xBGThread.Start()
              End If
      1:
              Dim EXfileName As String = Nothing
              Try
                  If Command() Is Nothing Or Command() = "" Then
            'No Commandline passed in
                  Else
        	'Deal with Commandline params
                End If
              Catch ex As Exception
                  MsgBox(ex.Message & vbCrLf & vbCrLf & Command().Trim)
              End Try
          End Sub
      This is an example of the threads being created:
      Code:
          Public Sub ThreadBeginThreads()
              Dim _Count As Integer = _BTCount
              DLThreads.Clear() ' Global list(Of Integer)
              For i As Integer = 0 To (_Count - 1)
                  DLThreads.Add(0)
                  Select Case i
                      Case 0
                          BG_Work0.RunWorkerAsync()
                      Case 1
                          BG_Work1.RunWorkerAsync()
                      Case 2
                          BG_Work2.RunWorkerAsync()
                      Case 3
                          BG_Work3.RunWorkerAsync()
                      Case 4
                          BG_Work4.RunWorkerAsync()
                      Case 5
                          BG_Work5.RunWorkerAsync()
                      Case 6
                          BG_Work6.RunWorkerAsync()
                      Case 7 ' And so on...
                          BG_Work7.RunWorkerAsync()
                  End Select
              Next
              DLThreads.Add(0)
          End Sub
      This is an example of the BG_Work0:
      Code:
          Public WithEvents  _Son0 As TheClassThatDoesTheWork 'Declaration in Form1
          ' And so on...
      
          Private Sub BG_Work0_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG_Work0.DoWork
              _Son0 = New TheClassThatDoesTheWork(0)
          End Sub
      This is an example of _Son0 getting fired events from the instance of TheClassThatDoe sTheWork(0):
      Code:
          Private Sub _Son0_WorkerParent_OnTransfer(ByVal ID As Integer, ByVal _SegFileName As String, ByVal _SegPartNumber As Integer, ByVal _MaxSegParts As Integer, ByVal e As  _TransferEventArgs) Handles _Son0.WorkerParent_OnTransfer
              Try
                  ' Deal with the event
              Catch ex As Exception
                  ' Deal with Error
              End Try
          End Sub
      This is an example of TheClassThatDoe sTheWork(0):
      Code:
      'Imports required
      
      Public Class TheClassThatDoesTheWork
      
      'All the declarations required
      'All the events
      
      
          Public Property Terminate_ThreadWorker As Boolean
              Get
                  Return xTerminateThread
              End Get
              Set(ByVal value As Boolean)
                  xTerminateThread = value
              End Set
          End Property
      
          Public Sub New(ByVal xID As Integer)
              _ID = xID
              _Worker.Timeout = 30
              _Worker.NewsServer = sServer
              _Worker.NewsPort = iPort
              _Worker.User = sUsername
              _Worker.Password = sPassword
              Select Case bIsSSL
                  Case True
                      _Worker.SSLStartMode = sslImplicit
                  Case False
                      _Worker.SSLStartMode = sslNone
              End Select
              xThread = New Thread(AddressOf DEngine)
              xThread.SetApartmentState(ApartmentState.STA)
              xThread.Priority = ThreadPriority.Lowest
              xThread.IsBackground = True
              xThread.Start()
          End Sub
      
          Private Sub DEngine()
              Nap = New TakeNap ' Non-Blocking sleep
              Do Until _IsClosing = True Or xTerminateThread = True
                  Nap.Sleep(50) ' or  Thread.Sleep(50)
                  If SomeVariable > 0 And SomeOtherVariable = False Then
                      Try
                          'Get the file
                      Catch ex As Exception
      	  'Report error to user, then...
                          GoTo 1
                      End Try
                      BusyNow = False
                      If CurrentFile = "" Then GoTo 1
                      GC.Collect()
                  ElseIf SomeOtherVariable = True Then
                      Do Until SomeOtherVariable = False
                          Nap.Sleep(50) ' or  Thread.Sleep(50)
                      Loop
                  ElseIf SomeVariable < 1 And SomeOtherVariable = True Then
                      ' do stuff
                  End If
      1:
                  If PAUSED_STOPPED = True Then
                      BusyNow = False
                  End If
                  If _Worker.Connected = False Then
                      BusyNow = False
      
                      Try
                          _Worker.Connect()
                      Catch ex As Exception
      	 ' Fire an event
                      End Try
                  End If
              Loop
          End Sub
      
          'All required Subs, & Functions
      
          Protected Overrides Sub Finalize()
              MyBase.Finalize()
          End Sub
      End Class
      This was posted to C# instead of VB.NET, but it's basically the same theory, at least that's what other C-Sharpers have told me.

      Sorry for the mistake though.
      Last edited by DTeCH; Jul 13 '11, 11:08 AM. Reason: Noticed this was the C# forum.

      Comment

      Working...