Click here to Skip to main content
15,891,645 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Dave Kreskowiak18-May-09 4:06
mveDave Kreskowiak18-May-09 4:06 
QuestionRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Sonhospa18-May-09 9:11
Sonhospa18-May-09 9:11 
AnswerRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Dave Kreskowiak18-May-09 10:01
mveDave Kreskowiak18-May-09 10:01 
NewsRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Sonhospa18-May-09 10:12
Sonhospa18-May-09 10:12 
GeneralRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Dave Kreskowiak18-May-09 13:13
mveDave Kreskowiak18-May-09 13:13 
GeneralRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Sonhospa18-May-09 20:14
Sonhospa18-May-09 20:14 
GeneralRe: BackgroundWorkers 'RunWorkerCompleted' not fired Pin
Dave Kreskowiak19-May-09 17:14
mveDave Kreskowiak19-May-09 17:14 
NewsRe: BackgroundWorkers 'RunWorkerCompleted' not fired - Resolved Pin
Sonhospa18-May-09 10:53
Sonhospa18-May-09 10:53 
Daves hint about queues lead me to a real nice solution for the producer/consumer issue I've been fighting with. A first test with some sample code works perfectly between different threads.

For those interested, here's the sample (translated from C# to VB.NET):
Imports System.Threading

Public Class Test
    Private Shared queue As ProducerConsumer

    Shared Sub Main()
        queue = New ProducerConsumer()
        CType(New Thread(New ThreadStart(AddressOf ConsumerJob)), Thread).Start()

        Dim rng As New Random(0)
        For i As Integer = 0 To 9
            Console.WriteLine("Producing {0} in thread {1}", i, Thread.CurrentThread.ManagedThreadId.ToString)
            queue.Produce(i)
            Thread.Sleep(rng.Next(1000))
        Next i
    End Sub

    Private Shared Sub ConsumerJob()
        ' Make sure we get a different random seed from the first thread
        Dim rng As New Random(1)
        ' We happen to know we've only got 10 items to receive
        For i As Integer = 0 To 9
            Dim o As Object = queue.Consume()
            Console.WriteLine(Constants.vbTab + Constants.vbTab + Constants.vbTab + Constants.vbTab & "Consuming {0} in thread {1}", o, Thread.CurrentThread.ManagedThreadId.ToString)
            Thread.Sleep(rng.Next(1000))
        Next i
    End Sub
End Class

Public Class ProducerConsumer
    Private ReadOnly listLock As Object = New Object()
    Private queue As New Queue()

    Public Sub Produce(ByVal o As Object)
        SyncLock listLock
            queue.Enqueue(o)
            ' We always need to pulse, even if the queue wasn't empty before. Otherwise, if we add several items
            ' in quick succession, we may only pulse once, waking a single thread up, even if there are multiple
            ' threads() waiting for items.            
            Monitor.Pulse(listLock)
        End SyncLock
    End Sub

    Public Function Consume() As Object
        SyncLock listLock
            ' If the queue is empty, wait for an item to be added. Note that this is a while loop, as we may 
            ' be(pulsed)but not wake up before another thread has come in and consumed the newly added object.
            ' In that case, we'll have to wait for another pulse.
            Do While queue.Count = 0
                ' This releases listLock, only reacquiring it after being woken up by a call to Pulse
                Monitor.Wait(listLock)
            Loop
            Return queue.Dequeue()
        End SyncLock
    End Function
End Class
An extra warning / quote from the author: "Note that using these methods can easily lead to deadlock - if thread A holds locks X and Y, and waits on Y, but thread B needs to acquire lock X before acquiring and then pulsing Y, thread B won't be able to do anything. Only the lock which is waited on is released, not all the locks the waiting thread owns. Usually you should ensure that prior to waiting, a thread only owns the lock it's going to wait on. Sometimes this isn't possible, but in those cases you should think extra carefully about how everything is going to work."

But for my purposes I seem to have a proper basis Thumbs Up | :thumbsup:
Kind regards and thank you, Dave!
Mick
QuestionConvert Text file into Excel file Pin
vijay248217-May-09 23:14
vijay248217-May-09 23:14 
AnswerRe: Convert Text file into Excel file Pin
Rajesh Anuhya17-May-09 23:29
professionalRajesh Anuhya17-May-09 23:29 
GeneralRe: Convert Text file into Excel file Pin
vijay248218-May-09 0:09
vijay248218-May-09 0:09 
GeneralRe: Convert Text file into Excel file Pin
Rajesh Anuhya18-May-09 0:36
professionalRajesh Anuhya18-May-09 0:36 
AnswerClarification Please Pin
Dalek Dave17-May-09 23:29
professionalDalek Dave17-May-09 23:29 
GeneralRe: Clarification Please Pin
vijay248217-May-09 23:55
vijay248217-May-09 23:55 
QuestionText is getting typed into another text box Pin
tiagu17-May-09 22:33
tiagu17-May-09 22:33 
AnswerRe: Text is getting typed into another text box Pin
Johan Hakkesteegt18-May-09 0:12
Johan Hakkesteegt18-May-09 0:12 
GeneralRe: Text is getting typed into another text box [modified] Pin
tiagu18-May-09 0:50
tiagu18-May-09 0:50 
GeneralRe: Text is getting typed into another text box Pin
Johan Hakkesteegt18-May-09 0:54
Johan Hakkesteegt18-May-09 0:54 
GeneralRe: Text is getting typed into another text box Pin
tiagu18-May-09 4:40
tiagu18-May-09 4:40 
GeneralRe: Text is getting typed into another text box Pin
tiagu18-May-09 4:52
tiagu18-May-09 4:52 
QuestionInserting block of file into a db...(logic issue) Pin
Lee Mwangi17-May-09 21:12
Lee Mwangi17-May-09 21:12 
AnswerRe: Inserting block of file into a db...(logic issue) Pin
Johan Hakkesteegt17-May-09 21:22
Johan Hakkesteegt17-May-09 21:22 
AnswerRe: Inserting block of file into a db...(logic issue) Pin
riced18-May-09 6:33
riced18-May-09 6:33 
QuestionRe: Inserting block of file into a db...(logic issue) Pin
Lee Mwangi18-May-09 20:27
Lee Mwangi18-May-09 20:27 
AnswerRe: Inserting block of file into a db...(logic issue) Pin
riced18-May-09 22:17
riced18-May-09 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.