Hopefully someone has used the Salesforce for .NET toolkit enough to point me in the right direction.

Here is my code:

private void btnOk_Click(object sender, EventArgs e)

private async Task ConnectToSf()
    bool isSandbox = true;
    var auth = new AuthenticationClient();
    var endPoint = Program.EndPoint;

    // Other junk here.

        await auth.UsernamePasswordAsync(Program.ConsumerKey, Program.ComsumerSecret,
            txtUsername.Text, txtPassword.Text + txtToken.Text, endPoint);

        SfAuth.AccessToken = auth.AccessToken;
        SfAuth.ApiVersion = auth.ApiVersion;
        SfAuth.InstanceURL = auth.InstanceUrl;

    catch (Exception ex)
        throw ex;


I'm using the ConnectToSf().Wait(); so that I can wait for the "await" to get the instanceURL and things set before trying to do other stuff. It's hanging forever.

If I take the Wait() off of ConnectToSf() it does not hang but other things are looking for the instanceUrl at that point and it's not always back in time.

Don't know if this is more a C# question or a Salesforce question but since it the "Salesforce Toolkit for .NET" I'd post it here first. Any ideas what I'm doing wrong? Haven't did a whole lot with async/await before so might be something stupid I'm doing wrong.

Here is the code I am using to call the form:

var f = new Connections();

if (f.ShowDialog() == DialogResult.OK)
    this.statusStripInstance.Text = "Connected: " + SfAuth.InstanceURL;

After I click Ok on the connect form it comes back to the main form and tries to set the status but right now it's just putting Connected: because SfAuth.InstanceURL is not set yet. I verified this by putting a breakpoint in the setter for SfAuth.InstanceURL and indeed I get back to the main form and it changes the status to Connected: then about a half second later I hit my breakpoint because the async method finally populated SfAuth.InstanceURL.

  • Once you set the await keyword on an async method it will wait for the result. Your accessToken, apiVersion and InstanceUrl will always be populated provided that you have the correct credentials. Also why don`t you return anything from the async method? You are waiting to return nothing Jan 17, 2015 at 11:41
  • Please see my edit above. Hope this explains my problem a little better.
    – Randy R
    Jan 17, 2015 at 15:47

1 Answer 1


I finally ended up putting a Timer in the main form that is activated when the connection form is called. Then this timer waits until it sees a real value in SfAuth.InstanceURL. Then the timer turns itself off so it's not hogging up any resources until the connection form is called again.

Might not be the best solution but for now I know the moment I am authenticated. Would still really like to find out why Wait(); doesn't work with the toolkit.

  • If this answer helped you, you should accept it, so others will be able to know that the solution works. Additionally, it is acceptable to accept your own answer. Aug 1, 2016 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .