views:

355

answers:

5

Hi, Previously I posted a question regarding multithreading. Actually my intension is to send SMS for 1000 (or more) people at a same point of time (Ex: 12:00 AM sharp) by using c# and asp.net application. Is it ok to choose multithreading concept to achieve this?

+1  A: 

I dont think that is going to work for you, and creating such a large number of threads is not advised.

Also, see this link

maximum-number-of-threads-in-a-net-app

Does the SMS application allow for send-to-many? Or maybe use different services on various boxes to send these subset of sms. But i think sending such a volume at once will be difficult.

astander
so can you suggest me what is the solution for my problem? is there any other alternative way to achieve?
Nagu
+1  A: 

Depends on how the SMS are actually sent. If you have let's say a web service that sends the SMS you'll end up in querying it 1000 times at one point which won't solve your problem.

To achieve this you need to make sure that the task of sending can be done simultaniously.

EDIT:
Furthermore I agree to astander that that amount of threads won't be healthy for your system at all.

Edit2: How sharp does this needs to be? Assumeing that hh:mm is enough you'd have 60s to send your about 1000 sms. This means that you need to send aprox 17 SMS per second. If you share this to lets say 4 threads then you'd only need to make sure that your sending process / device can send 4 SMS / s. this should be achievable I guess.

HTH

KB22
Oh if i send sumultaniously that time will become change. My intention is to send it at a same time.
Nagu
@Nagu I'm not sure how what time is displayed on SMS message: message's "receive" time or senders "send" time. Somehow I think it is the second choice and in that case it does not really matter for all your messages to be sent at exactly the same moment (cell phone time sync.). On other hand if your SMS service provider allows you to pass a list of receivers it still won't guaranty the singular send time as provider should use the (message receivers) number of SMS gateways or something (-. You have to expect that there will be some timespan (microseconds) between first and last messages sent.
Audrius
+1  A: 

I don't know how you send them sms. But almost all big sms service providers will allow you to send 1000 within 1 seconds.
So Unless you REALLY REALLY REALLY REALLY REALLY REALLY need to be send them all at once, I suggest you just make a loop and send the information to the service provider one at the time.

Carl Bergquist
+4  A: 

That concept do not need Multi Threading ...

That concept is more of a Task Manager / Cron Job

  1. Create an ASPX Script that sees the time and executes the method you need
  2. Set up Task Manager to run this script every xx minutes
  3. Create a method, that fetches the list of persons and send the SMS through an SMS API, and call it, for ex. SendSMSFromList( List usersList, string message ) {}
  4. Now set everything up and you will run this anytime you need (just set it in the ASPX Script)

please, fell free to tell me, if you need any code for this.


edited for having all steps


If you have a hosted solution, in your hosting control panel you have something as Task Schedule that you can set up to run your script page every n minutes, if so please by pass the next steps. If, by other hand, you are running your own server (IIS) then do this first.

  • Install cUrl for windows from this location and add curl.exe to C:\WINDOWS
  • Open Task Manager (Control Panel > Administrative Tools > Task Scheduler on win7)
  • Create a new task like this
  • Run the command

curl http://localhost/yourApp/taskManager.aspx

with this you just configured your system to run a file, just like if you execute that link in a browser, that will run every 15 minutes.

Now we need to create that taskManager.aspx file

public partial class taskManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now;

        // Run after midnight
        if (dt.Hour == 0 && dt.Minute <= 15)
        {
            Write2Log("Schedule Job Started", LogType.INFO);

            SendSMSFromList(
                GetUsersList(), 
                GetSMSMessage());

            Write2Log("Schedule Job Finished", LogType.INFO);
        }
    }

    private string GetSMSMessage()
    { 
        // Fetch the text from DB...

        return "This is the message content that I will send as SMS"; 
    }

    private List<string> GetUsersList()
    { 
        // fetch the list form DB...

        return new List<string>(); 
    }

    private void SendSMSFromList(List<string> usersList, string message)
    { 
        // send SMS's
        foreach (string phoneNr in usersList)
        { 
            // send message
            mySMSAPI.Send(phoneNr, message);
        }
    }

    private void Write2Log(string text, LogType type)
    { 
        // Log to a file what's going on...
        try
        {
            string filename = HttpContext.Current.Server.MapPath("") + "\\status.log";
            using (StreamWriter sw = new StreamWriter(filename, true))  // open to append
            {
                // example: 2008-12-17 13:53:10,462 INFO - Schedule Job Finished
                string write = String.Format("{0} {1} - {2}",
                                DateTime.Now,
                                type.ToString(),
                                text);

                sw.WriteLine(write);
            }
        }
        catch (Exception)
        { }
    }

    private enum LogType
    { INFO, WARNING, ERROR }
}

Done...

I made everything in just one file for the sake of the example, you should divide things ... but what I was after was to show you the principle of it.

balexandre
Or use http://quartznet.sourceforge.net/ Quartz.net if scheduling needs are more sophisticated.
fvu
Ya thank you, I'll follow like this. But is it ok if we call the database for every 10 secs or with in a time span?
Nagu
@fvu nice open source app, I need to see this :) I'm using Task manager with curl :D
balexandre
@Nagu you will only call the database when you actually want to send it ... until then, the SendSMSFromList is never called. I can show u code if u need.
balexandre
can you give me the code
Nagu
steps and code added...
balexandre
A: 

I suspect you'll have some transport issues getting that much data to your SMS provider at exactly that time presuming it is a realtime process.

I'd find a provider capable of doing scheduled sends and then queue up the messages to send at 12AM at my leisure.

Wyatt Barnett