C#-使用后台工作程序从另一个类返回bool响应
我有一个WinForm,它有一个按钮,单击该按钮时,会调用另一个类中的方法将文本文件上载到Pastebin。代码最初工作正常,但在上传成功完成之前锁定了UI,因此我现在尝试使用后台工作程序完成此任务,以便UI保持响应 我在Visual Studio中遇到的错误是C#-使用后台工作程序从另一个类返回bool响应,c#,winforms,backgroundworker,C#,Winforms,Backgroundworker,我有一个WinForm,它有一个按钮,单击该按钮时,会调用另一个类中的方法将文本文件上载到Pastebin。代码最初工作正常,但在上传成功完成之前锁定了UI,因此我现在尝试使用后台工作程序完成此任务,以便UI保持响应 我在Visual Studio中遇到的错误是将匿名函数转换为返回的无效委托无法返回值。我看过类似的线程/谷歌,但不太明白这意味着什么,以及如何去纠正它 我的上传按钮代码: private void btnUpload_Click(object sender, EventArgs e
将匿名函数转换为返回的无效委托无法返回值
。我看过类似的线程/谷歌,但不太明白这意味着什么,以及如何去纠正它
我的上传按钮代码:
private void btnUpload_Click(object sender, EventArgs e)
{
this.btnUpload.Enabled = false;
this.btnUpload.Text = "Uploading...";
if (Pastebin.UploadLog())
{
Clipboard.SetText(Properties.Settings.Default.logUrl);
MessageBox.Show("Your logfile has been uploaded to Pastebin successfully.\r\n" +
"The URL to the Paste has been copied to your clipboard.", "Upload successful!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("The upload of your logfile to Pastebin failed.", "Upload failed!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
this.btnUpload.Text = "Upload";
this.btnUpload.Enabled = true;
}
完成上载的我的代码:
class Pastebin
{
public static bool UploadLog()
{
var upload = new BackgroundWorker();
upload.DoWork += delegate
{
Properties.Settings.Default.logUrl = "";
Properties.Settings.Default.Save();
System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection();
Data["api_paste_name"] = "RWC_Log_" + DateTime.Now.ToString() + ".log";
Data["api_paste_expire_Date"] = "N";
Data["api_paste_code"] = File.ReadAllText(Properties.Settings.Default.AppDataPath + @"\Logs\RWC.log");
Data["api_dev_key"] = "017c00e3a11ee8c70499c1f4b6b933f0";
Data["api_option"] = "paste";
WebClient wb = Proxy.setProxy();
try
{
byte[] bytes = wb.UploadValues("http://pastebin.com/api/api_post.php", Data);
string response;
using (MemoryStream ms = new MemoryStream(bytes))
using (StreamReader reader = new StreamReader(ms))
response = reader.ReadToEnd();
if (response.StartsWith("Bad API request"))
{
Logging.LogMessageToFile("Failed to upload log to Pastebin: " + response);
return false;
}
else
{
Logging.LogMessageToFile("Logfile successfully uploaded to Pastebin: " + response);
Properties.Settings.Default.logUrl = response;
Properties.Settings.Default.Save();
return true;
}
}
catch (Exception ex)
{
Logging.LogMessageToFile("Error uploading logfile to Pastebin: " + ex.Message);
return false;
}
};
upload.RunWorkerAsync();
}
}
我建议您使用
async
函数,而不是BackgroundWorker
。您可以使您的UploadLog
函数async
如下所示
public static async Task<bool> UploadLog()
{
Properties.Settings.Default.logUrl = "";
Properties.Settings.Default.Save();
System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection();
Data["api_paste_name"] = "RWC_Log_" + DateTime.Now.ToString() + ".log";
Data["api_paste_expire_Date"] = "N";
Data["api_paste_code"] = File.ReadAllText(Properties.Settings.Default.AppDataPath + @"\Logs\RWC.log");
Data["api_dev_key"] = "017c00e3a11ee8c70499c1f4b6b933f0";
Data["api_option"] = "paste";
WebClient wb = Proxy.setProxy();
try
{
byte[] bytes = wb.UploadValues("http://pastebin.com/api/api_post.php", Data);
string response;
using (MemoryStream ms = new MemoryStream(bytes))
using (StreamReader reader = new StreamReader(ms))
response = reader.ReadToEnd();
if (response.StartsWith("Bad API request"))
{
Logging.LogMessageToFile("Failed to upload log to Pastebin: " + response);
return false;
}
else
{
Logging.LogMessageToFile("Logfile successfully uploaded to Pastebin: " + response);
Properties.Settings.Default.logUrl = response;
Properties.Settings.Default.Save();
return true;
}
}
catch (Exception ex)
{
Logging.LogMessageToFile("Error uploading logfile to Pastebin: " + ex.Message);
return false;
}
}
我建议您使用
async
函数,而不是BackgroundWorker
。您可以使您的UploadLog
函数async
如下所示
public static async Task<bool> UploadLog()
{
Properties.Settings.Default.logUrl = "";
Properties.Settings.Default.Save();
System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection();
Data["api_paste_name"] = "RWC_Log_" + DateTime.Now.ToString() + ".log";
Data["api_paste_expire_Date"] = "N";
Data["api_paste_code"] = File.ReadAllText(Properties.Settings.Default.AppDataPath + @"\Logs\RWC.log");
Data["api_dev_key"] = "017c00e3a11ee8c70499c1f4b6b933f0";
Data["api_option"] = "paste";
WebClient wb = Proxy.setProxy();
try
{
byte[] bytes = wb.UploadValues("http://pastebin.com/api/api_post.php", Data);
string response;
using (MemoryStream ms = new MemoryStream(bytes))
using (StreamReader reader = new StreamReader(ms))
response = reader.ReadToEnd();
if (response.StartsWith("Bad API request"))
{
Logging.LogMessageToFile("Failed to upload log to Pastebin: " + response);
return false;
}
else
{
Logging.LogMessageToFile("Logfile successfully uploaded to Pastebin: " + response);
Properties.Settings.Default.logUrl = response;
Properties.Settings.Default.Save();
return true;
}
}
catch (Exception ex)
{
Logging.LogMessageToFile("Error uploading logfile to Pastebin: " + ex.Message);
return false;
}
}
DoWork是一个事件处理程序,不能从事件处理程序返回任何内容。DoWork是一个事件处理程序,不能从事件处理程序返回任何内容。如果要使用BackgroundWorker,必须指定具有两个类似参数的委托。upload.DoWork+=委托(对象发送方,DoWorkEventArgs e){},然后在必要时设置
e.Result=true
,而不是返回值。感谢您的建议。这似乎现在运行,这是一个加号,但我似乎回到了我第一次遇到的问题,即在上传完成时UI被锁定。谢谢。正如我在评论中提到的,使用BackroungWorker可以获得相同的结果。只需将具有两个类似参数的委托添加到upload.DoWork
事件upload.DoWork+=delegate(对象发送方,doworkereventargs e){}
然后在必要时使用e.Result=true
或e.Result=false
,而不是使用return语句。谢谢,错过了那个评论!我再试试看。:)当您想要得到在DoWork事件中设置的结果时,您应该像这样使用另一个BackgroundWorker事件RunWorkerCompleted
upload.RunWorkerCompleted+=委托(对象发送方,RunWorkerCompletedEventArgs e){Messagebox.Show(e.Result.ToString())}
如果要使用BackgroundWorker,必须指定具有两个类似参数的委托。upload.DoWork+=委托(对象发送方,DoWorkEventArgs e){},然后在必要时设置e.Result=true
,而不是返回值。感谢您的建议。这似乎现在运行,这是一个加号,但我似乎回到了我第一次遇到的问题,即在上传完成时UI被锁定。谢谢。正如我在评论中提到的,使用BackroungWorker可以获得相同的结果。只需将具有两个类似参数的委托添加到upload.DoWork
事件upload.DoWork+=delegate(对象发送方,doworkereventargs e){}
然后在必要时使用e.Result=true
或e.Result=false
,而不是使用return语句。谢谢,错过了那个评论!我再试试看。:)当您想要得到在DoWork事件中设置的结果时,您应该像这样使用另一个BackgroundWorker事件RunWorkerCompleted
upload.RunWorkerCompleted+=委托(对象发送方,RunWorkerCompletedEventArgs e){Messagebox.Show(e.Result.ToString())}