1

デバッグ メッセージから、DoWorker メソッド全体で e.Result が「Success」に等しいことがわかりますが、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 を同期的に使用する方法がわかりません。

4

1 に答える 1

4

を持つことはできませんasync DoWork。戻っBackgroundWorkerた時点で完了とします。DoWork

まったく必要ありませんBackgroundWorkerTask.Run代わりに使用してください。

于 2013-02-05T18:56:53.320 に答える