i have problem to run task in button Problem is : cannot convert from threading.task.task to system action i marked the line in the button
private async void BtnStart_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty( txtProxy.Text) || lstviewcomp.Items.Count==0)
{
MessageBox.Show("Please uploads files");
return;
}
proxies = txtProxy.Text.Split('\n');
proxycount = proxies.Length;
foreach (string item in lstviewcomp.Items)
{
proxycount++;
if (proxyCounter> proxycount)
{
proxyCounter = 0;
}
ProxyInfo = proxies[proxyCounter].Split(',');
var result = await Task.Run( MainAsync("", "", "", "", "", "")).GetAwaiter().GetResult();// Problem in this line
// proxyCounter++;
}
}
public async Task MainAsync(string instausername,string pass,string proxyip,string proxyport,string proxyusername,string proxypass)
{
try
{
// create user session data and provide login details
var userSession = new UserSessionData
{
UserName = instausername,
Password = pass
};
// create proxy handeler
var httpHndler = new HttpClientHandler();
IWebProxy proxy = new WebProxy(proxyip,Convert.ToInt32(proxyport));
proxy.Credentials = new NetworkCredential(proxyusername, proxypass);
httpHndler.Proxy = proxy;
// create new InstaApi instance using Builder
_instaApi = new InstaApiBuilder()
.SetUser(userSession)
.UseHttpClientHandler(httpHndler)
.UseLogger(logger: new DebugFileLogger()) // use logger for requests and debug messages
.SetRequestDelay(TimeSpan.FromSeconds(1)) // set delay between requests
.Build();
// login
txtLog.Text = txtLog.Text + $"Logging in as {userSession.UserName}"+" At "+DateTime.Now+"\n";
var logInResult = await _instaApi.LoginAsync();
if (!logInResult.Succeeded)
{
txtLog.Text = txtLog.Text + $"Unable to login: {logInResult.Info.Message}" + " At " + DateTime.Now + "\n";
}
else
{
txtLog.Text = txtLog.Text + $"Logging in success : {userSession.UserName}" + " At " + DateTime.Now + "\n";
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
var logoutResult = Task.Run(() => _instaApi.LogoutAsync()).GetAwaiter().GetResult();
if (logoutResult.Succeeded) txtLog.Text = txtLog.Text + "Logout sucess \n";
}
return false;
}
public async Task MainAsync
should be changed to
public async Task<bool> MainAsync
And then instead of
var result = await Task.Run( MainAsync("", "", "", "", "", "")).GetAwaiter().GetResult();
you can use
var result = await MainAsync("", "", "", "", "", "");
Also you need to use async in the following line too:
var logoutResult = Task.Run(() => _instaApi.LogoutAsync()).GetAwaiter().GetResult();
Becomes:
var logoutResult = await _instaApi.LogoutAsync();
Task.Run take Func<Task> or Action (of course you can pass additionaly CancellationToken). So for your case you need pass a function that returns a Task and doesn't try to await a void (I hope you are remember that you cannot await a void). Just get the result of task:
Task.Run(() => MainAsync("", "", "", "", "", "")).GetAwaiter().GetResult();
You can directly wait the task instead of code above:
Task.Run(() => MainAsync("", "", "", "", "", "")).Wait();
But if you want to continue your workflow asynchronously by task' result your MainAsync should returns a Task<bool> as pointed out in @Olexiy Sadovnikov answer and await the returned task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With