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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:57:56+00:00 2026-05-23T23:57:56+00:00

I have a C# app with a button for dragging and dropping files. I

  • 0

I have a C# app with a button for dragging and dropping files. I am able to take 6 files from my desktop and drop it onto the button and have it process those 6 files.

However, when I start a thread from the DragDrop event and pass the file path to a new thread started from within the DragDrop event, the file path is incorrect once the thread receives the FilePath parameter.

If I execute my code by dragging 6 text files onto my button (I had to strip a lot of code out of it for this example), I will see the following in my console:

++ Calling testthread with these params: false, TestButton,test.txt,c:\test.txt
++ Calling testthread with these params: false, TestButton,test2.txt,c:\test2.txt
++ Calling testthread with these params: false, TestButton,test3.txt,c:\test3.txt
++ Calling testthread with these params: false, TestButton,test4.txt,c:\test4.txt
++ Calling testthread with these params: false, TestButton,test5.txt,c:\test5.txt
++ Calling testthread with these params: false, TestButton,test6.txt,c:\test6.txt

The above output is correct

The following output is incorrect, notice the FilePath does not match the CleanFileName like it does in the above console output.

++ testthread Thread – CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test.txt FilePath = c:\test2.txt
++ testthread Thread – CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test1.txt FilePath = c:\test3.txt
++ testthread Thread – CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test3.txt FilePath = c:\test4.txt
++ testthread Thread – CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test4.txt FilePath = c:\test5.txt
++ testthread Thread – CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test5.txt FilePath = c:\test5.txt
++ testthread Thread – CallingfromPendingUploads == false ButtonName == TestButton CleanFileName == test6.txt FilePath = c:\test5.txt

As you can see, the FilePath from thread does not match the FilePath that is being passed to the Thread before it starts. All of the FilePaths are off when compared to the filename that is passed to the Thread. And some of the FilePaths are duplicates such as text5.txt.

I have been struggling with this for hours. Can someone please tell me what I am doing wrong?

private void btnClick_DragDrop(object sender, DragEventArgs e)
{
    string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);

    string ButtonName = "TestButton"

    string[] files = new string[10];

    files = (string[])e.Data.GetData(DataFormats.FileDrop);


    foreach (string file in files)
    {
        FileInfo fileInfo = new FileInfo(file);

        Console.WriteLine("++  Filename: " + fileInfo.Name + "   Date of file: " + fileInfo.CreationTime + "   Type of file: " + fileInfo.Extension + "   Size of file: " + fileInfo.Length.ToString());

        string CleanFileName = System.Web.HttpUtility.UrlEncode(fileInfo.Name.ToString());

        //Start  thread
        try
        {
            Console.WriteLine("++ Calling testthread with these params: false, " + ButtonName + "," + CleanFileName + "," + file);

            new Thread(() => testthread(false, ButtonName, CleanFileName, file)).Start();

            Console.WriteLine("++ testthead thread started @ " + DateTime.Now);
         }
         catch (Exception ipwse)
         {
             logger.Debug(ipwse.Message + " " + ipwse.StackTrace);
         }
    }
}

public void testthread(bool CalledfromPendingUploads, string ButtonName, string CleanFileName, string FilePath)
{
    Console.WriteLine("++ testthread Thread - CallingfromPendingUploads == " + CalledfromPendingUploads.ToString() + "  ButtonName == " + ButtonName + "  CleanFileName == " + CleanFileName + "  FilePath = " + FilePath);
}
  • 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-23T23:57:56+00:00Added an answer on May 23, 2026 at 11:57 pm

    All of your threads are sharing the same file variable.
    If one of the threads only starts running after the UI thread has started the next iteration, it will use the next value of the file variable.

    You need to declare a separate variable inside the loop, so that each thread will get its own variable.

    For example:

    foreach (string dontUse in files)
    {
        string file = dontUse;
        ...
    }
    

    Since the file variable is now scoped within the loop, each iteration gets a separate variable.

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

Sidebar

Related Questions

I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
Hello StackOverflow'ers, I have a (flex) app that, on the click of a button,
I'm now testing GKPeerPickerController. I have a simple app that just have a button
I have a button that launches the google maps app on my device via
I have an app widget which runs neatly. However, I am unable to highlight
I have an app with a button 'convert'. When I click this button, the
I would like to have some of the controls in an app - Button
I have an app that allows you to add items to a list, (From
I need to do this: I have an app with a button, I wish,
I have an app that I want to have a button that will show

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.