C# Threads



  • <FONT face=Arial size=2>Hi</FONT>

    <FONT face=Arial size=2>Im trying to develop a asp.net web application which every 5 mins gets data back from a database and checks a flag for validity and updates any rows which require it.</FONT>

    <FONT face=Arial size=2>I have added some basic logging to the app to see what is going wrong (and where) all this does is inserts a row containing the date and a description of what was happening into a table in the database.</FONT>

    <FONT face=Arial size=2>Now On my PC (Single 2.4 AMD Athlon) the thread is created and the log is written:
    1- Initialise thread called.
    2- Thread Starting
    3- Thread Spining</FONT>

    <FONT face=Arial size=2>Item 3 is being created from inside the new thread - if left running "thread spinning would be entered every 5 mins. This is how it should work.</FONT>

    <FONT face=Arial size=2>The problem comes when I put this on a 'proper' web server (which Im told is a dual xeon system) running win 2003</FONT>

    <FONT face=Arial size=2>On here only items 1 and 2 are written to the log suggesting that the thread is not starting, this is confirmed when you look at the thread manager page that I created which shows the new thread does not exist.</FONT>

    <FONT face=Arial size=2>If I remove the logging  the thread starts (on my pc and on web server) and runs (as shown on my thread manager page)</FONT>

    <FONT face=Arial size=2>This is not a problem with win 2k3 as I have an old server here running 2k3 and the site works fine on there.</FONT>

    <FONT face=Arial size=2>Any one have any ideas?
    To me it seems like there is an exception occurring somewhere during the thread execution on the webserver (probably when it is trying to add to the log table)
    Any ideas why this would happen on the web server but not on my pc? and how to resolve the problem?</FONT>

    <FONT face=Arial size=2>I can post the code that is in use if it would be helpful.</FONT>

    <FONT face=Arial size=2>Any help appreciated.</FONT>

    <FONT face=Arial size=2>Thanks in advance.</FONT>

    <FONT size=2></FONT> 



  • I hope I'm missing something here.

    This is an ASP.NET app that runs on top of a web server?

    It's started by a client accessing the page, "FORKS" a new thread (inside the webserver?!) that is suposed to stay running and, every 5 minutes perform some database maintenance?!

    W T F

    If that really is the case, then I would assume that the script (and all children) is KILLED as soon as the client disconnects after all output has been sent.

    I don't see how this is supposed to work in practice at all. Do you access the ASP page and leave the browser open all day long to make sure the script runs? Does it really run in the background, and what happens when you access the page multiple times? do you end up with thousands of background threads all performing the same work?!

    You should do something like this:
    - Write a stored procedure that updates the rows  when new data is inserted.
    - Update the rows in the script that inserts the new data.
    - Write a standalone C# application that performs the maintenance.
    - Update the rows when the data is accessed.

    I hope I just don't understand what you're doing :)



  • @Nand said:

    I hope I'm missing something here.

    This is an ASP.NET app that runs on top of a web server?

    It's started by a client accessing the page, "FORKS" a new thread (inside the webserver?!) that is suposed to stay running and, every 5 minutes perform some database maintenance?!

    W T F

    If that really is the case, then I would assume that the script (and all children) is KILLED as soon as the client disconnects after all output has been sent.

    I don't see how this is supposed to work in practice at all. Do you access the ASP page and leave the browser open all day long to make sure the script runs? Does it really run in the background, and what happens when you access the page multiple times? do you end up with thousands of background threads all performing the same work?!

    You should do something like this:
    - Write a stored procedure that updates the rows  when new data is inserted.
    - Update the rows in the script that inserts the new data.
    - Write a standalone C# application that performs the maintenance.
    - Update the rows when the data is accessed.

    I hope I just don't understand what you're doing :)

    <FONT face=Arial><FONT size=2>The thread runs independently of any user interaction with the site. one thread (ontop of the main thread) is created when the app starts and that’s it.
    The maintenance that the thread will be doing is changing the properties of certain rows in the database depending on the time and date - for example a news article will be flagged as old (therefore showing in the news archive as opposed to the news page) when its expiry date is reached - this is not the only thing that is being done by the thread, but it is one of the tasks that it will perform.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT>



  • Again, that's a big WTF. There's no reason to "flag" a news article as old, when you can just test against the expiration date. Or do you seriously think that "SELECT * FROM tblAritclesTableTable WHERE isOld=0" is that much better than "SELECT * FROM tblTableArticlesTableTableTable WHERE expiryDate > '2006-04-17'"?

    Seriously, you need to *really* re-think your application architecture. If you absolutely *have* to have this regular maintenance (which I suggest is a really, really, really bad idea), don't do it on a thread from within ASP.NET, use a stand-alone "maintainer" program that does your work for you.



  • @weaver said:

    <font face="Arial" size="2">a 'proper' web server ... running win 2003</font>


    [img]http://www.tolitz.com/images/ots/cat-hah.jpg[/img]

    And yes, your application design is a WTF too



  •  

    As stated in previous post this is not the only thing that the thread will be doing, while I agree that this is not the best method for the example given, it is one of the best options for the other tasks that need to performed, and as the person that the site is being created for wants to be able to stop these threads themselves and the restrictions applied by their hosting company using a seperate app is not an option.

    The problem has been resolved now anyway, it was not a coding problem, it was the set up off IIS on the hosting companies web server.

    I will take advice on board and rethink the threading, however Im certian that there are some instances where I will not be able to get round it any other way.

     

     



  • Regular maintiance, etc. on a database isn't that strange. We have a part of our system that once a week creates an export file and mails it to people. If possible, see if your database has an in-built task schedular. Also, the archived thing isn't so strange, but is normaly only used when moving the data to a seperate table (and often a different database/server) for performance.

    If its just a flag to make quering easier you could look to see if the database support views or computed columns.



  • On a side note, it could entierly be possible that your thread is loading and working, but something it is doing is causing an exception and the thread is silently dieing, something you have to watch out for when threading. But being a website I wouldn't be supprised if like Nand is saying, the webserver (IIS?) is deciding that it will terminate the thread as that applicition hasn't been used in a while, which is perfectly legal for the webserver to do. If you really need a custom thread to run and perform background tasks you want to be looking at writing a small application to run as a service, this can then be loaded when the computer boots.


Log in to reply