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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:47:09+00:00 2026-05-25T10:47:09+00:00

Windows mobile 5; compact framework and relative newbie to c# and threads. I want

  • 0

Windows mobile 5; compact framework and relative newbie to c# and threads.

I want to download large files (several meg) from my own website; being GPRS this could take a while. I want to show a progress bar, and allow an option to cancel the download.

I’ve got a class called FileDownload and create an instance of it; give it a url and save location then:

MyFileDownLoader.Changed += new FileDownLoader.ChangedEventHandler(InvokeProgressBar);

BGDownload = new Thread(new ThreadStart(MyFileDownLoader.DownloadFile));
BGDownload.Start();

So I create an event handler for updates to progress bar and start the thread. This works fine.

I’ve got a cancel button which reads:

MyFileDownLoader.Changed -= InvokeProgressBar;
MyFileDownLoader.Cancel();
BGDownload.Join();
lblPercentage.Text = CurPercentage + " Cancelled"; // CurPercentage is a string
lblPercentage.Refresh();
btnUpdate.Enabled = true;

In the FileDownload class the key parts are:

public void Cancel()
{
    CancelRequest = true;
}

In method Download file:

...
success = false;
try {
//loop until no data is returned
while ((bytesRead = responseStream.Read(buffer, 0, maxRead)) > 0)
{
    _totalBytesRead += bytesRead;
    BytesChanged(_totalBytesRead);
    fileStream.Write(buffer, 0, bytesRead);
    if (CancelRequest)
       break;
}

if (!CancelRequest)
    success = true;
}
catch
{
    success = false;
    // other error handling code
}
finally
{
    if (null != responseStream)
        responseStream.Close();
    if (null != response)
        response.Close();
    if (null != fileStream)
        fileStream.Close();
}

// if part of the file was written and the transfer failed, delete the partial file
if (!success && File.Exists(destination))
    File.Delete(destination);

The code i’m using for the download is based on http://spitzkoff.com/craig/?p=24

The problem i’ve got is when I cancel, the download stops immediately, however it can take up to 5 seconds or so for the join process to complete. This is evidenced by lblPercentage.Text being updated after the join.

If I then try and download again, sometimes it works and sometimes I get a nullreference exception (still trying to track that down).

I think i’m doing something wrong in my approach to cancelling the thread.

Am i ?

  • 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-25T10:47:09+00:00Added an answer on May 25, 2026 at 10:47 am
    public void Cancel()
        {
            CancelRequest = true;
        }
    

    I suppose you should add thread-safe to this action.

    public void Cancel()
            {
                lock (this)
                {
                    CancelRequest = true;
                }
            }
    

    Hope this help!

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

Sidebar

Related Questions

I want to use delayed signing for my windows mobile compact framework assemblies. When
i want to get the running process in .net compact framework windows mobile.. i
I will start new PDA project on the windows mobile and compact framework 2.0
Does .NET Compact Framework 3.5 work on Windows Mobile 2003 SE without limitations?
My requirements: Support .NET Compact Framework 2.0 and Windows Mobile 6.0 devices. Only need
We work extensively in the .Net Compact Framework and Windows Mobile. I've seen plenty
My details: custom mobile device running Windows CE 4.2, Compact Framework 2.0 SP1. C#
I have a .Net Compact app running on Windows Mobile, and I want to
I have a windows mobile 5 program (compact framework 3.5) that I need to
I'm developing a compact-framework application for a Windows Mobile device to work with SQLServer

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.