Avatar of starusa
starusa

asked on 

Thread lifecycle

Hey,

I'm working on an application to scan receipts. When the receipt is saved, It has to be uploaded to a ftp server. I'm using Webclient, and it woked fine, but I don't want the user to wait for the file to be uploaded. I'm considering using a thread, so the upload would run on the background.

My doubt is about the time that thread will be running. When the file is uploaded, the thread is ended? How to manage the memory in that case? The user who scans, does that a thousand times per day, so, I want to be sure about the memory usage by the threads.

Thanks!
ASP.NET

Avatar of undefined
Last Comment
Christopher Kile
Avatar of Christopher Kile
Christopher Kile
Flag of United States of America image

You detect when the upload is complete, and then you close the thread, releasing any resources used.  A better idea woud be to create a queue of receipts (say, in a database table) with a completely different process consuming this queue and uploading receipts to your receipt server.
Avatar of strickdd
strickdd
Flag of United States of America image

What you want to do is create an event that is raised when the thread is finished. then have an event handler that will stop the thread and clear up the memory.
Avatar of Banzai10
Banzai10

When you start a thread you can define the CallBack Event that is called when the work stop

for any reason, so on the callback event you release the resources of the thread, OR

if you don't want to manage you can use the technique "Fire and Forget" so you just call the thread

and the Framewrok release it for you when it finishes,

The best reference I have ever seen before about threads is on this site:
http://www.albahari.com/threading/

Any questions I am here to help you.

Regards, Felipe Lopes
ASKER CERTIFIED SOLUTION
Avatar of Christopher Kile
Christopher Kile
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of starusa
starusa

ASKER

That's true. I read the webclient documentation carefully and found the UploadFileAsync. It does automatically what I was trying to figure out.

Thanks!
Avatar of starusa
starusa

ASKER

Actually, now I'm looking for how to get the responses from server.

In this mean time, if anybody knows, please let me know!

Thanks.
If you plan to use UploadDataAsync, consult the documentation on the UploadDataCompletedEventArgs object (passed as a parameter to the UploadDataComplete event):

http://msdn2.microsoft.com/en-us/library/system.net.uploaddatacompletedeventargs_members.aspx

If you plan to use UploadFileAsync, consult the documentation on the UploadFileCompletedEventArgs object (passed as a parameter to the UploadFileComplete event):

http://msdn2.microsoft.com/en-us/library/system.net.uploadfilecompletedeventargs.aspx

You'll notice members such as Error and Result.  I can't tell you what you'll find in them, but I expect you'll get something you can use to verify success or failure and to help diagnose failure.

Good luck, and good hunting :)
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo