We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,451 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Multhreading Issue - Que Is Empty

Hello,

In my multithreaded applications, on occassion the error Que Is Empty occurs when trying to clear the que. Is there any way to prevent this from happening or catch it without causing the program to lock up? Below is the code that throws the exception.

`

  Private Sub Callback(ByVal o As Object)
            If tasks.Count > 0 Then
                Dim Task As Action
                SyncLock (Lock)
                    Task = tasks.Dequeue
            End SyncLock
        Task.BeginInvoke(AddressOf Callback, Nothing)
        Else
        Threading.Interlocked.Decrement(RunningThread)
            End If
    End Sub

Any help would be great apprecied.

Many thanks,

Resentful

2
Contributors
2
Replies
15 Hours
Discussion Span
3 Months Ago
Last Updated
3
Views
Resentful
Junior Poster in Training
55 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Do the sinclock before anything else in that function. Move line 4 up to between lines 1 and 2 and move line 6 down to line 11. If you don't want to lock the entire thread then you might consider something like this: (I'm not VB.NET expert, but I think this will work)

  Private Sub Callback(ByVal o As Object)
      Dim Task As Action = Nothing
      SyncLock (Lock)
           If tasks.Count > 0 Then
              Task = tasks.Dequeue
            END IF
      End SyncLock
      IF Task <> Nothing THEN
        Task.BeginInvoke(AddressOf Callback, Nothing)
      Else
        Threading.Interlocked.Decrement(RunningThread)
      End If
    End Sub
Ancient Dragon
Achieved Level 70
Team Colleague
32,159 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

Thank you for your reply. I'll see if I can run the program for an hour without it locking up.

Resentful
Junior Poster in Training
55 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0870 seconds using 2.8MB