public string Check(string key, CheckMode checkMode, KeyDatabase keyDatabase) { key = key.ToUpper(); string result = ""; for (int j = 0; j < key.Length; j++) { char chr = key[j]; if (!(chr == '-' || (chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z'))) { return(result); } } KeyDetail detail = keyDatabase.GetKeyDetail(key); bool isKeyExistOnDB = true; if (detail == null) //Key is not exist on DB { detail = PIDChecker.Check(key); isKeyExistOnDB = false; } if (detail == null || detail.prd.Contains("Unsupported")) //Key is invalid { // result += "Key: " + key + "\n"; // result += "Error: Key is invalid or not supported\n\n"; return(result); } bool isActivationCountUpdated = false; bool isErrorCodeUpdated = false; bool isGetErrorCodeFail = false; if (!(isKeyExistOnDB && detail.activationCount < 0)) { detail.activationCount = GetRemainingActivations(detail.eid); isActivationCountUpdated = true; } if (detail.activationCount <= 0) { bool isNeedGetErrorCode = true; bool isVLKey = false; if (detail.activationCount == 0) { isVLKey = true; } if (detail.errorCode == "0xC004C003" || detail.errorCode == "0xC004C060" || detail.errorCode == "0xC004C004" || detail.errorCode == "Key Blocked") //key blocked or no remaining { isNeedGetErrorCode = false; } else { if (isKeyExistOnDB && detail.time != "") { var now = DateTime.Now; var lastTime = Utils.ConvertStringToTime(detail.time); var dt = now - lastTime; int timeToRecheck = 4; if (detail.prd.Contains("Windows 7") || detail.prd.Contains("Office14")) { timeToRecheck = 10; } if (dt.TotalMinutes <= timeToRecheck) { isNeedGetErrorCode = false; } } } if (isNeedGetErrorCode && checkMode == CheckMode.ALL_DATA) { string errorCode = this.GetErrorCodeOnServer(detail.prd, detail.key, isVLKey); if (errorCode == "") { isGetErrorCodeFail = true; } else { detail.errorCode = errorCode; isErrorCodeUpdated = true; } } } if (!isKeyExistOnDB) { detail.time = Utils.GetCurrentTime(); keyDatabase.AddKeyToDB(detail); } else if ((isErrorCodeUpdated || isActivationCountUpdated) && !isGetErrorCodeFail) { detail.time = Utils.GetCurrentTime(); keyDatabase.UpdateKeyInDB(detail); } else if (detail.time == "") { detail.time = Utils.GetCurrentTime(); } result += "Key: " + key + "\r\n"; result += "Description: " + detail.prd + "\r\n"; result += "Sub Type: " + detail.sub + "\r\n"; if (detail.activationCount >= 0) { result += "Activation Count: " + detail.activationCount.ToString() + "\r\n"; } if (detail.activationCount <= 0 && checkMode == CheckMode.ALL_DATA) { if (isGetErrorCodeFail) { result += "Error Code: Server busy, please try again later\r\n"; } else { result += "Error Code: " + detail.errorCode + "\r\n"; } } result += "Time: " + detail.time + "\r\n\r\n"; return(result); }
private void keyTableRefreshClick(object sender, EventArgs e) { if (isKeyRefreshCanceling) { return; } if (isKeyRefreshing) { isKeyRefreshCanceling = true; return; } DataGridViewSelectedCellCollection selectedCells = this.keyDataGridView.SelectedCells; HashSet <DataGridViewRow> selectedRows = new HashSet <DataGridViewRow>(); for (int i = 0; i < selectedCells.Count; i++) { selectedRows.Add(this.keyDataGridView.Rows[selectedCells[i].RowIndex]); } if (selectedRows.Count == 0) { return; } isKeyRefreshing = true; string tableName = this.keyTypeComboBox.SelectedItem.ToString(); foreach (var r in selectedRows.Reverse()) { string key = r.Cells[1].Value.ToString(); r.Cells[3].Value = "Updating.."; r.Cells[4].Value = "Updating.."; r.Cells[5].Value = "Updating.."; Thread t = new Thread(() => { keyChecker.Check(key, CheckMode.ALL_DATA, keyDatabase); }); t.Start(); while (t.IsAlive) { Application.DoEvents(); Thread.Sleep(1); } KeyDetail detail = keyDatabase.GetKeyDetail(key); if (detail != null) { r.Cells[3].Value = detail.activationCount.ToString(); r.Cells[4].Value = detail.errorCode; r.Cells[5].Value = detail.time; Application.DoEvents(); } if (isKeyRefreshCanceling) { break; } } isKeyRefreshing = false; isKeyRefreshCanceling = false; }