c# - Task.Run with Parameter(s)? -


i'm working on multi-tasking network project , i'm new on threading.tasks. implemented simple task.factory.startnew() , wonder how can task.run()?

here basic code:

task.factory.startnew(new action<object>( (x) => {     // 'x' }), rawdata); 

i looked system.threading.tasks.task in object browser , couldn't find action<t> parameter. there action takes void parameter , no type.

there 2 things similiar: static task run(action action) , static task run(func<task> function) can't post parameter(s) both.

yes, know can create simple extension method my main question can write on single line task.run()?

private void runasync() {     string param = "hi";     task.run(() => methodwithparameter(param)); }  private void methodwithparameter(string param) {     //do stuff } 

edit

due popular demand must note task launched run in parallel calling thread. assuming default taskscheduler use .net threadpool. anyways, means need account whatever parameter(s) being passed task potentially being accessed multiple threads @ once, making them shared state. includes accessing them on calling thread.

in above code case made entirely moot. strings immutable. that's why used them example. you're not using string...

one solution use async , await. this, default, capture synchronizationcontext of calling thread , create continuation rest of method after call await , attach created task. if method running on winforms gui thread of type windowsformssynchronizationcontext.

the continuation run after being posted captured synchronizationcontext - again default. you'll on thread started after await call. can change in variety of ways, notably using configureawait. in short, rest of method not continue until after task has completed on thread. calling thread continue run in parallel, not rest of method.

this waiting complete running rest of method may or may not desirable. if nothing in method later accesses parameters passed task may not want use await @ all.

or maybe use parameters later on in method. no reason await continue safely doing work. remember, can store task returned in variable , await on later - in same method. instance, once need access passed parameters safely after doing bunch other work. again, not need await on task right when run it.

anyways, simple way make thread-safe respect parameters passed task.run this:

you must first decorate runasync async:

private async void runasync() 

important note

preferably method marked async should not return void, linked documentation mentions. common exception event handlers such button clicks , such. must return void. otherwise try return task or task<tresult> when using async. it's practice quite few reasons.

now can await running task below. cannot use await without async.

await task.run(() => methodwithparameter(param)); //code here , below in same method not run until after above task has completed in 1 fashion or 

so, in general, if await task can avoid treating passed in parameters potentially shared resource pitfalls of modifying multiple threads @ once. also, beware of closures. won't cover in depth linked article great job of it.

side note

a bit off topic, careful using type of "blocking" on winforms gui thread due being marked [stathread]. using await won't block @ all, see used in conjunction sort of blocking.

"block" in quotes because technically cannot block winforms gui thread. yes, if use lock on winforms gui thread will still pump messages, despite thinking it's "blocked". it's not.

this can cause bizarre issues in rare cases. 1 of reasons never want use lock when painting, example. that's fringe , complex case; i've seen cause crazy issues. noted completeness sake.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -