- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我可以从我的调试消息中看到 e.Result 在 DoWorker 方法中一直等于“成功”,但在 RunWorkerCompleted 方法开始时 e.Result 根本不返回任何内容。
这是 WinForm 的 __construct
public LicenseValidator()
{
// Initialize the UI
InitializeComponent();
// Start the background worker
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
// Tell debug log that the UI thread is still processing
Debug.WriteLine("UI thread running");
}
这是我的 worker_DoWork 方法
public async void worker_DoWork(object sender, DoWorkEventArgs e)
{
// Tell debug log that the worker thread is now running
Debug.WriteLine("Worker thread running");
// Define variables
String HWID = Security.FingerPrint.Value();
String APIPath = "http://xxxxxxxxx.net/api.php";
String Serial;
HttpClient client = new HttpClient();
HttpResponseMessage response;
String responseString;
// Check for license in AppData
try{
// Get license path
String licensePath = Environment.GetEnvironmentVariable("APPDATA") + @"\UniLeech\License.txt";
// License exists, validate it
if (File.Exists(licensePath))
{
Serial = File.ReadAllText(licensePath);
Debug.WriteLine(Serial);
response = await client.GetAsync(APIPath + "?hwid=" + HWID + "&serial=" + Serial);
responseString = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseString);
dynamic jObj = JsonConvert.DeserializeObject(responseString);
if (jObj.success == "true")
{
Debug.WriteLine("License validation was successful");
e.Result = "Success";
}
else
{
e.Result = jObj.error;
}
Debug.WriteLine(e.Result);
}
// License does not exist, prompt for one
else
{
Debug.WriteLine("License file not found");
e.Result = "Unregistered";
}
}
最后是我的 worker_RunWorkerCompleted 方法
public void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("Worker completed");
String result = e.Result as String;
if ((e.Error == null))
{
Debug.WriteLine("No errors");
Debug.WriteLine("Result = " + result);
if (e.Cancelled)
{
Debug.WriteLine("Worker cancelled");
}
switch ((String)e.Result.ToString())
{
case "Success":
this.Hide();
this.ShowInTaskbar = false;
Main mainForm = new Main();
mainForm.Show();
break;
case "Banned":
popupError("ERROR: License revoked!",
"Your license has been revoked, contact Time Sheep for more information. You can purchase a new one by visiting the ordinary order link."
);
break;
case "Invalid":
popupError("ERROR: Invalid serial/HWID",
"Either your serial number or hardware identifier was invalid. If you purchased UniLeech, please contact Time Sheep with proof of purchase."
);
break;
case "Unregistered":
this.Hide();
this.ShowInTaskbar = false;
Register registerForm = new Register();
registerForm.Show();
break;
default:
MessageBox.Show((string)e.Result, "ERROR");
break;
}
}
}
执行时会出现以下调试消息(案例:我有文件并且 e.Result 应该是“成功”):
UI thread running
Worker thread running
0123-1234-1234 (The value of Serial)
Worker completed
No errors
{"success": "true","action": "validate","error": "None"} (Returned from web API)
License validation was successful
Success
RunWorkerCompleted finished
我怀疑问题是DoWork方法是异步的,但我不知道如何同步使用HttpClient。
最佳答案
你不能有一个async
DoWork
。 BackgroundWorker
假定当 DoWork
返回时,它已完成。
您根本不需要 BackgroundWorker
。只需使用 Task.Run
即可。
关于c# - 为什么我的 RunWorkerCompleted 没有收到 RunWorkerCompletedEventArgs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14714730/
我可以从我的调试消息中看到 e.Result 在 DoWorker 方法中一直等于“成功”,但在 RunWorkerCompleted 方法开始时 e.Result 根本不返回任何内容。 这是 Win
大家好。我有一组运行 Worker 类实例的 BackgroundWorker 对象。当我调用 Worker 类时,对象实例会执行它,然后用完代码(循环结束)。我能够收听 RunWorkerCompl
我通过调用 BackgroundWorker 上的 CancelAsync() 方法取消我的操作,当执行落入事件 RunWorkerCompleted 时,属性 Cancelled RunWorker
我是一名优秀的程序员,十分优秀!