Showing posts with label threading. Show all posts
Showing posts with label threading. Show all posts

Tuesday, March 13, 2012

system.threading question

Hey everyone...

I don't know exactly where to post this to, so i'll try here first.

Focussing on the system.threading.timer object...

Where would i place this timer and assocaited delegates in an asp.net web application?

I need it to simply process the files in a directory on
a shared folder, but i need it to run every 10 minutes or so,
irregardless of someone has loaded a page or not - the server
still needs to process the files...

thanks
tonyYou will need to develop a windows service. You can not have a timer run without a asp.net application behind it. The asp.net application gets created on the first request and stays around for a while.

If you need this processing to happen irregardless if a web request comes in or not, then you can not host this process in a asp.net application.

HTH,

bill
Why not make a Windows service?
Some months back, Rob Howard gave a talk at the user group I attend and covered a wide variety of topics - one of the things he showed us was background thread processing implemented for an ASP.NET app. You can find a link to the download of the code on his site (http://www.rob-howard.net/), click on the "Demos" link under "Blackbelt ASP.NET" (not the "Blackbelt ASP.NET Controls"). In the "BlackbeltBLL" folder in the code you download, you will find a file, "HttpModule.cs;" here is the code (modified to include only what you need):


using System;
using System.Web;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BlackbeltBLL
{
public class BackgroundService : IHttpModule
{
static Timer timer;
int interval = 5000;

public void Init(HttpApplication application)
{
// Wire-up application events
if (timer == null)
{
timer = new Timer(new TimerCallback(ScheduledWorkCallback), application.Context, interval, interval);
}
}

public void Dispose()
{
timer = null;
}

private void ScheduledWorkCallback (object sender)
{
// here you call whatever code you want to run at the interval
ProcessFiles();
}
}
}

Add that to your project and of course register it with the web.config file.

Basically, this starts a background thread that will run at whatever interval you specify and perform some task(s). I have extended and used this for a variety of applications, and it works nicely.

HTH
Good question...

That is how i have it implemented now...

but if i can streamline the installation, remove a few tasks
from the installation/implementation team, that would save
bottom line cash - and when you save a little time here and there, and
the installation/implementation team says the install took
20 less minutes, then the management sees that as a good thing.

so to answer your question - i'm sick of writing windows services...

thanks for the reply

take care
tony
Let me know if that ends up working for you (the HttpModule) . . .
Hi Jason and everyone!!

Your code snippet looks like a winner..

and the rob howard site is a fantastic reference - thank you

i will create a working demo in a few minutes -

if there are any problems, i'll post them up!!

thanks again -

take care
tony
Sweet, I hope it works out for you . . . I love it. Not that anyone cares, but I added this HttpModule to my installation of nGallery for my family site, and it emails all of my family members when anyone uploads new images so that we know when to go look at the site. I have had that up and running for months and love the convenience of not having to run a separate service.
Hi Jason,

Where exactly do i hook this new class from??

i was thinking the
protected void Application_Start(Object sender, EventArgs e)
in the global.asax section...

but i'm not sure if this is the proper place to kick it off from...

advise would be greatly appreciated

take care
tony
Tony,

Check out the section with the heading "Creating and Registering Custom HTTP Modules and HTTP Handlers" inthis MSDN article for more information on how to register HttpModules (about 1/4 of the way down the page).

Let me know if there is anything else . . .

System.Threading.ThreadAbortException

Hi,

I'm running some code that posts from my application (ASP.Net) to a Payment Gateway. I get this error - [System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}. I guess this is because my I am transfering to another server and once this is done my code doesn't know what to do with itself.

Could someone explain what this error is precisely, and how I might avoid it.

Regards,

Quinton Smith

For any one wondering what I did to get this working as no one answered, I had to move my code transferring so that it was the last code running before transfer and not in any try/catch blocks.

Still don't quite understand the error description though.

Quinton


Not enough info to give an accurate answer, but if you're using Response.Redirect:

From Visual Studio Help:
Redirect calls End which always raises a ThreadAbortException exception upon completion.

NC...


youre the man! out of the first couple of posts yours is the only one that actually helped KUDOS!

Hi Programmers,

I am using asp.net 2.0 with c#.

" myReportDocument.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, false, rptName);"

As soon as compiler execute this statement i get error

" [System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} "

What will be the possible reasons for the error.

Sorry for the bad english.


I believe that this is because, Response.End is called internally in the ExportToHttpResponse method:

Try wrapping it in a try/catch block:

try
{
myReportDocument.ExportToHttpResponse(...);
}
catch (System.Threading.ThreadAbortException)
{
}

NC...


Thanks for reply,

I m already using try{} catch{} block

try

{

Some coding here

try
{

Response.TransmitFile("CodeGeneratedFiles/" + TemplateFileName);
Response.End();
}

catch (System.Threading.ThreadAbortException ex)
{
Log.WriteLog("Report Viewer", "Set The Datasource", ex.Source, ex.Message);
}

Response.Addheader("Title","Some Name for Popup");

}

Catch(Exception ex)

{

}

But it does not execute "Response.Addheader("title","Some Name for Popup");" after catch block it directly jumps to outer catch block.

Is there anyway to kill the exception..

Is there no other way to solve this error.[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

Hope to hear u soon.


No as I think there is a bug in ExportToHttpResponse. You might try Googling ExportToHttpResponse and System.Threading.ThreadAbortException. I found some work-a-rounds there.

NC...