C# program corrupted/losing content Rect LIST

August 31, 2011 at 09:23:10
Specs: Windows 7, 4GB
C# program corrupted/losing content Rect LIST

I have a Rectangle list that is being corrupted by an Invoke call.

The rectangle list is declared as follows:
public List<PDFHighlight> RectList = new List<PDFHighlight>(100);

This is taken from this class
public class PDFHighlight
{
public Rectangle TheReactangle;
public Boolean Flag;
public Boolean SecondRect;
}

When the list contents gets initialised by my search loop, it is then suddendly bizarrely (after the loop) being corrupted or lost when an Invoke call is made. Although these invoke calls are nothing to do with using this list.
Example:
EndSearchCallBack d = new EndSearchCallBack(EndSearch);
this.Invoke(d, new Object[] { });

I know this post might not make a lot of a sense to anyone but I'm just wondering why an invoke call would corrupt/erase contents of a list when declared / used elsewhere in the program? (C#)

I spent the last 5 / 6 hours looking at this, wondering why the contents is lost by these invoke calls, very annoying!

Thanks for your help / advice

Andrew Ashcroft


See More: C# program corrupted/losing content Rect LIST

Report •

#1
September 1, 2011 at 10:14:22
I'm just wondering why an invoke call would corrupt/erase contents of a list when declared / used elsewhere in the program? (C#)
This shouldn't happen unless your other threads are touching this list, or its objects. It's also hard to tell what's going wrong without source.

How To Ask Questions The Smart Way


Report •

#2
September 1, 2011 at 13:12:31
I have now fixed this problem by adding Thread.sleep(10), - 100th of a second. I think it was intialising the list too fast (search thread) before code elsewhere was completed.

Report •

#3
September 1, 2011 at 13:25:18
You still have a race condition, only now you're hiding it. A better practice would be to use locks. Suggested reading: Managed Threading Best Practices. It's not as thorough as it could be, but most articles dealing with the practical aren't.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
September 5, 2011 at 01:34:54
I found out the exact problem. Two functions were called after Search thread, one of which deletes the entire contents of rectangle list for initialisation.
I have now added a boolean variable flag to determine when the two lines of code (function calls) after the thread call have completed.
Alternatively I could have just changed the order before calling the search thread but this method works fine.

Thanks,

Andy


Report •

Ask Question