3

我对后台工作人员控制非常陌生。我有一个构建文件的现有项目,但在整个项目中构建文件时出现死锁错误。我试图通过创建另一个仅由后台工作人员组成的项目来解决它。然后我将它们合并。

我的问题是我不知道在哪里实施我的后台工作人员会更有效,而且主要问题是如何将 SaveDialog 与我的后台工作人员一起使用?我需要向我的后台工作程序项目发送一个参数,告诉它何时构建我的文件。

这是我的文件正在构建的地方:

  srOutputFile = New System.IO.StreamWriter(strFile, False) 'Create File

  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
4

1 回答 1

1

你可以试试这个:

Private locker1 As ManualResetEvent = New System.Threading.ManualResetEvent(False)
Private locker2 As ManualResetEvent = New System.Threading.ManualResetEvent(False)
Dim bOpenFileOK As Boolean
Dim myOpenFile As OpenFileDialog = New OpenFileDialog()

Private Sub FileOpener()
    While Not bTerminado
        If myOpenFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            bOpenFileOK = True
        Else
            bOpenFileOK = False
        End If

        locker2.Set()
        locker1.WaitOne()
    End While
End Sub

' Detonator of the action
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
    Dim tFileOp As Thread = New Thread(AddressOf FileOpener)
    tFileOp.SetApartmentState(ApartmentState.STA)
    tFileOp.Start()

    ' Start BackgroundWorker
    BW1.RunWorkerAsync()
End Sub

Private Sub AsyncFunctionForBW(ByVal args As ArrayList)
    '[...]

    'Change options dinamically for the OpenFileDialog
    myOpenFile.Filter = ""
    myOpenFile.MultiSelect = True

    'Calling the FileDialog
    locker1.Set()
    locker2.WaitOne()
    locker1.Reset()
    locker2.Reset()

    If bOpenFileOK Then
        myStream = myOpenFile.OpenFile()

        '[...]
    End If
End Sub

这有点复杂,但它有效。

ManualResetEvents到达时中断代码的执行(如果他们被告知停止),直到您使用.Set(). 如果您使用.WaitOne()它,则将其设置为停止模式,因此到达时它将再次停止。

此代码定义了两个ManualResetEvents. 当您单击Button1启动FileOpener()新功能时Thread,然后启动BackgroundWorker. 该FileOpener()函数显示一个FileOpenDialog并等待,locker1所以当您使用locker1.Set()该函数时显示文件对话框。

由于myOpenFile是“全局”变量(以及bOpenFileOK),一旦用户选择(或不选择)文件,您就可以检测到对话框结果(bOpenFileOK)和所选文件。

于 2013-01-14T09:05:17.700 回答