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)
{
ConnectToSf().Wait();
this.Close();
}
private async Task ConnectToSf()
{
bool isSandbox = true;
var auth = new AuthenticationClient();
var endPoint = Program.EndPoint;
// Other junk here.
try
{
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;
}
return;
}
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
.