Simple way of running a background process from asp.net

Sometimes in asp.net you want to initiate a process that may take some time - and so don't want to hold the user up while it finishes, or which it just isn't necessary that it finishes before the page returns to the user (e.g. send an email).

Therefore you need some way to pass some parameters through to another process and have it just run as a background task.

I've been looking round for some simple way to do this, but haven't really come across anything. I had assumed Windows Workflow Foundation might be the answer but I've read a fair amount on it and can't see it really does what I want - to be honest it seems less to do with running some background process and more to do with managing workflows.

Ideally I'd like this to be reasonably straight forward, and use something which already exists within Windows - I'd prefer not to have to install anything extra on a target server machine.
purplesoupProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
You can fire off another thread...

http://stackoverflow.com/questions/1824933/right-way-to-create-thread-in-asp-net-web-application

The only thing to watch with this is that asp.net worker processes usually power down after a period of inactivity, which could be as short as 5mins on some servers. This will also shut down any work threads you've fired of.
0
purplesoupProgrammerAuthor Commented:
ok thanks that is useful but it doesn't sound like the most robust of solutions - not your fault of course - I'd appreciate it if anyone else had any ideas.
0
Dale BurrellDirectorCommented:
It actually works pretty well, you have to have a very long process and have the site not used very much for it to be an issue. And you can always extend the timeout of the worker process.

This is the straight forward solution... any other solution is going to be more complex.

For example you can put an entry in a database and then write yourself a windows service which monitors the database and executes jobs for you.

Or you could use MSMQ http://msdn.microsoft.com/en-us/library/ms978430.aspx.

I think you'll find thats most of your options.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
purplesoupProgrammerAuthor Commented:
Sorry I know I've marked the question but I can't seem to get this example working.

I've got a simple one button on a page and have put the code in the example - but trying to run it with Init(work) fails - I've tried with new Worker() as well without success. I'll copy the code below. I'm a bit out of practice with delegates.

namespace WorkerThreadTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        private const string connString =
@"Data Source=(local)\SQL2008CI;User ID=sa;Password=dsa;Initial Catalog=Test;";

        public delegate void Worker();
        private static Thread worker;

        public static void Init(Worker work)
        {
            worker = new Thread(new ThreadStart(work));
            worker.Start();
        }

        public static void Work()
        {
            // do stuff
            for (int i = 0; i < 10; i++)
            {
                UpdateTable();
                Thread.Sleep(5000);
            }
        }

        protected void RunMe(object sender, EventArgs e)
        {

            Init(work);
            lblMessage.Text = "Worker process started";
        }

        private static void UpdateTable()
        {
            var conn = new SqlConnection(connString);
            conn.Open();
            var sql = "UPDATE WorkerThreadTest SET NUMBER_OF_UPDATES=NUMBER_OF_UPDATES+1";

            var cmd = new SqlCommand(sql, conn);

            var result = cmd.ExecuteNonQuery();

            conn.Close();
        }
    }
}

Open in new window

0
Dale BurrellDirectorCommented:
Rather than try and work out what you've done, here is some code which I use regularly and which I know works. Its its own class (not mixed in with an asp.net page). Otherwise it doesn't look much different to yours. But give it a try anyway. Maybe an error is occurring which you aren't catching?

using System;
using System.Data;
using System.IO;
using System.Threading;

public class Maintenance
{
    private Thread ThisThread;

    public bool IsAlive
    {
        get { return ThisThread != null ? ThisThread.IsAlive : false; }
    }

    public Maintenance() { }

    public void Start()
    {
        System.Threading.ThreadStart ts = new System.Threading.ThreadStart(this.StartThread);
        ThisThread = new System.Threading.Thread(ts);

        try
        {
            ThisThread.Start();
        }
        catch (Exception Error)
        {
            LogError(Error, "Maintenance:Thread Failed to Start:" + ThisThread.ThreadState.ToString());
        }
    }

    public void Stop()
    {
        if (ThisThread != null)
        {
            ThisThread.Abort();
            ThisThread = null;
        }
    }

    private void StartThread()
    {
        LogError("Maintenance:Thread - Start: " + DateTime.Now.ToString());
        // DO STUFF
    }
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Server OS

From novice to tech pro — start learning today.