Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 ASP.NET MVC
I have this code that sends out sms notifications to subscribers (about 60,000 subscribers) I keep getting the error System.InvalidOperationException Invalid attempt to read when no data is present. What am I doing wrongly. The code is below. Error is thrown at the beginning of the parallel foreach Parallel.ForEach(accounts, options, x =>
 
var sw = new Stopwatch();
            sw.Start();
 
            var networkSettings = ops.GetNetworkSettings().ToDictionary(x => x.NetworkID, x => x.BillingURL);
            var serviceIds = serviceRepository.All.Where(x=>x.Status);
            //var blastMsgs = new Dictionary<int, Blast>();

            var currTime = DateTime.Now.TimeOfDay;
            foreach (var item in serviceIds)
            {
                var termTime = item.TermTime;
                var termTimeCeiling = currTime.Add(TimeSpan.FromMinutes(15));
                var termTimeFloor = currTime.Add(TimeSpan.FromMinutes(-15));
 
                if (termTime < termTimeFloor || termTime > termTimeCeiling) continue;
 
                var activeEntry = entryRepository.GetMessage(item.ServiceId);
 
                if (activeEntry == null) continue;
 
                var accounts = accountRepository.SubscribersByServ(item.ServiceId);
                log.Debug("entry is: "+activeEntry.Msg);
                log.Debug("num of accounts: "+accounts.Count());
                log.Debug("service id is: "+item.ServiceId);
                var options = new ParallelOptions {MaxDegreeOfParallelism = 200};
 
                var count = 0;
                Parallel.ForEach(accounts, options, x =>
                                                        {
                                                            Thread.CurrentThread.IsBackground = false;
                                                            lock (log)
                                                            {
                                                                var msg = new OutboundMessage
                                                                              {
                                                                                  Message = activeEntry.Msg,
                                                                                  PhoneNumber = x.PhoneNumber,
                                                                                  SenderId = item.Sender,
                                                                                  NetworkId = x.NetworkId
                                                                              };
 
                                                                if (!ops.DoBilling(msg, item.CampaignId.ToString(), networkSettings)) return;
                                                                accountRepository.UpdateAccount(x.SubscriberId);
                                                                Interlocked.Increment(ref count);
                                                                log.Debug("current bill count is: "+count);
                                                            }
 
                                                        });
 
                entryRepository.UpdateMessage(activeEntry.MessageId);
                log.Debug("Service Id: " + item.CampaignId);
                log.Debug("Bill count is " + count);
            }
 
            sw.Stop();
            log.Debug("total bill time: "+sw.Elapsed.TotalMinutes+" minutes");
Posted 20-Feb-13 0:14am
Comments
S. M. Ahasan Habib at 20-Feb-13 5:35am
   
you need to debug the code and address which line of code throw InvalidOperationException and capture exception details and post that.
Aman.A at 20-Feb-13 6:57am
   
its obviously an issue with the multi-threading code you have. for some reason, the locking is not working i.e. one thread is trying to read some data that the other thread was meant to write but couldn't get to it due to contention and hence the "no data present" error. IMO atleast.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 330
2 Richard MacCutchan 225
3 BillWoodruff 185
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100