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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:01:21+00:00 2026-06-18T04:01:21+00:00

Possible Duplicate: C# Downloader: should I use Threads, BackgroundWorker or ThreadPool? C# , how

  • 0

Possible Duplicate:
C# Downloader: should I use Threads, BackgroundWorker or ThreadPool?
C# , how reach something in current thread that created in other thread?

So I have following code

Downloader.cs

class Downloader
{  
    private WebClient wc = new WebClient();
    public void saveImages(string picUrl, string path)
    {
                this.wc.DownloadFile(picUrl, path + p);
                Form1.Instance.log = picUrl + " is downloaded to folder " + path + ".";
    }
}

Form1.cs / Windows Form

public partial class Form1 : Form
{
    static Form1 instance;
    public static Form1 Instance { get { return instance; } }

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        instance = this;
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        instance = null;
    }
    public string log
    {
        set { logbox.AppendText(value); } // Logbox is Rich Text Box
    }
    private void ThreadJob()
    {
        Downloader r = new Downloader();
        r.saveImages("http://c.org/car.jpg","c:/temp/");
    }
    private void download_Click(object sender, EventArgs e)
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        CheckForIllegalCrossThreadCalls = false;
        thread.Start();
    }
}

I need to get Form1.Instance.log = picUrl + " is downloaded to folder " + path + "."; working without CheckForIllegalCrossThreadCalls set to false because I’ve heard it’s bad way to do things.

PS:: Some of the code is missing but I think the relevant information is there

  • 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-18T04:01:22+00:00Added an answer on June 18, 2026 at 4:01 am

    Rather than having saveImages be a void method and modifying the form, saveImages should return the string value that it computes and allow the form to modify itself:

    public string saveImages(string picUrl, string path)
    {
                this.wc.DownloadFile(picUrl, path + p);
                return picUrl + " is downloaded to folder " + path + ".";
    }
    

    Now what you’re really looking for is a way of performing a long running task in a background thread and updating the UI with the result. The BackgroundWorker class is specifically designed for that purpose, and is much easier to use in a winform application than directly dealing with threads.

    You just need to create a BackgroundWorker, set the work that it needs to do in the DoWork event, and then update the UI in the RunWorkerCompleted event.

    In this case, the DoWork just needs to call saveImages and then set the Result to the return value and then the completed event can add the Result to the rich text box. The BackgroundWorker will ensure that the completed event will be run by the UI thread.

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

Sidebar

Related Questions

Possible Duplicate: How to list my app downloads I created an android app that
Possible Duplicate: What Java FTP client library should I use? I am looking for
Possible Duplicate: Launching other applications in Windows phone 7 Programatically I have created an
Possible Duplicate: Android: HTTP communication should use Accept-Encoding: gzip My Android application downloads temp.bin
Possible Duplicate: What should Linux/Unix 'make install' consist of? I'm making a program that
Possible Duplicate: iPhone apps for company-internal use - possible? As a developer, I can
Possible Duplicate: How to add some non-standard font to a website? I created a
Possible Duplicate: How to use HTML Agility pack I haven't yet downloaded Html Agility
Possible Duplicate: How to install boost c++ libraries in OSX I want to use
Possible Duplicate: Timeout on a Python function call I want to implement that when

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.