Click here to Skip to main content
15,867,950 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys,

I am very new with Background worker control. I have an existing project that builds file but throughout my project while building files I get the deadlock error.
I am trying to solve it by creating another project that will only consist out of the background worker. I will then merge them.

My problem is I don't know where it will be more effective for my background worker to be implemented and also the main problem is how can I use the SaveDialog with my background worker? I need to send a parameter to my background worker project telling it when my file is being build en when it is done.

This is where my file is being build:
VB
' Create file
      srOutputFile = New System.IO.StreamWriter(strFile, False)

      For iSeqNo = 0 To iPrintSeqNo
        ' Loop through al the record types
        For Each oRecord As stFileRecord In pFileFormat
          If dsFile.Tables.Contains(oRecord.strRecordName) Then
            ' Loop through al the records
            For Each row As DataRow In dsFile.Tables(oRecord.strRecordName).Rows
              ' Check record id
              If oRecord.strRecordId.Length = 0 Then
                bMatched = True
              Else
                bMatched = (CInt(oRecord.strRecordId) = CInt(row.Item(1)))
              End If

              ' Match records
              If iSeqNo = CInt(row.Item(0)) And bMatched Then
                strRecord = ""
                ' Loop through al the fields
                For iLoop = 0 To UBound(oRecord.stField)
                  ' Format field
                  If oRecord.stField(iLoop).iFieldLength = -1 Then
                    If strRecord.Length = 0 Then
                      strTmp = row.Item(iLoop + 1).ToString
                    Else
                      strTmp = strDelimiter & row.Item(iLoop + 1).ToString
                    End If
                  ElseIf oRecord.stField(iLoop).eFieldType = enumFieldType.TYPE_VALUE Or _
                         oRecord.stField(iLoop).eFieldType = enumFieldType.TYPE_AMOUNT_CENT Then

                    strTmp = row.Item(iLoop + 1).ToString.Replace(".", "").PadLeft(oRecord.stField(iLoop).iFieldLength, "0")
                    strTmp = strTmp.Substring(strTmp.Length - oRecord.stField(iLoop).iFieldLength)
                  Else
                    strTmp = row.Item(iLoop + 1).ToString.PadRight(oRecord.stField(iLoop).iFieldLength, " ").Substring(0, oRecord.stField(iLoop).iFieldLength)
                  End If

                  If oRecord.stField(iLoop).iFieldLength > -1 And (bForceDelimiter) And strRecord.Length > 0 Then
                    strTmp = strDelimiter & strTmp
                  End If

                  strRecord = strRecord & strTmp
                Next

                ' Final delimiter
                If (bForceDelimiter) Then
                  strRecord = strRecord & strDelimiter
                End If

                srOutputFile.WriteLine(strRecord)
              End If
            Next
          End If
        Next
      Next


and this is how I call my method in my SaveToolStripMenu event:

oDisplayFile.sbWriteFile(ToolStripFileName.Text)

Any help would be much appreciated,
Posted

1 solution

You got a lot going on in your question and I'm not sure I understand all of it, but here goes.

First of all, You don't need to create an entirely different VS project to do this. In fact, I'd guess that it's more work to do that and merge them later. If I were you, I'd copy what I had now and keep it as a backup in case I messed it up but still work on changes in just one project.

Second, I can't see anywhere in your code any reference to a save dialog or to a backgroundworker. I am guessing that you are just looking for advice on how to organize it, and that really depends on how it's being used. Typically when I do this, I use the save dialog control to collect the name of the file the user wants. Then I pass it into the backgroundworker using the RunWorkerAsyc(object) method[^]. You can pass an object into the background worker this way. However, you can only pass one object. So if you need multiple things passed into the backgroundworker, it's a good idea to make a custom class to kind of package it all before sending it in.

Here[^] is a general walk-through in how the background worker can be used, if you need more help.

I hope this helps. If not, could you explain a bit more of what you are actually looking for and what you have tried?
 
Share this answer
 
Comments
Gericke Hoeksema 16-Jan-13 2:51am    
Yes I am still in the process to plan my project and to get all the info before I start. Thank you I will give it a try.

this is my SaveDialog process:

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim cCompClient As New CompressClient.Utilities.CompressClient.CompressClient
Dim cFtp As New Utilities.FTP.FTPclient
Try
Dim dlg As New SaveFileDialog()
Dim strFileName As String

If oDisplayFile Is Nothing Then
MsgBox("Please load file.", MsgBoxStyle.Critical)
Exit Sub
End If

' Set defaults
dlg.InitialDirectory = (New cParameters).fnGetParameter("PACS_FILES", "")
dlg.DefaultExt = "*.txt"
dlg.FileName = ToolStripFileName.Text
dlg.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
strFileName = dlg.FileName

If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
' Force update file
sbDisplayFile(iTable, True)

' Save file
ToolStripFileName.Text = dlg.FileName
oDisplayFile.sbWriteFile(ToolStripFileName.Text)

'Compress file
cCompClient.ZipFile(dlg.FileName)

End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900