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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:53:08+00:00 2026-05-12T06:53:08+00:00

I’m starting to implement a simple daemon that basically fetches a file from an

  • 0

I’m starting to implement a simple daemon that basically fetches a file from an FTP location with the help of a BackgroundWorker component to kind of guarantee sort of thread safety there. Although I sort of feel I’m heading towards the right direction, I’m not completely familiar with the technologies involved, therefore not quite at ease with the whole application life cycle, say there’s scenarios that I definitely don’t know how to handle yet, namely, what to do with the stop event, what would happen if the service gets stopped while the worker is running, et cétera. I guess the following piece of code barely represents what I’m trying to achieve:

#region Daemon Events
protected override void OnStart(string[] args)
{
    this.transferBackgroundWorker.RunWorkerAsync();
}

protected override void OnStop()
{
            this.transferBackgroundWorker.CancelAsync(); // Thanks Wolfwyrd!
}
#endregion

#region BackgroundWorker Events
    private void transferBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        worker.WorkerSupportsCancellation = true;

        #region FTP Download

        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(Daemon.FTP_HOST);
        ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
        ftpRequest.Credentials = new NetworkCredential(Daemon.FTP_USER, Daemon.FTP_PASS);
        FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

        using(Stream ftpResponseStream = ftpResponse.GetResponseStream())
        {
            using (StreamWriter sw = File.CreateText(FILE_NAME))
            {
                sw.WriteLine(ftpResponseStream);
                sw.Close();
            }
            ftpResponse.Close();
        }
        #endregion

        e.Result = "DEBUG: Download complete" + ftpResponse.StatusDescription;
    }

private void transferBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
    {
        EventLog.WriteEntry("Exception caught: " + e.Error.Message);
    }
    else
    {
        EventLog.WriteEntry(e.Result.ToString());
    }
}
#endregion

Any and every suggestion would be really appreciated. Thanks much in advance for the assistance.

Edit: just reimplemented the FTP file fetching.

  • 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-12T06:53:08+00:00Added an answer on May 12, 2026 at 6:53 am

    A BackgroundWorker works by spinning off some task onto a separate background thread independent from the calling application. The lifecycle of the background worker follows the pattern of:

    • Create the Background Worker (i.e. BackgroundWorker bgw = new …)
    • Call bgw.RunWorkerAsync A seperate background thread is started by the BackgroundWorker and the DoWork method is invoked on this thread
    • Optional notification events are raised, these are raised by calling bgw.ReportProgress() and are subscribed to (and acted upon) in the BackgroundWorkers parent thread
    • Optional cancellation may occur, this happens when bgw.CancelAsync() is called. This call may come from any thread with access to the BackgroundWorker object. It is up to you to honour the cancellation by checking on the CancellationPending property in your DoWork method
    • The BackgroundWorker DoWork method finishes either through natural completion or due to abnormal termination (i.e. an unhandled exception occurs in the DoWork method) The – RunWorkerCompleted method is invoked

    To offer advice on your original question, it seems that what you want to do is flag the backgroundworker as supporting cancellation (bgw.WorkerSupportsCancellation = true) and have your stop event call the CancelAsync() method on the BackgroundWorker. Update your DoWork method to poll the “CancellationPending” property at suitable junctures and perform the cancellation when it occurs leaving your service in a known state.

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

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Makefile must support this feature. I think the autoconf… May 12, 2026 at 9:14 am
  • Editorial Team
    Editorial Team added an answer There are two ways around this. 1) introduce the necessary… May 12, 2026 at 9:14 am
  • Editorial Team
    Editorial Team added an answer This regular expression should do it: ^http://domain2\.com/ And converted into… May 12, 2026 at 9:14 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.