multithreading - bind log Asynchronous in winForm c# -
i tried add log inside winform while doind something
private async task savechunk(datachunksaver chunk) { int = 0; int step = 10; while (chunk.savechunk(i, step)) { += step; addlog(chunk.log); } }
where:
private async task addlog(string text) { logbulider.appendline(text); logbox.text = logbulider.tostring(); }
andlogbulider simple global stringbulider. problem when fire button savechunk task form freezes, can see logbox after done , wanned bisplayed after each step of chunk.savechunk.
tried fire them few methods, can't handle it
what doing wrong?
private async void button2_click(object sender, eventargs e) { await task.factory.startnew(() => savechunk(chunk)); task t = savechunk(chunk); // none of these works, tried few other //ways it, none prevents winform freezing }
i tried modify code using progress<string>
:
private async void button2_click(object sender, eventargs e) { var progress = new progress<string>(msg => { logbulider.appendline(msg); logbox.text = logbulider.tostring(); }); await task.run(() => savechunk(chunk, progress)); }
and
private async task savechunk(datachunksaver chunk, iprogress<string> progress) { int = 0; int step = 10; while (chunk.savechunk(i, step)) { += step; progress?.report(chunk.log); // use progress if null! } }
Comments
Post a Comment