Problem
I’m polling an ashx handler to check if a specific file is present and return true or false depending on it.
public void ProcessRequest(HttpContext context)
{
int myId;
if (context.IsRequestClean())
{
if (int.TryParse(context.Request["id"], out myId))
{
var directory = Directory.GetDirectories(context.Server.MapPath("~/App_Data/Files"), "*" + myId, SearchOption.TopDirectoryOnly);
if (directory.Length > 0)
{
if (Convert.ToInt32(directory[0][directory[0].Length - 1].ToString()) == myId)
{
context.Response.ContentType = "text/plain";
context.Response.Write("true");
}
}
}
}
}
How can I speed the above up?
Solution
If you want a shorter time between file is created until the client is notified, I recomment you implement long-running queries, for example using SignalR.
Then setup a FileSystemWatcher that notifies the query as soon as the file is present.