473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Threading: Threads stall, while others run.

DTeCH
23
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 Images
File Type: png StalledThreads.png (17.1 KB, 171 views)
Jul 12 '11 #1
2 1647
Christian Binder
218 Expert 100+
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?
Jul 12 '11 #2
DTeCH
23
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3.         Process.GetCurrentProcess.PriorityClass = ProcessPriorityClass.Idle
  4.  
  5.         Try
  6.             sServer = GetSetting("Program's Name", "Server Settings", "Server").Trim
  7.         Catch ex As Exception
  8.             sServer = ""
  9.         End Try
  10.         Try
  11.             iPort = CInt(Val(GetSetting("Program's Name", "Server Settings", "Port").Trim))
  12.             If iPort = 0 Then
  13.                 iPort = 119
  14.             End If
  15.         Catch ex As Exception
  16.             iPort = 119
  17.         End Try
  18.         Try
  19.             bIsSSL = GetSetting("Program's Name", "Server Settings", "SSL").Trim
  20.         Catch ex As Exception
  21.             bIsSSL = False
  22.         End Try
  23.         Try
  24.             UserConnections = CInt(Val(GetSetting("Program's Name", "Server Settings", "Connections").Trim))
  25.             If UserConnections = 0 Then
  26.                 UserConnections = 2
  27.             End If
  28.         Catch ex As Exception
  29.             UserConnections = 2
  30.         End Try
  31.         Try
  32.             sUsername = _Decrypt(GetSetting("Program's Name", "Server Settings", "UserName").Trim, GenerateSysKey())
  33.         Catch ex As Exception
  34.             sUsername = ""
  35.         End Try
  36.         Try
  37.             sPassword = _Decrypt(GetSetting("Program's Name", "Server Settings", "Password").Trim, GenerateSysKey())
  38.         Catch ex As Exception
  39.             sPassword = ""
  40.         End Try
  41.         If sServer IsNot "" And sServer IsNot Nothing And UserConnections <> 0 Then
  42.             _BTCount = UserConnections
  43.             xBGThread = New Thread(AddressOf ThreadBeginThreads)
  44.             xBGThread.SetApartmentState(ApartmentState.STA)
  45.             xBGThread.Priority = ThreadPriority.Lowest
  46.             xBGThread.IsBackground = True
  47.             xBGThread.Start()
  48.             bReadyToConnect = True
  49.             GoTo 1
  50.         Else
  51.             If frmSettings.ShowDialog() = DialogResult.OK Then
  52.                 If sServer IsNot "" And sServer IsNot Nothing Then
  53.                     If iPort <> 0 Then
  54.                         bReadyToConnect = True
  55.                     End If
  56.                 End If
  57.             End If
  58.         End If
  59.         If bReadyToConnect = True Then
  60.             _BTCount = UserConnections
  61.             xBGThread = New Thread(AddressOf ThreadBeginThreads)
  62.             xBGThread.SetApartmentState(ApartmentState.STA)
  63.             xBGThread.Priority = ThreadPriority.Lowest
  64.             xBGThread.IsBackground = True
  65.             xBGThread.Start()
  66.         End If
  67. 1:
  68.         Dim EXfileName As String = Nothing
  69.         Try
  70.             If Command() Is Nothing Or Command() = "" Then
  71.       'No Commandline passed in
  72.             Else
  73.       'Deal with Commandline params
  74.           End If
  75.         Catch ex As Exception
  76.             MsgBox(ex.Message & vbCrLf & vbCrLf & Command().Trim)
  77.         End Try
  78.     End Sub
  79.  
This is an example of the threads being created:
Expand|Select|Wrap|Line Numbers
  1.     Public Sub ThreadBeginThreads()
  2.         Dim _Count As Integer = _BTCount
  3.         DLThreads.Clear() ' Global list(Of Integer)
  4.         For i As Integer = 0 To (_Count - 1)
  5.             DLThreads.Add(0)
  6.             Select Case i
  7.                 Case 0
  8.                     BG_Work0.RunWorkerAsync()
  9.                 Case 1
  10.                     BG_Work1.RunWorkerAsync()
  11.                 Case 2
  12.                     BG_Work2.RunWorkerAsync()
  13.                 Case 3
  14.                     BG_Work3.RunWorkerAsync()
  15.                 Case 4
  16.                     BG_Work4.RunWorkerAsync()
  17.                 Case 5
  18.                     BG_Work5.RunWorkerAsync()
  19.                 Case 6
  20.                     BG_Work6.RunWorkerAsync()
  21.                 Case 7 ' And so on...
  22.                     BG_Work7.RunWorkerAsync()
  23.             End Select
  24.         Next
  25.         DLThreads.Add(0)
  26.     End Sub
  27.  
This is an example of the BG_Work0:
Expand|Select|Wrap|Line Numbers
  1.     Public WithEvents  _Son0 As TheClassThatDoesTheWork 'Declaration in Form1
  2.     ' And so on...
  3.  
  4.     Private Sub BG_Work0_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG_Work0.DoWork
  5.         _Son0 = New TheClassThatDoesTheWork(0)
  6.     End Sub
  7.  
This is an example of _Son0 getting fired events from the instance of TheClassThatDoesTheWork(0):
Expand|Select|Wrap|Line Numbers
  1.     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
  2.         Try
  3.             ' Deal with the event
  4.         Catch ex As Exception
  5.             ' Deal with Error
  6.         End Try
  7.     End Sub
  8.  
This is an example of TheClassThatDoesTheWork(0):
Expand|Select|Wrap|Line Numbers
  1. 'Imports required
  2.  
  3. Public Class TheClassThatDoesTheWork
  4.  
  5. 'All the declarations required
  6. 'All the events
  7.  
  8.  
  9.     Public Property Terminate_ThreadWorker As Boolean
  10.         Get
  11.             Return xTerminateThread
  12.         End Get
  13.         Set(ByVal value As Boolean)
  14.             xTerminateThread = value
  15.         End Set
  16.     End Property
  17.  
  18.     Public Sub New(ByVal xID As Integer)
  19.         _ID = xID
  20.         _Worker.Timeout = 30
  21.         _Worker.NewsServer = sServer
  22.         _Worker.NewsPort = iPort
  23.         _Worker.User = sUsername
  24.         _Worker.Password = sPassword
  25.         Select Case bIsSSL
  26.             Case True
  27.                 _Worker.SSLStartMode = sslImplicit
  28.             Case False
  29.                 _Worker.SSLStartMode = sslNone
  30.         End Select
  31.         xThread = New Thread(AddressOf DEngine)
  32.         xThread.SetApartmentState(ApartmentState.STA)
  33.         xThread.Priority = ThreadPriority.Lowest
  34.         xThread.IsBackground = True
  35.         xThread.Start()
  36.     End Sub
  37.  
  38.     Private Sub DEngine()
  39.         Nap = New TakeNap ' Non-Blocking sleep
  40.         Do Until _IsClosing = True Or xTerminateThread = True
  41.             Nap.Sleep(50) ' or  Thread.Sleep(50)
  42.             If SomeVariable > 0 And SomeOtherVariable = False Then
  43.                 Try
  44.                     'Get the file
  45.                 Catch ex As Exception
  46.       'Report error to user, then...
  47.                     GoTo 1
  48.                 End Try
  49.                 BusyNow = False
  50.                 If CurrentFile = "" Then GoTo 1
  51.                 GC.Collect()
  52.             ElseIf SomeOtherVariable = True Then
  53.                 Do Until SomeOtherVariable = False
  54.                     Nap.Sleep(50) ' or  Thread.Sleep(50)
  55.                 Loop
  56.             ElseIf SomeVariable < 1 And SomeOtherVariable = True Then
  57.                 ' do stuff
  58.             End If
  59. 1:
  60.             If PAUSED_STOPPED = True Then
  61.                 BusyNow = False
  62.             End If
  63.             If _Worker.Connected = False Then
  64.                 BusyNow = False
  65.  
  66.                 Try
  67.                     _Worker.Connect()
  68.                 Catch ex As Exception
  69.      ' Fire an event
  70.                 End Try
  71.             End If
  72.         Loop
  73.     End Sub
  74.  
  75.     'All required Subs, & Functions
  76.  
  77.     Protected Overrides Sub Finalize()
  78.         MyBase.Finalize()
  79.     End Sub
  80. End Class
  81.  
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.
Jul 13 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Lane LiaBraaten | last post by:
I am developing a GUI that uses multiple threading.Threads, and Queue.Queues for communicating between the threads, and threading.Timers for checking the queues. Everything works well (runs to...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
9
by: Arafangion | last post by:
Hello, recently I've been trying to figure out how the heck to just destroy a thread. I have since rewritten the code in question, but what I was trying to do was to use the .Abort() method, but...
3
by: OpticTygre | last post by:
I have a class, ProcessFiles, with several subroutines it runs for each type of file I want to "process." First, I check directories for files. Then, based on the filenames of those I find in...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
15
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
2
by: Zerge | last post by:
I can launch threads just fine, but then I have to do a time.sleep(n) so the main thread from which they where launched will wait for all the threads to return. How can I detect when all threads...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.