Click here to Skip to main content
16,011,120 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Server Connectivity if no server exist Pin
Samir Ibrahim19-May-09 4:35
Samir Ibrahim19-May-09 4:35 
QuestionExcel column formatting issue, format style changed when formatted again with the same style Pin
BREdwards18-May-09 3:36
BREdwards18-May-09 3:36 
AnswerRe: Excel column formatting issue, format style changed when formatted again with the same style Pin
Johan Hakkesteegt19-May-09 3:06
Johan Hakkesteegt19-May-09 3:06 
QuestionCompare 2 Excel Sheets Pin
vijay248218-May-09 3:25
vijay248218-May-09 3:25 
AnswerRe: Compare 2 Excel Sheets Pin
dan!sh 18-May-09 4:08
professional dan!sh 18-May-09 4:08 
QuestionHow to merge columns Pin
Pankaj Garg18-May-09 0:20
Pankaj Garg18-May-09 0:20 
AnswerRe: How to merge columns Pin
Dave Kreskowiak18-May-09 1:52
mveDave Kreskowiak18-May-09 1:52 
QuestionBackgroundWorkers 'RunWorkerCompleted' not fired Pin
Sonhospa17-May-09 23:38
Sonhospa17-May-09 23:38 
Hi all,

it's been a lot of learning already, but here's a point I can't figure out even with the help all the sources I reviewed by now. Maybe one of you can help me find my error?

In my little application (written in VB 2008 Express) I use Backgroundworkers. One of them is supposed to read / filter Outlook-Mails and put extracted customer data into datatable rows. The other one gets the rows and checks a DB for previous inquiries of that customer. Between both of them I put a buffer in order to avoid race conditions. I hope I expressed the basic concept well enough to be understandable - and although it's working I'd appreciate comments and ideas to make the code easier.

What doesn't work is: The RunWorkerCompleted-event in the class "Consumer" is not fired, which I tried to work around in the ProgressChanged-event, but that can't be it. Here's the code:

First I give you the buffer class in which I guess the error must be - suspecting some SyncLock or Monitor activities (which I took from a sample without fully understanding them):
Public Class Puffer
        Private mBuffer As New List(Of DataRow) With {.capacity = 1}

        Property Puffer() As DataRow
            Get
                SyncLock (Me)
                    If mBuffer.Count = 0 Then
                        MsgBox("Alle Puffer sind leer. " & Thread.CurrentThread.Name & " wartet.")
                        Monitor.Wait(Me)
                    End If

                    Dim readValue As DataRow = mBuffer(0)
                    MsgBox(String.Format("{0} liest Element '{1}'.", Thread.CurrentThread.Name, readValue.Item("Name")))
                    OutCounter += 1
                    mBuffer.Remove(mBuffer(0))
                    Monitor.Pulse(Me)
                    Return readValue

                End SyncLock
            End Get

            Set(ByVal WriteValue As DataRow)
                SyncLock (Me)
                    If mBuffer.Count = mBuffer.Capacity Then
                        MsgBox("Alle Puffer sind voll. " & Thread.CurrentThread.Name & " wartet.")
                        Monitor.Wait(Me)
                    End If

                    MsgBox(String.Format("{0} schreibt Element '{1}'.", Thread.CurrentThread.Name, WriteValue.Item("Name")))
                    mBuffer.Add(WriteValue)
                    InCounter += 1
                    Monitor.Pulse(Me)

                End SyncLock
            End Set

        End Property ' Buffer
    End Class

Producer class (shortened):
Public Class Producer
       Dim WithEvents ProducerThread As New BackgroundWorker
       Private Puffer As Puffer

       Public Sub New(ByVal sharedObject As Puffer)

           With ProducerThread
               .WorkerReportsProgress = True
               .WorkerSupportsCancellation = True
               .RunWorkerAsync()
           End With
           Puffer = sharedObject

       End Sub

       Public Sub Produce(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ProducerThread.DoWork
           Thread.CurrentThread.Name = "Produzent"

           'Return a reference to the MAPI layer
           Dim oApp As New Outlook.Application
           Dim oNameSpace As Outlook.NameSpace = oApp.GetNamespace("MAPI")
           ....

           Try
               oNameSpace.Logon(Nothing, Nothing, False, False) ' TODO: Ggf ändern...
               ....
               ' Get Messages collection of Inbox and count them
               Dim oItems As Outlook.Items = oInFolder.Items
               Dim intTotal As Integer = oItems.Count

               ' Loop each unread message.
               For i As Integer = 1 To oItems.Count
                   If ProducerThread.CancellationPending Then Exit Sub

                   If CurrentMedia <> "" Then
                       Dim CurrentRow As DataRow = MailsTable.NewRow()
                       CurrentRow("Absender") = Inquiry.ClientMail
                       ....
                       ' erst einmal in den Puffer
                       Puffer.Puffer = CurrentRow           <----- write into the buffer

                       CurrentRow = Nothing
                   End If
                   oMsg = Nothing
               Next
               oItems = Nothing

           Catch ex As System.Exception
               MsgBox("Fehler in der Routine 'Produce'", MsgBoxStyle.Critical, String.Format("Klasse '{0}'", Me.GetType.Name))

           End Try
       End Sub

       Private Sub ProducerThread_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ProducerThread.RunWorkerCompleted
           'Thread.CurrentThread.Name = "Produzent"
                 frmProcessOutlAtt.btnRead.Text = "Lesen"

       End Sub
   End Class

Consumer Class (shortened):
Public Class Consumer
        Dim WithEvents ConsumerThread As New BackgroundWorker
        Private Puffer As Puffer
        Private rowCounterValid As Integer
        Private rowCounterInvalid As Integer

        Public Sub New(ByVal sharedObject As Puffer)
            With ConsumerThread
                .WorkerReportsProgress = True
                .WorkerSupportsCancellation = True
                .RunWorkerAsync()
            End With
            Puffer = sharedObject
        End Sub

        Public Sub Consume(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ConsumerThread.DoWork
            MailsTable.Clear()
            Dim DA As m2prolapDataSetTableAdapters.AdressenTableAdapter = New m2prolapDataSetTableAdapters.AdressenTableAdapter
            Try
                If ConsumerThread.CancellationPending then Exit Sub

                Dim myrow As DataRow = Puffer.Puffer           <----- read from the buffer
                While myrow IsNot Nothing
                    If DA.GetDataByNamesStreetsMail(myrow("Name"), myrow("Strasse"), myrow("Vorname"), myrow("Absender")).Count > 0 Then
                        rowCounterValid += 1
                        myrow("Antworten") = False
                        ConsumerThread.ReportProgress(1, myrow)
                    Else
                        rowCounterInvalid += 1
                        myrow("Antworten") = True
                        ConsumerThread.ReportProgress(0, myrow)
                    End If

                    myrow = Puffer.Puffer
                End While

            Catch ex As System.Exception
                MsgBox("Fehler in der Routine 'Consume'", MsgBoxStyle.Critical, String.Format("Klasse '{0}'", Me.GetType.Name))

            End Try

        End Sub

        Private Sub ConsumerThread_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles ConsumerThread.ProgressChanged
            Dim myRow As DataRow = e.UserState
            Dim Marker As Boolean = CBool(e.ProgressPercentage)
            MailsTable.Rows.Add(myRow)

            With frmProcessOutlAtt
                .DataGrid.DataSource = MailsTable
                .DataGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
                .DataGrid.Refresh()
            End With

            'MessageBox.Show(String.Format("Datensatz '{0}' wurde ausgewertet.", myRow("Name")))
            'OutCounter = MailsTable.Rows.Count

        End Sub

        'THIS ONE IS NOT FIRED AT ALL:
        Private Sub ConsumerThread_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ConsumerThread.RunWorkerCompleted
            Thread.CurrentThread.Name = "Konsument"
            MsgBox(String.Format("{0} Datensätze von {1} verarbeitet." & vbCrLf & "{1} ist beendet.", _
                                 OutCounter, Thread.CurrentThread.Name), MsgBoxStyle.Information)
        End Sub
    End Class

I really hope that one of you can find the time and examine what happens behind the borders of my knowledge here... As written before, I think that something about "SyncLock" or "Monitor" prevents the Consumers "RunWorkerCompleted" from getting fired. But as we all know there are so many trees Shucks | :-\ sometimes.... maybe you just find a typing error Sigh | :sigh:

Thank you in advance
Mick
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 
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 

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.