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

  • Home
  • SEARCH
  • 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 3951718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:45:51+00:00 2026-05-20T01:45:51+00:00

I have been creating multiple background threads to parse xml files and recreate new

  • 0

I have been creating multiple background threads to parse xml files and recreate new xml files. Now the problem I am having is that even though I use synclock on global variables, I will still at times get errors and I am sure that this is just the crude way of coding I am doing, but I was wondering if someone had a better option.
program flow =

  • access local folder and upload all files into list
  • strip each file into xml entries and put these entries into an arraylist
  • parse for specific values and enter these values into a database table
  • now create a thread and take the arraylist of entries and the thread will reparse
  • thread parses and creates a new xml file
  • main thread continues with another function and then goes and get a file from list

I will add some code to show problem areas but if I have declared global variable in use does the different threads overwrite that value in the variable causing contamination.

    For Each g In resultsList
        gXmlList.Add(g)
    Next

    Dim bgw As New BackgroundWorker
    bgw.WorkerSupportsCancellation = True
    AddHandler bgw.DoWork, New DoWorkEventHandler(AddressOf createXML)
    AddHandler bgw.RunWorkerCompleted, AddressOf WorkComplete
    threadlist.Add(bgw)
    bgw.RunWorkerAsync()

  Private Sub createXML()
     num += 1
     Dim file As String = Module1.infile
     xmlfile = directoryPath & "\New" & dateTime.Now.ToUniversalTime.ToString("yyyyMMddhhmmss") & endExtension
     Thread.Sleep(2000)
     Dim doc As XmlDocument = New XmlDocument
     **xwriter = New XmlTextWriter(xmlfile, Encoding.UTF8)** this is where ioexception error
     xwriter.Formatting = Formatting.Indented
     xwriter.Indentation = 2
     xwriter.WriteStartDocument(True)
     xwriter.WriteStartElement("Posts")

I have global variables through out the app and should I be locking each one and does this not make using threads then useless.

    Dim j As Integer = 0
  • 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-05-20T01:45:51+00:00Added an answer on May 20, 2026 at 1:45 am

    I believe your biggest problem is not knowing what features in .Net are thread safe. A list for example is not (a dictionary is). While you may get away with it you will eventually run into problems with locking, etc.

    Your using classes and variables that are not thread safe. Any time you are working with threads you have to be Extremely careful with locking. To answer your question, yes, you have to lock and unlock everything you are working with unless the type / method specifically handles it for you.

    There are a lot of multi threading (PLINQ for example) in .Net 4.0 which handle a lot of the “grunt work” for you. While you should learn and understand how to do thread safe code yourself it will give you a head start.

    Try passing the data into the createXML() method. That may help isolate the code from other data being accessed. I would suggest reading up on threading and learning how to do it without a background worker.

    Global variables are generally a bad idea. Given your VB code I’m guessing this is a carry over from the VB6 world for you. That’s not in any way intended to be insulting, just trying to help advance your skills forward. Variable scope should be as confined as possible.

    Another thought looking at your code is to learn how to use String.Format() when building strings / paths.

    Simple manual thread in VB to get you started:

    Dim bThread As New Threading.Thread(AddressOf createXML)
    bThread.IsBackground = True
    bThread.Start()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been creating Unit tests like crazy and find that I'm often having
I have been tasked with creating a program that will generate an amortization schedule.
I have been tasked with creating a program what will create take files in
I have been creating a website with Ruby on Rails, and will be hosting
I have been tasked with creating a reusable process for our Finance Dept to
I have been in both situations: Creating too many custom Exceptions Using too many
For my assignment I have been assigned the task of creating a DTD for
Have been looking at the MVC storefront and see that IQueryable is returned from
So far I have been creating Silverlight apps with all logic crunched into a
So far I have been creating Web Portal but recently I had a request

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.