Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should use <code>await</code> keyword to wait for the task to finish. Just put <code>await</code> at the beginning of the line:</p> <pre><code>Task.Factory.StartNew(() =&gt; connectUser(username, password, server)); </code></pre> <p>Also, you have to add <code>async</code> keyword before the return type of the function.</p> <p>UPDATE (more info): <code>Task.Factory.StartNew</code> creates a task, starts it and returns it to you. When you don't use <code>await</code> you don't wait for it to finish. This helps for cases that you want to start a bunch of tasks and let them run simultaneously.</p> <pre><code> private static void connectUser(string username, string password, string server) { while (true) { Console.WriteLine("Connecting... " + username); Thread.Sleep(500); } } static void Main(string[] args) { var server = "a.com"; var alts = new string[] { "abc:abc", "bcd:bcd" }; var tasks = new Task[alts.Length]; for (int i = 0; i &lt; alts.Length; i++) { String password = alts[i].Substring(alts[i].IndexOf(":") + 1); String username = alts[i].Substring(0, alts[i].IndexOf(":")); Console.WriteLine("Loaded account: " + username); tasks[i] = Task.Factory.StartNew(() =&gt; connectUser(username, password, server)); } Task.WaitAll(tasks); } </code></pre> <p>This is what I suggested before. You create an array of <code>Task</code>s and put the result of <code>Task.Factory.StartNew()</code> into that array. You then use the <code>Task.WaitAll()</code> function to wait for the tasks to finish. Another approach would be to use threads.</p> <p>By the way, I suggest you to use Capital letters for function names, meaning: <code>ConnectUser</code> instead of <code>connectUser</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload