Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:24:47+00:00 2026-06-17T06:24:47+00:00

I am very new with Background worker control. I have an existing project that

  • 0

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:

  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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:24:49+00:00Added an answer on June 17, 2026 at 6:24 am

    You could try this:

    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
    

    It’s a little bit complicated but it works.

    ManualResetEvents interrupt the execution of code (if they are told to stop) when reached until you use .Set(). If you use .WaitOne() you set it in stop mode, so it will stop again when reached.

    This code defines two ManualResetEvents. When you click the Button1 starts the function FileOpener() in a new Thread, and then starts the BackgroundWorker. The FileOpener() function shows a FileOpenDialog and waits in the locker1 so when you use locker1.Set() the function shows the file dialog.

    As the myOpenFile is a “global” variable (as well as bOpenFileOK), once the user select the file (or not) you could detect the dialog result (bOpenFileOK) and the selected file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to web development, have many years background on the desktop, but
Hi I'm very new to web development but have a coding background. I'm trying
Very new to XSL (and XML for that matter), but I need to step
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
Very new to python and can't understand why this isn't working. I have a
Very new to C++ and Cocos2d-x but I was just toying around with CCArray
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Im very new in C++ I have found this post http://msdn.microsoft.com/en-us/magazine/cc163486.aspx and trying to
I have a user control that shows a speed in a dial format (An
I have a ListView control receiving data from BackgroundWorker through ReportProgress . The worker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.