locked
Lock Function RRS feed

  • Question

  • Hi,

    I have a function which sends an email when it is called.

    However, on occassion this function can get called twice simultaneously (from 2 separate processes) and thus the same email is sent twice.

    Is there a way to lock this function for one process?

    Thanks!

    Thursday, October 11, 2012 2:23 PM

Answers

  • Hi obrienkev

    You say the same email is sent twice because two requests are made to send the same e-mail.

    Do you have something that can identify the e-mail uniquely? Like an integer ID or another hash value?

    This can work if the 2 threads are in the same appdomain.

    static class EmailSender {
    
        // a UNIQUE list of IDs
        static HashSet<int> _SentEmails = new HashSet<int>();
    
        // a thread locker
        static readonly object _locker = new object();
    
        /// <summary>
        /// Sends an email.
        /// </summary>
        public static void SendEmail(int ID, string subject, string body, string to) {
    
            lock (_locker) {
    
                if (_SentEmails.Add(ID) == false /* already in list */) {
    
                    return;
                }
            }
    
            /* 
             * logic to send the email goes here 
             */
        }
    }
    

    If however you need a machine-wide lock, then the mutex Stefan suggested would have to replace the normal locker, and instead of the HashSet<int>, you will need to query a central database or log file, or wherever you flag that an email has been sent.

    Let me know if this is what you are looking for.

    Thursday, October 11, 2012 7:57 PM

All replies

  • Use Mutex to gate your send mail method.
    Thursday, October 11, 2012 2:50 PM
  • obrienkev :

    Can you control it by creating a notification table or  have a flag/count in
    existing tables for email sent status..


    Thursday, October 11, 2012 7:41 PM
  • Hi obrienkev

    You say the same email is sent twice because two requests are made to send the same e-mail.

    Do you have something that can identify the e-mail uniquely? Like an integer ID or another hash value?

    This can work if the 2 threads are in the same appdomain.

    static class EmailSender {
    
        // a UNIQUE list of IDs
        static HashSet<int> _SentEmails = new HashSet<int>();
    
        // a thread locker
        static readonly object _locker = new object();
    
        /// <summary>
        /// Sends an email.
        /// </summary>
        public static void SendEmail(int ID, string subject, string body, string to) {
    
            lock (_locker) {
    
                if (_SentEmails.Add(ID) == false /* already in list */) {
    
                    return;
                }
            }
    
            /* 
             * logic to send the email goes here 
             */
        }
    }
    

    If however you need a machine-wide lock, then the mutex Stefan suggested would have to replace the normal locker, and instead of the HashSet<int>, you will need to query a central database or log file, or wherever you flag that an email has been sent.

    Let me know if this is what you are looking for.

    Thursday, October 11, 2012 7:57 PM
  • Yes the lock statement does not seem to work on it's own so maybe I need to add some unique identifier
    Friday, October 12, 2012 8:00 AM
  • Maybe you should explain the scenario a little bit better. Do your two processes call one single mailing process or are they also calling two separate processes. What kind of processes are we talking about? Is there any WCF, Windows Service, IIS involved?
    Friday, October 12, 2012 8:04 AM