gpt4 book ai didi

vb.net - 后台 worker 和 SaveDialog

转载 作者:行者123 更新时间:2023-12-02 03:06:22 24 4
gpt4 key购买 nike

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

我的问题是我不知道在哪里实现我的后台工作人员会更有效,而且主要问题是如何将 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

最佳答案

你可以试试这个:

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 时,会在新的 Thread 中启动函数 FileOpener(),然后启动 BackgroundWorkerFileOpener() 函数显示一个 FileOpenDialog 并在 locker1 中等待,因此当您使用 locker1.Set() 时该函数显示文件对话框。

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

关于vb.net - 后台 worker 和 SaveDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14315180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com