Results 1 to 11 of 11

Thread: Create an image on the server every 5 minutes

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Create an image on the server every 5 minutes

    Hi,

    I need to create an image from some data in my database on the server every 5 minutes. The image path is Images/top5.jpg, so that users can place a link to that imagine on some forum or whatever and it will update every 5 minutes.

    I am doing this by using a System.Threading.Timer in Global.asax:
    vb.net Code:
    1. Public Class Global_asax
    2.     Inherits System.Web.HttpApplication
    3.  
    4.     Dim path As String
    5.     Dim timer As System.Threading.Timer
    6.  
    7.     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    8.         ' Fires when the application is started
    9.  
    10.         path = Server.MapPath("~/Images/top5.jpg")
    11.         timer = New System.Threading.Timer(AddressOf TimerTick)
    12.         timer.Change(1000, 300000)
    13.     End Sub
    14.  
    15.     Sub TimerTick(ByVal status As Object)
    16.         Try
    17.             Top5Image.UpdateTop5(path)
    18.         Catch ex As Exception
    19.             Debug.WriteLine(ex)
    20.         End Try
    21.     End Sub
    22.  
    23. End Class
    The Top5Image.UpdateTop5 method will create and save the image.


    Locally, this seems to work OK. The image is created every 5 minutes in the Images folder.

    I uploaded the website, but it does not seem to work on the server. What could be causing this?

    I thought maybe I don't have write permission on the server or something. What I tried:
    - Placed a random image on the server just so one is there already (maybe I could change an image but not create a new one?)
    - Gave Write permissions to the Image folder and the image in there.

    Doesn't seem to work, it's now just displaying the random image but it should have generated the new image already.


    As a side question: how can I debug stuff like this? I am using a Try/Catch block but I'm finding it well... impossible to see any exception. Debug.WriteLine works fine in the IDE but now that it's on the server I've no idea how I could view the error, if any.

    Thanks!

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Create an image on the server every 5 minutes

    This doesn't seem like a task you would want to put in a web site. Seems like a background process like a service or a scheduled task would be more appropriate. But I guess it depends how much access you have to the server too.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create an image on the server every 5 minutes

    I agree, but I have no idea how to create a service or scheduled task on the server. I doubt I even can, it's not like I have the server in my basement, I rented it.

    By the way, it seems to work now suddenly... It took maybe 10 minutes but then suddenly the image popped up. That's strange because
    1) The delay of the timer is only 5 minutes
    2) The timer should fire 1 second after it's created, so the image should have popped up almost immediately after restarting the server.

    So it seems to work, but with a delay.. :/ Again this is hard to test for me, because the image will probably not change (as the data will probably not change anymore) and even if it did I would have to notice that within 5 minutes of the data changing to be able to test it...

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Create an image on the server every 5 minutes

    Quote Originally Posted by NickThissen View Post
    I agree, but I have no idea how to create a service or scheduled task on the server. I doubt I even can, it's not like I have the server in my basement, I rented it.

    By the way, it seems to work now suddenly... It took maybe 10 minutes but then suddenly the image popped up. That's strange because
    1) The delay of the timer is only 5 minutes
    2) The timer should fire 1 second after it's created, so the image should have popped up almost immediately after restarting the server.

    So it seems to work, but with a delay.. :/ Again this is hard to test for me, because the image will probably not change (as the data will probably not change anymore) and even if it did I would have to notice that within 5 minutes of the data changing to be able to test it...
    Running it on a web page in general I think will lead to unusual results as to when the timer fires.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create an image on the server every 5 minutes

    I'm running the timer in the Global.asax file. I've read that that was the most appropriate place, and the System.Threading.Timer was the most appropriate timer to use (contrary to the two other times that I know of). Is it really that much of a problem?

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Create an image on the server every 5 minutes

    Quote Originally Posted by NickThissen View Post
    As a side question: how can I debug stuff like this? I am using a Try/Catch block but I'm finding it well... impossible to see any exception. Debug.WriteLine works fine in the IDE but now that it's on the server I've no idea how I could view the error, if any.
    You could use Trace, and then enable Trace on your web page/application.

    The other alternative would be to use logging, you can easily add logging to your web application using something like log4net.

    Gary

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Create an image on the server every 5 minutes

    Quote Originally Posted by NickThissen View Post
    I'm running the timer in the Global.asax file. I've read that that was the most appropriate place, and the System.Threading.Timer was the most appropriate timer to use (contrary to the two other times that I know of). Is it really that much of a problem?
    I have always tended to shy away from timers in my web applications, as this sort of breaks the web paradigm, about being stateless. In your situation, global.asax would be the "best" place to do this work, although, you are at risk of the application pool being recycled, and things getting "messed" up.

    An alternative solution would be to put the necessary work in a web service method, and then from another machine, or the server you are running it on, call the web service method and the required interval. Most hosting companies allow you to set this up, although, you might need to pay a little extra.

    Gary

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Create an image on the server every 5 minutes

    Thanks. I am still using the timer, and it hasn't stopped working yet. It's not a critical part of my website, just an image that shows the top-5 drivers in a formula one game competition. I think I'll just keep it this way, but if it ever does stop working I'll get back to this Thanks.

    And in the mean-time I've built my own 'logging system' (basically just a dump of the exception to a log file on the server), but I haven't encountered an exception yet so the file is still empty

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Create an image on the server every 5 minutes

    No exceptions, woo hoo!!

    In case you need it in the future, take a look here:

    http://www.vbforums.com/showthread.php?t=595883

    10 simple steps to getting log4net working across your entire application.

    Gary

  10. #10

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Create an image on the server every 5 minutes

    It really is. Once it is set up, you never have to worry about it again, just do:

    Code:
    log.Debug("This is a debug statement");
    log.Error("Error", ex); //passing in the exception, and log4net does everything else.
    There are other frameworks out there, but log4net is the one that I have settled on for now, although elmah is starting to look very interesting:

    http://code.google.com/p/elmah/

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width