c# - Why does Windows Forms control's property cannot be changed during the OnPaint event -
i utilize onpaint (c#) event draw in form. want value of variable during onpaint process. cant during, before or after onpaint process...
in fact, variable counter want increment value of progressbar.
i tried adding thread, timer , "valuechanged" events, still can't value. code quite long (it's generating heatmap data).
i increment value in loops during event, , phone call onpaint event "invalidate()" function.
i hope clear without pasting code (it's long) ! thanks.
with code improve : (simplified)
public partial class heatpainter : usercontrol { public long _progress = 0; //my counter public heatpainter() { initializecomponent(); } public void drawheatmap(list<list<int>> items, decimal value, int maxstacks, int factor, string filename) { if (_allowpaint) //if command ready process { timer1.start(); _progress = 0; _allowpaint = false; invalidate(); } } protected override void onpaint(painteventargs e) { base.onpaint(e); (int pass = _factor; pass >= 0; pass--) { //some draw stuff //... _progress++; } } private void timer1_tick(object sender, eventargs e) { console.writeline(_progress); } }
it looks repainting takes lot of time. unable alter on form during repainting (it won't changed until end).
so should @ task other point of view. if create image(like how create jpg image dynamically in memory .net?) in parallel thread(or parallelization build http://www.dotnetperls.com/backgroundworker)? after painted set background or .image
of picturebox. form responsive time.
you have synchronize(to update progress bar) isn't such hard task (thread not updating progress bar command - c#, c# windows forms application - updating gui thread , class?).
and future: threads , backgroundworkers going away the.net world. still used in .net < 4.0 .net 4.0 , higher provide improve abstractions asynchronous operations. recommend read tasks , async await. more appropriate many launch-the-work-get-the-result-on-completion scenoarios.
you should utilize 1 asyncronous construct(for illustration backgroundworker) paint image. user command should provide event (actually improve refactor functionality out of user interface) like
public event eventhandler progresschanged;
and raise event in code creates image when modify property of progress
. don't forget synchronization , dispatching(see above).
c# events onpaint
No comments:
Post a Comment