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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:02:44+00:00 2026-05-30T21:02:44+00:00

i have file transfer application (Server-Client) TCP [.net 4] why is backgroundworker gives me

  • 0

i have file transfer application (Server-Client) TCP [.net 4]
why is backgroundworker gives me an exception in (backgroundWorker1_ProgressChanged)

The client send command that has a destination path (where file should saved into) and the size of file to the Server to start receiving that file .. so once the server takes that command .. it will call :

fileTransfer1.Receive(destPath, fileSize);

this method run in its own thread in form1.cs :

private void Job(object o)
    {

        Socket client = (Socket)o;
        NetworkStream stream = new NetworkStream(client);
        StreamReader sr = new StreamReader(stream);

        string cmd = null;

        while ((cmd = sr.ReadLine()) != null)
        {
            string[] command = cmd.Split('<');
            switch (command[0])
            {
               case "receive":
                            fileTransfer1.Receive(command[1], Convert.ToInt64(command[2]));
                            break;
                            default:
                            break;
            }
        }

fileTransfer1 (userControl) :

    public void Receive(string destPath, long fileSize)
    {
            List<object> job = new List<object>();
            job.Add(destPath);
            job.Add(fileSize);
            backgroundWorker1.RunWorkerAsync(job);
    }

    long sum = 0;
    long fileSize = 0;   //this will equal the size of the file later.
    private void ReceiveFile(string destPath, long fileSize)
    {
        using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
        {
            try
            {
                int count = 0;
                data = new byte[packetSize];
                while (sum < fileSize)
                {
                    count = network.Read(data, 0, packetSize);
                    fs.Write(data, 0, count);
                    sum += count;
                    backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
                }
            }
            finally
            {
                CloseTransfer();
            }
        }
    }

here’s backgroundworker method :

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
         List<object> job = (List<object>)e.Argument;
         string destPath = (string)job[1];
         long fileSize = (long)job[2];
         ReceiveFile(destPath, fileSize);
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBarFile.Position = e.ProgressPercentage;
        progressBarFile.Text = progressBarFile.Position.ToString() + "%";
  /*here*/  labelRemaining.Text = CnvrtUnit(fileSize - sum);  
    }

my problem that i got an exception (Cross-thread operation not valid: Control ‘labelRemaining’ accessed from a thread other than the thread it was created on.)
that references to this line:

 labelRemaining.Text = CnvrtUnit(fileSize - sum);

i know about that exception and i know that i have to use (delegate/invoke) thing .. but.. i know that backgroundworker meant for this .. plus .. i have the same code but The server here Send a file to the client .. and it doesn’t give that exception .. it works fine and the form shows me the details as it should be.
So why do i get this exception just when i receive a file ??
note : receive method works fine.

  • 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-30T21:02:46+00:00Added an answer on May 30, 2026 at 9:02 pm

    i’ve figured that out

    since my command receiver method Void Job(object o) is running under a thread .. so this:

    case "receive":
               fileTransfer1.Receive(command[1], Convert.ToInt64(command[2]));
               break;
    

    also runs in the same thread .. that means the rest of the code also runs in that thread ..
    so i just changed this line to this:

    case "receive":
           Action a = () => fileTransfer1.Receive(command[2], Convert.ToInt64(command[3])); 
           Invoke(a);
           break;
    

    now UI-Thread will be notified to run the rest of the receiving code .. which solved my problem.

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

Sidebar

Related Questions

I have a client-server application that uses .net remoting. The clients are in a
I have a server, and Client that are working fine, they transfer file to
I have a file transfer application (server-client) ... while sending a file, i want
I'm making a file transfer application between (client-server) and have the tools I need
I have to develop a file server service that transfer file form a specified
I have a client and server, client sending file to server. When i transfer
im making a Server-Client application.. chat sends commands file-transfer i was wondering if it
I have a large application written using .Net remoting for file transfer. This was
I'm developing a client and a server file transfer application with C++ sockets over
I have a web application in asp.net and C# that must access a remote

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.