Click here to Skip to main content
16,017,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code:
C#
void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                bool pingResult = verificareInternet("www.google.ro");
                if (pingResult == true)
                {
                    int count = 0;
                    int count_notificari_trimise = 0;
                    sendNotification[] dateNotificari = null;
                    sendNotification[] notificari_trimise = null;
                    this.Invoke((MethodInvoker)delegate
                    {
                        dateNotificari = sendNotification.dateDeTrimisSms();
                        notificari_trimise = sendNotification.statusNotificari();
                        count = dateNotificari.AsEnumerable().Count();
                        count_notificari_trimise = notificari_trimise.AsEnumerable().Count();
                    });
                   
                    if (count == count_notificari_trimise)
                    {
                        m_oWorker.ReportProgress(100);
                    }
                    else
                    {
                        int start_from = 0;
                        int rest_notificari = 0;
                        rest_notificari = count - count_notificari_trimise;

                        if (count_notificari_trimise != 0)
                        {
                            start_from = count_notificari_trimise;
                        }

                        for (int i = start_from; i < count; i++)
                        {
                            try
                            {
                                m_oWorker.ReportProgress(((i * 100) / count));

                                if (m_oWorker.CancellationPending)
                                {
                                    e.Cancel = true;
                                    m_oWorker.ReportProgress(0);
                                    return;
                                }
                                try
                                {
                                    sendNotification.autoSMS("AMANET FABIO. Contractul dvs. nr.: " + dateNotificari[i].ContractId + " (" + dateNotificari[i].Name.Substring(0, 10) + "...) la " + SetariProgram.DenPunctDeLucru + " FABIO, expira in data de " + DateTime.Today.AddDays(1).ToShortDateString() + ". Informatii la: " + SetariProgram.TelefonPc + ".", dateNotificari[i].PhoneNumber1);
                                    Thread.Sleep(100);
                                    sendNotification.addNotificare(dateNotificari[i].ContractId);
                                    Thread.Sleep(100);
                                }
                                catch
                                {

                                }
                            }
                            catch (Exception ex)
                            {
                                sendNotification.autoSMS("AMANET FABIO. Eroare sms" + SetariProgram.DenPunctDeLucru + ", " + ex, "0748976325");
                                programeazaNotificari(new TimeSpan(20000));
                            }
                        }
                    }
                    m_oWorker.ReportProgress(100);
                }
                else
                {
                    programeazaNotificari(new TimeSpan(200000));
                }
            }
            catch (Exception ex)
            {
                sendNotification.autoSMS("AMANET FABIO. Eroare sms" + SetariProgram.DenPunctDeLucru + ", " + ex, "0748598698");
                m_oWorker.ReportProgress(0);
                programeazaNotificari(new TimeSpan(200000));
            }
        }

an i have the error:

C#
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at Amanet_.winAmanet.m_oWorker_DoWork(Object sender, DoWorkEventArgs e)


i can't see wy

What I have tried:

because when the debugger is attached, it does not seem to occur.
Posted
Updated 5-Jun-16 23:33pm
Comments
BillWoodruff 6-Jun-16 5:05am    
Put a break-point in your code, then single-step through it (F11 in Visual Studio) and isolate exactly where the error occurs, then add that information to this post. You would expect this kind of error to happen in a loop, but, I don't see anything in your for-loop that is modifying a collection.

1 solution

The only enumerable code I can see there is the Linq Count method calls:
C#
count = dateNotificari.AsEnumerable().Count();
                        count_notificari_trimise = notificari_trimise.AsEnumerable().Count();
So what I would suspect is happening is that one or other of the two collections dateNotificari or notificari_trimiseis being modified in a different thread during the Count operation. That would cause the "operation may not execute" error, but unless you are doing it pretty often I'd expect it to be an intermittent problem rather than a regular thing.
If you are modifying it outside the thread, then you need to look very carefully at your whole scheme - it sounds like it's not thread safe at all.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900