Non-locking sleep/waitfor/delay function for ASP.NET - asp.net

I am writing an ASP.NET class that interfaces with an external application. The flow of the transaction between the web server and this application is as follows:
My object writes a file to a directory.
The outside application detects this file and processes it. This can take between 1-5 seconds.
The outside application writes a response file to the same directory.
My object detects the response file and parses the results.
The 1-5 seconds it can take for the external application to process my file is my problem. The most straightforward way to wait for the file seems to be something like this:
Do While Not File.Exists(f)
Thread.Sleep(500)
Loop
Of course, Thread.Sleep() completely locks up the rest of my website until the outside application processes the file. Clearly, this is not a workable solution.
How can I effectively "wait" for my file to be processed without locking up the rest of my website?

Use the FileSystemWatcher - when a file will be created it will fire an event that you can subscribe to.

Related

When exactly are the files transmitted to the server?

If I have a servlet that handles file uploads, when does the server actually receive the files?
Are they already available when I invoke HttpServletRequest::getParts or will the individual files only be fetched when I actually call the corresponding Part::write?
Asked differently, is it my servlet's responsibility to implement a parallel upstream of all files or do I just need to worry about writing them all to disk?
Are they already available when I invoke HttpServletRequest::getParts
Yes.
or will the individual files only be fetched when I actually call the corresponding Part::write?
Technically, files are not fetched. The server doesn't actually "download" files from the client. It's the client who sends the files as part of the request body to the server and the server has just to listen for these files and write them to a temporary storage before invoking the service method. The server will only invoke the service method when the request body is fully read. This is regardless of the request body content type. Hence the "Yes" on the previous question.
Asked differently, is it my servlet's responsibility to implement a parallel upstream of all files
Absolutely not. You don't at all need to synchronize anything on the HttpServletRequest. At most only on the HttpSession, but even this doesn't have a role here.
or do I just need to worry about writing them all to disk?
Yes, exactly that. Simply grab the Part, validate it and finally write it.
See also:
Recommended way to save uploaded files in a servlet application

Calling method Asyncronously from aspx

i am using .net4.0 framework
i am working on Web Project using c#
i want to make some request which will populate some items in cache beforehand.
Ex.
user comes to the home page of application.
[ i will write some code in Home Page(.ASPX) which will call some method which will read some files from disk and put them into cache though i don't need this cache on home page... at this point as reading files from disk is going to be somewhat lengthy operation... i would to load home page completely without waiting for response from method(which is going to read files from disk)]
Something like
function page_load{
CacheGenerator.CreateCache();//this is going to be heavy operation..i don't want to wait for this operation to complete
repose.write("Hello world:); // this statement should run moment after above function is called
}
and one more thing can we do this without using threading.
Anything asynchronous is "using threading" in one way or another.
More importantly, you should not start any async operation from a page. The page only exists in memory for the duration of the request. Your async operation may complete after the page no longer exists in memory.
You may be able to use the Task class to start an async operation from the Application_Start event in global.asax.

Console Application or WebService

I am working on a functionality that will generate around 2000 excel sheets. THe process will accept input from front end web application. I dont want my web application to wait till the process of generating the excel files is over. I should write a console application and trigger it through web application or a web service? Whic is the better way of implementation?
Thanks,
Rohit
I would suggest storing excel-gen as a task in a database, then have a background service constantly running, checking that task queue, processing it (creating spreadsheets), then finally updating job records as done. Your asp.net app can in turn, aside from creating job records, simply check on status periodically or on page-refresh.

Invoke asynchronous method call from ASP.NET

I'm currently working on a web site which involves a data upload process. The file is currently uploaded to the server method is called (in app_code) where ultimately a DTS package is called (via a web method) to load the data into a database and perform some validation on it.
The client has specified that they don't want to have to wait for the DTS package to execute (execution time is less than 5 minutes) so it appears that I need to call the method asynchronously. The user will probably logout or close the browser window while this task is running so I believe I'm unable to run this on an asp.net thread.
Can anyone give me some guidance as to the best way to proceed on this?
Actually, all you need to do is launch a thread or a method invoker and it will operate as intended, even if the users session end.
We use this same ability to manage very large data processing tasks in our web based CMS.

Calling a method in an ASP.NET application from a Windows application

Other than using a web service, is there anyway to call a method in a web app from a windows application? Both run on the same machine.
I basically want to schedule a job to run a windows app which updates some file (for a bayesian spam filter), then I want to notify the web app to reload that file.
I know this can be done in other ways but I'm curious to know whether it's possible anyway.
You can make your windows app connect to the web app and do a GET in a page that responds by reloading your file, I don't think it is strictly necessary to use a web service. This way you can also make it happen from a web browser.
A Web Service is the "right" way if you want them to communicate directly. However, I've found it easier in some situations to coordinate via database records. For example, my web app has bulk email capability. To make it work, the web app just leaves a database record behind specifying the email to be sent. The WinApp scans periodically for these records and, when it finds one with an "unprocessed" status, it takes the appropriate action. This works like a charm for me in a very high volume environment.
You cannot quite do this in the other direction only because web apps don't generally sit around in a timing loop (there are ways around this but they aren't worth the effort). Thus, you'll require some type of initiating action to let the web app know when to reload the file. To do this, you could use the following code to do a GET on a page:
WebRequest wrContent = WebRequest.Create("http://www.yourUrl.com/yourpage.aspx");
Stream objStream = wrContent.GetResponse().GetResponseStream();
// I don't think you'll need the stream Reader but I include it for completeness
StreamReader objStreamReader = new StreamReader(objStream);
You'll then reload the file in the PageLoad method whenever this page is opened.
How is the web application loading the file? If you were using a dependency on the Cache object, then simply updating the file will invalidate the Cache entry, causing your code to reload that entry when it is found to be null (or based on the "invalidated" event).
Otherwise, I don't know how you would notify the application to update the file.
An ASP.NET application only exists as an instance to serve a request. This is why web services are an easy way to handle this - the application has been instantiated to serve the service request. If you could be sure the instance existed and got a handle to it, you could use remoting. But without having a concrete handle to an instance of the application, you can't invoke the method directly.
There's plenty of other ways to communicate. You could use a database or some other kind of list which both applications poll and update periodically. There are plenty of asynchronous MQ solutions out there.
So you'll create a page in your webapp specifically for this purpose. Use a Get request and pass in a url parameter. Then in the page_load event check for this paremter. if it exists then do your processing. By passing in the parameter you'll prevent accidental page loads that will cause the file to be uploaded and processed when you don't want it to be.
From the windows app make the url Get request by using the .Net HttpWebRequest. Example here: http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx

Resources