c# - Working with multiple threads and while updating the UI -
background info:
i'm creating batch encoder project. it's this: (i need add cancel button inside each item , replace placeholders)
for each item added, thread/task should process given information (image) , saved on disk encoded image.
i'm used work delegate
, delegateobject.begininvoke(...)
, updated ui static
calls inside delegate
method.
the problem:
i did winforms
, not dynamic list of items. i'm not familiar cancellation of threads/tasks ui.
the question:
i need little push head best way of approaching problem.
how manage multiple parallel operations , update ui?
have looked @ using cancellation tokens. along lines of:
private cancellationtokensource _cts; private async void start_click(object sender, eventargs e) { _cts = new cancellationtokensource(); var token = _cts.token; try { await task.run(() => { // encoding process }); } catch (operationcanceledexception) { // handle cancellation } } private void stop_click(object sender, eventargs e) { if (_cts != null) _cts.cancel(); }
Comments
Post a Comment