c# - How to end thread in win CF -
Windows Mobile 5; For compact framework and relative novice C # and thread.
I want to download large files (several meg) from my own website; Getting GPRS may take some time, I want to show a progress bar, and allow an option to cancel the download.
I have found a cloud that the file has been downloaded and make an example of it; Give it a URL and then save the location:
MyFileDownloader By changing + new file downloader. Changing event handler (invoice display); Bg Download = New Thread (New Thread Start (MyFileDownloader.filefile)); BGDownload.Start (); Therefore I am creating an event handler for updating progress bar and starting thread. It works fine.
I have a canceled button that reads:
MyFileDownLoader.Changed - = InvokeProgressBar; MyFileDownLoader.Cancel (); BGDownload.Join (); LblPercentage.Text = CurPercentage + "Canceled"; // CurPercentage is a string lblPercentage.Refresh (); BtnUpdate.Enabled = true; FileDownload contains the main parts of the class:
cancel the cancellation () {CancelRequest = true; } In the method download file:
... success = false; Try {// Loop until no data is returned ((bytes read = ResponseStream.read (buffer, 0, MaxRead)); gt; {_totalBytesRead + = bytesRead; BytesChanged (_totalBytesRead); FileStream.Write ( Break (break, repeat request); if (! CancelRequest) breakthrough = true;} hold {success = false; // other error handling code} Finally {if (empty! = Reactions) Response (); if (zero! = Response) response. Close (); If (null! = FileStream) fileStream.Close ();} // if file If the part was written and the transfer failed, then delete the partial file (if the success and file .xis (destination)) file. Delete (destination); I used to download I am doing the code which is based on
, when I cancel, the download immediately stops, but to complete the process of joining for 5 seconds or so.
If I try again and download it again, sometimes it Works and sometimes I get a nullification exception (even though he is trying to track down).
I think I am doing something wrong to cancel my thread.
What do I do?
cancel public zeros () {CancelRequest = true; } I think you should add thread-safe for this action.
Cancel Public Zero () {lock (this) {CancelRequest = true; }} Hope this helps!
Comments
Post a Comment