Reputation: 9378
I have not used multi threading much for asp.net. I have a web application that uploads a large temp file to folder. I would like to take that temp file and do some other things with it after it is uploaded. Can I do this work on another thread without the user being on the website any more? Thanks for any help or suggestions.
1.User post large file 2.uploading temp to server 3.After upload completes. I would like to run another thread/worker that can run without any user iteration but is trigger by the user.
void uploading(){
//Uploading file To server
}
void Submitclick(){
Start a Thread
Thread thread = new Thread(DoThreadWork);// does the user still need to be logged in?
Send to another page
}
void DoThreadWork(){Do this in background}
Upvotes: 1
Views: 5510
Reputation: 19911
It's definitely possible, I've used background threads quite a bit in ASP.NET to do some funky stuff. If you have complete control over the server it might be more elegant to run the background code in a separate application or a windows service.
It's a better separation of concerns to have your IIS app dealing with just responding to web requests, and it's not geared up for that.
Also a warning, if you have a background thread in ASP.NET 2.0 and it has an unhandled exception, the default is to reset the application pool.
More information here: http://blogs.msdn.com/b/tess/archive/2006/04/27/584927.aspx
// 3 downvotes?
Listen, it's not alway possible to avoid running something in a background thread. I've hit this in several situations:
The question was if it is possible. I'm answering that question.
Upvotes: 8
Reputation: 148110
If you want to separate the uploading of file from website user interaction you can make a windows service that will contineously check that if file is ready for upload and upload the file.
Upvotes: 0
Reputation: 1371
You can use the thread pool for that. The sample code relies on the article in the following lik: http://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx
First write your method that does the work. The method must get 1 argument of type object:
public void DoWork(object threadContext)
{
}
than, in the place of the code that you want to call the method do:
...
var threadParam = ... // Put any value you want to be used by the DoWork method
ThreadPool.QueueUserWorkItem(DoWork, threadParam );
The method will be queued until the system will hav free thread to handle the work and execute the method regardless if the request has been ended or not.
Upvotes: -1