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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:05:44+00:00 2026-05-19T05:05:44+00:00

Here is the thing, I want to create a simply app that copy many

  • 0

Here is the thing, I want to create a simply app that copy many files from one site, and move them to another; but using async methods and create a new thread.

private void button3_Click(object sender, RoutedEventArgs e)
{

    //progressBar1.Maximum = _FileInfoArray.Count;

    DispatcherTimer dt1 = new DispatcherTimer();
    foreach (FileInfo Fi in _FileInfoArray)
    {
        Thread t = new Thread(new ThreadStart(delegate()
        {
            DispatcherOperation _dispOp = progressBar1.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(delegate()
            {

                File.Copy(txtdestino.Text, Fi.FullName, true);

                //progressBar1.Value = n;
                //txtstatus.Content = ("Copiados " + n.ToString() + " archivos");
                //Thread.Sleep(100);
            }
            ));
            _dispOp.Completed += new EventHandler(_dispOp_Completed);
        }
            ));
        t.Start();
    }
}

UnauthorizedAccessException is throw! It says that I can’t access to txtdestino content. Some clues?

——————————————————————————-Edited
This is the version with all the changes, get the same error 🙁 any clues?

private void button4_Click(object sender, RoutedEventArgs e)
{
    //First: Build mynames
    List<string> mynames = new List<string>();
    foreach (FileInfo fi in _FileInfoArray)
    {
        mynames.Add(fi.FullName);
    }



    Thread t = new Thread(new ThreadStart(delegate()
       {
          foreach (string fullname in mynames)
            {
            DispatcherOperation _dispOp = progressBar1.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(delegate()
            {
                string destino = System.IO.Path.Combine(@"C:\", System.IO.Path.GetFileName(fullname));
                File.Copy(fullname, destino, true);
                //Some progressbar changes
            }
            ));
            _dispOp.Completed += new EventHandler(_dispOp_Completed);
          }
        }
                ));
        t.Start();
    }

File.Copy(txtdestino.Text, Fi.FullName, true); // here the exception is throw

  • 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-19T05:05:45+00:00Added an answer on May 19, 2026 at 5:05 am

    If multiple threads try to access (simultaneously) the file at txtdestino.Text – isn’t that doomed from the start? You might want to read the contents into memory first and write from there…

    Equally, you are going to hammer the IO; I wonder if a more practical answer (that solves the issue above and below) is to simply do the copies sequentially on the worker.

    It also looks like you might actually be pushing all the work here back to the UI thread anyway…? surely you should do something like:

    string path = txtdestino.Text;
    Thread t = new Thread(new ThreadStart(delegate() {
        foreach (FileInfo Fi in _FileInfoArray) {
            File.Copy(path, Fi.FullName, true);
        }
    }));
    t.Start();
    

    which:

    • avoids the foreach/capture issue (Fi is not captured)
    • reads the path from the UI thread and uses it (captured) on the worker
    • process each file sequentially to avoid hammering IO

    You also have the foreach/capture issue; change it to:

    foreach (FileInfo tmp in _FileInfoArray)
    {
        FileInfo Fi = tmp;
        ...
    

    The problem is that most likely all the threads are trying to access the last file. No, really. This is because foreach technically declares the variable (tmp above) outside the loop; and the variable capture rules (used by lambdas / anon-methods) say that therefore this is the same variable (important: lambdas / anon-methods are full lexical closures, and capture the variable, not the value).

    Re-declaring a variable inside the loop changes the scope, and now the lambda / anon-method treats the variable as different per loop iteration.

    If you really want, I could write it out in something that shows the underlying objects involved, but it depends whether you want that level of detail ;p

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

Sidebar

Related Questions

I have some doubt on wget command. Here is the thing I want to
The thing I want to know here should be really 'basic', but it is
And what if you want to autocomplete passwords? I am using similar thing here...
Here is the thing. Right now I have this e-commerce web site where people
This one very simple thing I can't find the right technique. What I want
I really want to create a stunning-looking GUI desktop application that looks like, for
There is one thing I want to do in PowerPoint VBA. I want to
I want to create a simple ttf font within an iphone/ipad app. The concept
Here is link if you want to download application: Simple banking app Text file
Okay, I think I'm just making a stupid mistake here, but I want to

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.