1

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}
1

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems like a harsh downvote? Is it possible: yes, but you should probably use something else and if you do be careful about unhandled exceptions.
0

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.

4 Comments

Rather than continuously check you could just message the service when it's ready as well.
Yes through inter process call isn't it?
One of the easiest ways is calling a WCF service hosted in the windows service.
Right or we can use MSMQ that is simple as well, anyways thanks for info.
-1

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.

2 Comments

One warning about using background processing on ASP.NET sites. IIS can recycle the application at any given moment and will kill any running background thread while doing this. So there is no guarantee that your thread pool threads will run succesfully. If that's a problem, it is better to use a Windows service to do the background processing.
Before the application pool is recycled your application will receive an event whcih can be captured using Global.asax Application_End method. In this method you can do necessary checks and deal with running threads (your code will get little bit more complicated since you will need to communicate with running threads.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.