Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:12:46+00:00 2026-06-05T07:12:46+00:00

It is possible to run a thread from the WebRole.cs OnStart() method in such

  • 0

It is possible to run a thread from the WebRole.cs OnStart() method in such a way that we are able to access it through aspx page to perform background work?
I know that the correct way to do it would be to use a Worker Role but i wish to maintain the running costs as low as possible.

The idea would be to create a thread that would be always running and waiting for a job, for instance if i want to make a blocking operation like sending an email i would use the thread giving the SendEmail method, is it possible to do? If so, can you provide me some examples that could point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:12:49+00:00Added an answer on June 5, 2026 at 7:12 am

    I would suggets a solution that is different from Leon and David’s solution:

    • David’s solution is OK but is not resilient. What it the instance/process goes offline while processing the task?
    • Leon’s solution mostly applies to scheduled jobs, but sending an email isn’t always something that is scheduled (maybe you want to send an email when someone registers in your app).

    An other option you should look at is using Windows Azure Storage Queues (they are very cheap) in this scenario:

    • Your web application: Sends messages to the queue (like ‘send an email to someone@someone.com‘)
    • WebRole.cs: spawn a new thread when starting the instance and have it listen for messages from that queue. Whenever an message arrives, process it. If success, remove the message from the queue.

    This solution has many advantages. The WebRole.cs runs in a different process than your web application, so there is no impact on the request threads. And besides that, if sending the mail fails for whatever reason, the message will stay in the queue and will be processed the next time. This will make sure you won’t loose any tasks to execute if the application or the process crashes.

    Here is an example to get you started. Note that you’ll need to improve this code if you want it to be production ready (retry policy, exception handling, backoff polling, …):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.Diagnostics;
    using Microsoft.WindowsAzure.ServiceRuntime;
    using Microsoft.WindowsAzure.StorageClient;
    using System.Threading.Tasks;
    
    namespace MvcWebRole1
    {
        public class WebRole : RoleEntryPoint
        {
            public override bool OnStart()
            {
                Task.Factory.StartNew(InitializeQueueListener);
                return base.OnStart();
            }
    
            private void InitializeQueueListener()
            {
                Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                {
                    configSetter(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(configName));
                });
    
    
                var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
                var queueStorage = storageAccount.CreateCloudQueueClient();
                var queue = queueStorage.GetQueueReference("myqueue");
                queue.CreateIfNotExist();
    
                while (true)
                {
                    CloudQueueMessage msg = queue.GetMessage();
                    if (msg != null)
                    {
                        // DO SOMETHING HERE
                        queue.DeleteMessage(msg);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: I need a good way to get data from a thread to
1) Is is possible to have a thread be able to run only one
Is it possible to run a windows xp bat script remotely from a ubuntu
Is it possible to run R in Processing through rJava/JRI? If I deployed a
Is it possible to run the wpf app only on a CD/DVD that we
Is it possible to run JUnit tests from inside my Java application? Are there
Is it possible to run a class method (starting with a '+') in a
Possible Duplicate: How to update GUI from another thread in C#? I've currently got
Is it possible for each thread select its own unique value from the csv
Is it possible to run a Drools Flow process from a StatelessKnowledgeSession ? If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.