win CF中如何结束线程

发布于 2024-12-02 11:18:32 字数 1866 浏览 0 评论 0原文

Windows 移动 5;紧凑的框架和 C# 和线程的相对新手。

我想从我自己的网站下载大文件(几兆);如果是 GPRS,这可能需要一段时间。我想显示一个进度条,并允许选择取消下载。

我有一个名为 FileDownload 的类并创建它的一个实例;给它一个 url 并保存位置,然后:

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

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

所以我创建一个事件处理程序来更新进度条并启动线程。这很好用。

我有一个取消按钮,上面写着:

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

在 FileDownload 类中,关键部分是:

public void Cancel()
{
    CancelRequest = true;
}

在方法下载文件中:

...
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);

我用于下载的代码基于 http://spitzkoff.com/craig/?p=24

我遇到的问题是当我取消时,下载立即停止,但是可能需要最多 5 秒左右加入过程完成。连接后更新的 lblPercentage.Text 证明了这一点。

如果我然后尝试再次下载,有时它会起作用,有时我会收到空​​引用异常(仍在尝试追踪该异常)。

我认为我取消线程的方法做错了。

我是吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-12-09 11:18:32
public void Cancel()
    {
        CancelRequest = true;
    }

我想你应该为此操作添加线程安全。

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

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文