Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.NET and JavaScript timeout interval problem??

Options
  • 22-02-2005 10:23am
    #1
    Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭


    Ok, i have an asp.net page that has a simple animated status bar gif on it, it is waiting for a PDF file to be created in a directory. What i want it to do is to poll that directory every 10 seconds until it finds the PDF file and then open the PDF file.

    Everything works except the polling, without the timeout, i can get the page to reload over and over and over which looks horrible because it is flickering and the status bar animated gif isnt displayed long enough to animate. the page refreshes about 6 times a second, at least thats what it looks like.

    My reload code is in java script created at run time in the ReloadPage method at the bottom of this block of code, ive tried putting in a setTimeout call but that doesnt work, its commented out, if it is in, my animated gif displays but the page doesnt reload even after the timeout expires, if i leave it out, the page does what its supposed to but wont display the animated gif because it is reloading itself so quickly and it just flickers.

    Any suggestions are welcome!
    public class ShowPDF : System.Web.UI.Page
    	{
    		protected System.Web.UI.WebControls.Label Label1;
    
    		private string pdfName;
    
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			string firstTime = Request.QueryString["firsttime"].ToString();
    			pdfName = Request.QueryString["pdfname"].ToString();
    
    			if (firstTime == "Y")
    				ReloadPage("N");
    			else
    				CheckForPDF();
    		}
    
    		private void CheckForPDF()
    		{
    			try
    			{
    				string pdf = ConfigurationSettings.AppSettings["PDFFiles"].ToString() + pdfName;
    
    				if (File.Exists(pdf)) 
    					Response.Redirect(pdf);
    				else
    					ReloadPage("N");
    			}
    			catch(Exception ex)
    			{
    				Debug.Write(ex.Message) ;
    			}
    		}
    
    		private void ReloadPage(string firstTime)
    		{
    			string link = "ShowPDF.aspx?firsttime=" + firstTime + "&pdfname=" + pdfName;
    
    			string strScript = ("<script>");
    			
    			
    			strScript += ("window.location.href = \"" + link + "\"" + ";");
    
    			//try and add a 5 second delay to the javascript before it does the reload
    			//strScript += ("setTimeout(\"window.location.href = '" + link + "'\", 1000)");
    
    			strScript += "</" + "script>";
    
    			RegisterStartupScript("redirect", strScript);
    		}
    
    	}
    


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Just quickly off the top of my head could you do something like this:

    private void CheckForPDF()
    {
    	try
    	{
    		string pdf = ConfigurationSettings.AppSettings["PDFFiles"].ToString() + pdfName;
    		long i = 0;
    
    		while (!File.AppendText(pdf))
    		{
    			i++;
    			// Add code to check for a timeout here
    		}
    		
    	}
    	catch(Exception ex)
    	{
    		Debug.Write(ex.Message) ;
    	}
    }
    

    This way there's no need to reload the page, your just waiting for the file to be dropped into the folder. Obviously you don't want a infinite loop so you include some kind of timeout code. I'm sure there's a better way to poll a folder in .Net I just don't have the time to look it up. We'll see what other people say.

    I'm taking it that you're application isn't creating this pdf file in another thread and that's its coming from an external process.


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    Thanks phil,

    yeah PDF creation is happening in adobe central, i create a .dat file in .NET that adobe picks up and then from the .dat file it processes the .pdf and drops it back into the directory im polling. ill try this now and see if it works.

    If anyone else has any suggestions, they are appreciated.

    cheers


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Actually, if you're adding timeout checking code to the while loop you won't need i++; I only put it in there to avoid an empty loop.


Advertisement