// 发起一次书目检索 // 发起检索成功后,调主应该用 SearchResponseEvent 事件接收检索结果 // return: // -1 出错 // 0 没有检索目标 // 1 成功发起检索 public int BeginSearchBiblio( string userNameList, #if NO string operation, string inputSearchID, string dbNameList, string queryWord, string fromList, string matchStyle, string formatList, long maxResults, #endif SearchRequest searchParam, out string outputSearchID, out string strError) { strError = ""; outputSearchID = ""; AddInfoLine("BeginSearchBiblio " + "; userNameList=" + userNameList #if NO + "; operation=" + operation + "; searchID=" + inputSearchID + "; dbNameList=" + dbNameList + "; queryWord=" + queryWord + "; fromList=" + fromList + "; matchStyle=" + matchStyle + "; formatList=" + formatList + "; maxResults=" + maxResults #endif ); try { Task <MessageResult> task = HubProxy.Invoke <MessageResult>("RequestSearchBiblio", userNameList, #if NO inputSearchID, dbNameList, queryWord, fromList, matchStyle, formatList, maxResults #endif searchParam ); #if NO while (task.IsCompleted == false) { Application.DoEvents(); Thread.Sleep(200); } #endif WaitTaskComplete(task); if (task.IsFaulted == true) { // AddErrorLine(GetExceptionText(task.Exception)); strError = GetExceptionText(task.Exception); AddInfoLine("BeginSearchBiblio inputSearchID=" + searchParam.SearchID + "; return error=" + strError + " value=" + -1); return(-1); } MessageResult result = task.Result; if (result.Value == -1) { // AddErrorLine(result.ErrorInfo); strError = result.ErrorInfo; AddInfoLine("BeginSearchBiblio inputSearchID=" + searchParam.SearchID + "; return error=" + strError + " value=" + -1); return(-1); } if (result.Value == 0) { // AddErrorLine(result.ErrorInfo); strError = result.ErrorInfo; AddInfoLine("BeginSearchBiblio inputSearchID=" + searchParam.SearchID + "; return error=" + strError + " value=" + 0); return(0); } // AddMessageLine("search ID:", result.String); outputSearchID = result.String; AddInfoLine("BeginSearchBiblio inputSearchID=" + searchParam.SearchID + "; return value=" + 1); return(1); } catch (Exception ex) { strError = ex.Message; // ExceptionUtil.GetAutoText(ex); AddInfoLine("BeginSearchBiblio inputSearchID=" + searchParam.SearchID + "; return error=" + strError + " value=" + -1); return(-1); } }
// 发起一次书目检索 // 发起检索成功后,调主应该用 SearchResponseEvent 事件接收检索结果 // return: // -1 出错 // 0 没有检索目标 // 1 成功发起检索 public int BeginSearchBiblio( string inputSearchID, string dbNameList, string queryWord, string fromList, string matchStyle, string formatList, long maxResults, out string outputSearchID, out string strError) { strError = ""; outputSearchID = ""; try { Task <MessageResult> task = HubProxy.Invoke <MessageResult>("RequestSearchBiblio", inputSearchID, dbNameList, queryWord, fromList, matchStyle, formatList, maxResults); while (task.IsCompleted == false) { Application.DoEvents(); Thread.Sleep(200); } if (task.IsFaulted == true) { // AddErrorLine(GetExceptionText(task.Exception)); strError = GetExceptionText(task.Exception); return(-1); } MessageResult result = task.Result; if (result.Value == -1) { // AddErrorLine(result.ErrorInfo); strError = result.ErrorInfo; return(-1); } if (result.Value == 0) { // AddErrorLine(result.ErrorInfo); strError = result.ErrorInfo; return(0); } // AddMessageLine("search ID:", result.String); outputSearchID = result.String; return(1); } catch (Exception ex) { strError = ex.Message; return(-1); } }
async Task DeleteUser() { string strError = ""; if (this.listView1.SelectedItems.Count == 0) { strError = "尚未选定要删除的事项"; goto ERROR1; } { DialogResult result = MessageBox.Show(this, "确实要删除选定的 " + this.listView1.SelectedItems.Count.ToString() + " 个用户?", "UserManageDialog", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.Yes) { return; } } List <User> users = new List <User>(); foreach (ListViewItem item in this.listView1.SelectedItems) { User user = (User)item.Tag; users.Add(user); } this.EnableControls(false); try { MessageResult result = await this.Connection.SetUsers("delete", users); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } finally { this.EnableControls(true); } foreach (ListViewItem item in this.listView1.SelectedItems) { this.listView1.Items.Remove(item); } this.Changed = true; return; ERROR1: this.Invoke((Action)(() => { MessageBox.Show(this, strError); } )); }
// 进行检索并得到结果 // 这是将发出和接受消息结合起来的功能比较完整的 API public Task <SearchResult> SearchAsync( string strRemoteUserName, SearchRequest request, TimeSpan timeout, CancellationToken token) { return(Task.Factory.StartNew <SearchResult>(() => { SearchResult result = new SearchResult(); if (string.IsNullOrEmpty(request.TaskID) == true) { request.TaskID = Guid.NewGuid().ToString(); } MessageResult message = HubProxy.Invoke <MessageResult>( "RequestSearch", strRemoteUserName, request).Result; if (message.Value == -1 || message.Value == 0) { result.ErrorInfo = message.ErrorInfo; result.ResultCount = -1; return result; } DateTime start_time = DateTime.Now; // 循环,取出得到的检索结果 for (; ;) { if (token != null) { token.ThrowIfCancellationRequested(); } if (DateTime.Now - start_time >= timeout) { throw new TimeoutException("已超时 " + timeout.ToString()); } SearchResult result0 = (SearchResult)_resultTable[request.TaskID]; if (result0 != null && result0.ResultCount == -1) { ClearResultFromTable(request.TaskID); return result0; } if (result0 != null && result0.Records != null && result0.Records.Count >= result0.ResultCount) { ClearResultFromTable(request.TaskID); return result0; } if (result0 != null && result0.Finished == true) { ClearResultFromTable(request.TaskID); return result0; } Thread.Sleep(200); } }, token)); }
async Task ModifyUser() { string strError = ""; if (this.listView1.SelectedItems.Count == 0) { strError = "尚未选定要修改的事项"; goto ERROR1; } ListViewItem item = this.listView1.SelectedItems[0]; UserDialog dlg = new UserDialog(); dlg.Font = this.Font; dlg.ChangeMode = true; dlg.UserItem = (User)item.Tag; dlg.ShowDialog(this); if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel) { return; } if (dlg.Changed == false && dlg.ChangePassword == false) { MessageBox.Show(this, "没有发生修改"); return; } List <User> users = new List <User>(); User user = dlg.UserItem; users.Add(user); this.EnableControls(false); try { if (dlg.Changed == true) { MessageResult result = await this.Connection.SetUsers("change", users); if (result.Value == -1) { strError = result.ErrorInfo; // 如果这里返回出错仅仅是因为权限不够,还需要尝试继续执行后面的修改密码的操作 if (result.String != "Denied") { goto ERROR1; } } } if (dlg.ChangePassword) { MessageResult result = await this.Connection.SetUsers("changePassword", users); if (result.Value == -1) { if (string.IsNullOrEmpty(strError) == false) { strError += "; "; } strError += result.ErrorInfo; goto ERROR1; } } if (string.IsNullOrEmpty(strError) == false) { goto ERROR1; } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } finally { this.EnableControls(true); } ChangeItem(item, dlg.UserItem); this.Changed = true; return; ERROR1: this.Invoke((Action)(() => { MessageBox.Show(this, strError); } )); }