user516883
user516883

Reputation: 9378

multi threading on asp.net

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

Answers (3)

Andrew Barrett
Andrew Barrett

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:

  • I've worked in a company with an unreasonable attitude to software where we were not allowed to deploy a separate app to handle the background processing. I argued for a windows service, but was overruled and told to implement it in a background thread. Obviously I moved on from that company to a healthier environment, but that is the reality is this you have to deal with unreasonable situations sometimes.
  • if you're in a hosted environment you don't always have the option to offload onto a seperate process.

The question was if it is possible. I'm answering that question.

Upvotes: 8

Adil
Adil

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

Koby Mizrahy
Koby Mizrahy

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

Related Questions