gpt4 book ai didi

c# - 无法从线程任务 转换为系统操作

转载 作者:行者123 更新时间:2023-11-30 20:17:13 25 4
gpt4 key购买 nike

我在按钮中运行任务时遇到问题问题是:无法从 threading.task.task 转换为系统操作我在按钮中标记了行

         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)异步任务 MainAsync(字符串 instausername,字符串 pass,字符串 proxyip,字符串 proxyport,字符串 proxyusername,字符串 proxypass) { 尝试 { //创建用户 session 数据并提供登录详细信息 var userSession = new UserSessionData { UserName = instausername, 密码=通过 }; //创建代理处理器 var httpHndler = new HttpClientHandler(); IWebProxy proxy = new WebProxy(proxyip,Convert.ToInt32(proxyport)); proxy.Credentials = new NetworkCredential(proxyusername, proxypass); httpHndler.Proxy = 代理; //使用 Builder 创建新的 InstaApi 实例 _instaApi = 新 InstaApiBuilder() .SetUser(用户 session ) .UseHttpClientHandler(httpHndler) .UseLogger(logger: new DebugFileLogger())//将记录器用于请求和调试消息 .SetRequestDelay(TimeSpan.FromSeconds(1))//设置请求之间的延迟 .Build();

//登录
txtLog.Text = txtLog.Text + $"以 {userSession.UserName} 身份登录"+"在 "+DateTime.Now+"\n";
var logInResult = await _instaApi.LoginAsync();
如果(!logInResult.Succeeded)
{
txtLog.Text = txtLog.Text + $"无法登录:{logInResult.Info.Message}"+ "At "+ DateTime.Now + "\n";

}
别的
{
txtLog.Text = txtLog.Text + $"登录成功:{userSession.UserName}"+ "At "+ DateTime.Now + "\n";

}
}
catch (异常前)
{
控制台.WriteLine(ex);
}
最后
{
var logoutResult = Task.Run(() => _instaApi.LogoutAsync()).GetAwaiter().GetResult();
if (logoutResult.Succeeded) txtLog.Text = txtLog.Text + "注销成功\n";

}
返回假;
}

最佳答案

public async Task MainAsync

应该改为

public async Task<bool> MainAsync

然后代替

var result = await Task.Run( MainAsync("", "", "", "", "", "")).GetAwaiter().GetResult();

你可以使用

var result = await MainAsync("", "", "", "", "", "");

此外,您还需要在以下行中使用 async:

var logoutResult = Task.Run(() => _instaApi.LogoutAsync()).GetAwaiter().GetResult();

变成:

var logoutResult = await _instaApi.LogoutAsync();

关于c# - 无法从线程任务 <bool> 转换为系统操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com