c# - FillPie() in OnPaint caussing an error after minimizinig -
i've encountered problem handling onpaint event. have custom class:
namespace winformsconsumer { public class cookie { public point point{get; set;} public color color { get; set; } public int angle { get; set; } public cookie(point point, color color, int angle) { point = point; color = color; angle = angle; } } }
i defined helper method (taken class paintservice taken webservice):
public nullable<point> startdraw(int startx, int starty, int width, int height) { try { int x = rnd.next(startx, width); int y = rnd.next(starty + height); return (new point(x, y)); } catch (argumentoutofrangeexception e) { console.writeline(e.message); return null; } }
later on, i've created form. in have:
public partial class form1 : form { paintservice ps = new paintservice(); private list<cookie> list = new list<cookie>(); private backgroundworker bgw = new backgroundworker(); private int maxcookies = 500; private int sleepinterval = 200; private string[] colors = enum.getnames(typeof(knowncolor)); public form1() { initializecomponent(); this.doublebuffered = true; this.progressbar1.maximum = this.maxcookies; this.centertoscreen(); this.paint += form1_paint; this.bgw.dowork += delegate (object sender, doworkeventargs e) { backgroundworker bg = sender backgroundworker; bg.workerreportsprogress = true; (int = 0; < maxcookies; i++) { random r = new random(environment.tickcount); color tempcolor = color.fromname(colors[r.next(colors.length - 1)]); //startdraw z webservice this.list.add(new cookie((ps.startdraw(clientrectangle.x, clientrectangle.y, clientrectangle.width, clientrectangle.height-100)),tempcolor,r.next(360))); thread.sleep(this.sleepinterval); this.invalidate(); bg.reportprogress(i); } }; this.bgw.progresschanged += delegate (object sender, progresschangedeventargs e) { this.progressbar1.value = e.progresspercentage; }; this.bgw.runworkercompleted += delegate (object sender, runworkercompletedeventargs e) { this.progressbar1.visible=false; messagebox.show("metoda serwisowa z osobnego wątku zakończona!"); }; } private void form1_paint(object sender, painteventargs e) { graphics g = e.graphics; foreach(cookie wp in list.toarray()) { if(wp != null) { g.fillpie(new solidbrush(wp.color), wp.point.x, wp.point.y, 20, 20, wp.angle, 280); } } } private void button1_click(object sender, eventargs e) { this.bgw.runworkerasync(); } private void button2_click(object sender, eventargs e) { if(this.textbox1.text != null) { int interval=-1; int.tryparse(this.textbox1.text, out interval); if(interval>0) { this.sleepinterval = interval; } else { messagebox.show("błędna liczba!"); } } } }
now, thing is, when run app , click button1
goes right until minimize window. after reopen it, application crushes null refference exception. in debugger i've checked wp.point.x
, wp.point.y
cause error.
can tell me why happening?
your code seems allow null
valid value point
objects. so, need check null
before using point
property of cookie
class.
if(wp != null && wp.point != null) { g.fillpie(new solidbrush(wp.color), wp.point.x, wp.point.y, 20, 20, wp.angle, 280); }
try that.
Comments
Post a Comment