How can i clear the List and protect it with mutex so i will not get an exception ?


maybe mutex not needed here. need clear list without gtting exception.

i explain exception in bottom of post.

i have in form1 backgroundworker:

private void backgroundworker1_dowork(object sender, doworkeventargs e)         {             backgroundworker bgw = (backgroundworker)sender;             distance = 0; counter = 0;             if (bgw.cancellationpending == true)             {                              }             else             {                 while (counter < 200)                 {                     counter += 1;                     distance = (float)counter;                     datetime dt1, dt2;                     dt1 = datetime.now;                     cloudenteringalert.cloudalert(localbmp, distance);                     dt2 = datetime.now;                     timespan ts = dt2 - dt1;                     logger.write(ts.totalmilliseconds.tostring());                     if (counter == 199)                     {                         counter = 0;                         cloudenteringalert.clearlist();                                              }                 }             }         }

this cloudalert method in new class cloudenteringalert:

private static bitmap bmp;         public static list<pointf> cloudalert(bitmap nbmp, float kilometers)         {             adddistancetopoints = new list<pointf>();             color c1 = color.white;             color c2 = color.fromargb(c1.a,                 (int)(c1.r * 1), (int)(c1.g * 1), (int)(c1.b * 1));             float distance = kilometers / (float)1.09;             clouds = new list<pointf>();             clouds1 = new list<pointf>();                                                    (int = 0; < pointsfloat.count; i++)             {                                  adddistancetopoints.add(new pointf(pointsfloat[i].x - distance, pointsfloat[i].y));             }                          if (bmp==null)                 bmp = fastcomparison(nbmp, properties.resources.clean_radar_image);             newbitmap = bmp;             (int x = 0; x < adddistancetopoints.count; x++)             {                 if (adddistancetopoints[x].x > 0)                 {                                         color color = bmp.getpixel((int)adddistancetopoints[x].x, (int)adddistancetopoints[x].y);                     int dr = (int)color.r;                     int dg = (int)color.g;                     int db = (int)color.b;                     if (dr == 0 && dg == 0 && db == 0)                     {                      }                     else                     {                                                  clouds.add(new pointf(adddistancetopoints[x].x, adddistancetopoints[x].y));                                              }                 }                              }             //newbitmap.save(@"d:\test\newbitmap.jpg");             if (clouds.count == 0)             {                 cloudsfound = false;                 cloudsdistance.text = distance.tostring();                 return clouds;             }             else             {                 cloudsfound = true;                                  (int = 0; < clouds.count; i++)                 {                     pointtocolor.add(clouds[i]);                     cloudsdistance.text = distance.tostring();                 }             }             return clouds;         }

now have timer tick event:

private void timer6_tick(object sender, eventargs e)         {             picturebox1.invalidate();         }


in picturebox1 paint event have:

private void picturebox1_paint(object sender, painteventargs e)         {                 cloudenteringalert.paint(e.graphics, factor, distance);             anglecounter += 1;             drawline(e.graphics, anglecounter);             if (null != mimage)             {                 e.graphics.drawimage(mimage, mrect);             }             drawrectangle(e.graphics);         }


the paint metho cloudenteringalert class color points:

public static void paint(graphics e, double currentfactor, float kilometers)         {             float distance = kilometers / (float)1.09;             pen p;             p = new pen(brushes.green);             if (points == null)             {                 return;             }             foreach (point pt in pointsint)             {                 e.fillellipse(brushes.red, pt.x * (float)currentfactor, pt.y * (float)currentfactor, 2f, 2f);             }             foreach (pointf pt in movingpoints)             {                 if (clouds != null)                 {                     e.fillellipse(brushes.red, (pt.x - distance) * (float)currentfactor, pt.y * (float)currentfactor, 2f, 2f);                 }             }                         int current_list_length = pointtocolor.count;             (int kk=0;kk<current_list_length;kk++)             {                 pointf pt = pointtocolor[kk];                 e.fillellipse(cloudcolors[cloudcolorindex], pt.x * (float)currentfactor, pt.y * (float)currentfactor, radius, radius);             }                      }


in paint method in bottom i'm coloring list pointtocolor , list update add items backgroundworker in cloudalert method.

so wanted clear list each time list not big.

so in cloudenteringalert class in bottom added method:

internal static void clearlist()         {             pointtocolor.clear();         }

i'm calling clearlist in form1 backgroundworker dowork event.

but when i'm doing throw exception in cloudebteringalert class in paint method:

when i'm coloring items in list:

int current_list_length = pointtocolor.count;             (int kk=0;kk<current_list_length;kk++)             {                 pointf pt = pointtocolor[kk];                 e.fillellipse(cloudcolors[cloudcolorindex], pt.x * (float)currentfactor, pt.y * (float)currentfactor, radius, radius);             }

the exception on line:

pointf pt = pointtocolor[kk];

index out of range. must non-negative , less size of collection

since i'm clearing list pointtocolor each time it's empty can't color whats in kk.

the problem how can clear list pointtocolor each time without getting exception ?

if not clear list very big each time , slow since in backgorundworker keep calling cloudalert method , loop on list add items again.

so in beginning run fast since list small after few times become slow , when it's getting more 50 or on 100 times won't move.

that's why must clear list somehow without making exception.



hi,

why dont't check if count > 0 right before assign pt?

if count = 0, jump out of loop


regards, nico

pdfaid, my blog



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'