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)

batch encoder

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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -