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 8885101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:13:38+00:00 2026-06-14T21:13:38+00:00

I have a function which reads each file from a directory and upload it

  • 0

I have a function which reads each file from a directory and upload it to a database.
I cannot work out how to wait for the task to finish before it goes back to the foreach loop, as it seems to do it straight away, where as the task takes a few seconds:

foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
    //load file
    currentReader = new XmlDataReader(transferInstructions, file);

    currentReader.RowsUploaded += new EventHandler<RowsUploadedEventArgs>(currentReader_RowsUploaded);
    currentReader.TableUploaded += new EventHandler<TableUploadedEventArgs>(currentReader_TableUploaded);
    currentTask = new Task(() => currentReader.executeBulkCopy(initialConnString, workingDatabase));
    currentTask.ContinueWith(task =>
    {
        cleanUp(task);
        //MessageBox.Show("Complete!");
    });
    currentTask.Start();
    writeResult("Started the transfer process.");
    cmdDataTransfer.Text = "CANCEL TRANSFER";
    cmdDataTransfer.ForeColor = Color.DarkRed;
    transferAction = () => cancelCurrentReader();   
}

I need to wait for where the MessageBox.show would be before it continues the foreach loop.
It takes a few seconds to get to the
cleanUp(task);
//MessageBox.Show(“Complete!”);
section.

Thanks.

  • 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-14T21:13:39+00:00Added an answer on June 14, 2026 at 9:13 pm

    The async-awqait pattern in C# 5 / .NET 4.5 is a perfect match for this. I see you’ve tagged this as .NET4, but if you can use the Async Targeting Pack, there is a very elegant way of doing this:

    void Main()
    {
        foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
        {
            currentReader = new XmlDataReader(transferInstructions, file); //load file
            currentReader.RowsUploaded += new EventHandler<RowsUploadedEventArgs>(currentReader_RowsUploaded);
            currentReader.TableUploaded += new EventHandler<TableUploadedEventArgs>(currentReader_TableUploaded);
    
            var task = Task.Factory.StartNew(() => currentReader.executeBulkCopy(initialConnString, workingDatabase));
            await task;
    
            cleanUp(task);
            MessageBox.Show("Complete!");
            writeResult("Started the transfer process.");
            cmdDataTransfer.Text = "CANCEL TRANSFER";
            cmdDataTransfer.ForeColor = Color.DarkRed;
            transferAction = () => cancelCurrentReader();   
        }
    }
    

    If you have to keep it VS2010, you’ll have to emulate what async-await does, something along the lines of:

    void MyForm()
    {
        _syncContext = SynchronizationContext.Current;
        Execute(Directory.EnumerateFiles(folderPath, "*.xml").GetEnumerator());
    }
    
    void Execute(IEnumerator<string> files)
    {
        if (!files.MoveNext())
        {
            files.Dispose();
            return;
        }
        Task.Factory.StartNew(() => Execute(files.Current)).ContinueWith(() => Execute(files));
    }
    
    public void Execute(string file)
    {
        currentReader = new XmlDataReader(transferInstructions, file); //load file
        currentReader.RowsUploaded += new EventHandler<RowsUploadedEventArgs>(currentReader_RowsUploaded);
        currentReader.TableUploaded += new EventHandler<TableUploadedEventArgs>(currentReader_TableUploaded);
        () => currentReader.executeBulkCopy(initialConnString, workingDatabase);
        cleanUp(task);
        _syncContext.Send(updateGUI); 
        transferAction = () => cancelCurrentReader(); 
    }
    public void updateGUI()
    {
        MessageBox.Show("Complete!");
        writeResult("Started the transfer process.");
        cmdDataTransfer.Text = "CANCEL TRANSFER";
        cmdDataTransfer.ForeColor = Color.DarkRed;
    }
    

    EDIT Now that I think about it there’s also a much simpler way. You can have the entire loop running in a task of its own (delegating to the synchronization context for GUI work). Using the conventions of the code above:

    Task.Factory.StartNew(() => 
    {
        foreach (var file in Directory.EnumerateFiles(folderPath, "*.xml"))
            Execute(file);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function TRANSLATE which reads value from the xml file based on
I have a function which first reads an image from disk, resizes it and
I have a function(please see code below) which reads some data from the web.
I have a jQuery XML file parser that I got from somewhere which reads
Have the following line of code, which reads pairs of words from a file,
I have a program which reads an XML file using the DOM functions: $doc
I am baffled. I have a ready function, from jQuery, which sets an HTML5
hi i have function which is called by tinker listbox so i cannot return
In a function that reads data (data meaning exclusively strings) from disk, which should
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >

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.