c# - Do work without blocking the gui thread -


i have c# windows forms application , use library not provide async-await functionality.

when press on button want work (webrequesting). while doing work dont want freeze gui.

i tried several approaches, example:

public static task<bool> loginuser(string username, string password) {     return task.factory.startnew(() =>     {         try         {             session = new authenticatedsession<user>(new user(username), cryptography.getmd5(password));              return true;         }         catch (invalidauthenticationexception)         {             return false;         }     }); } 

when call loginuser("foo", "bar").result gui freezes until work done (i understand not async because can't await new authenticatedsession<...

so like:

  1. create thread action parameter
  2. return value thread
  3. end thread

try forcing new thread (or workerthread) instead of using taskfactory.

thread t = new thread (delegate() {     try     {         session = new authenticatedsession<user>(new user(username), cryptography.getmd5(password));         success();  //coded below     }     catch (invalidauthenticationexception)     {         fail();     }      }); t.start(); 

your list requires return value, can call method or set state indicating return value or signal (manualreseteventslim) if want blocking, requirements state want non-blocking.

to resume execution or signal gui process done invoke method on ui thread, this:

void success() {   invoke((methodinvoker) delegate {     somemethodontheui();   }); } 

this async/callback strategy.