private void btnReg_Click(object sender, EventArgs e) { string username = txtUser.Text; string password = txtPsw.Text; string repassword = txtRePsw.Text; if (password != repassword) { labInfo.Text = "两次密码输入不一致!"; txtRePsw.Focus(); return; } //拼接url string strURL = URL.GetUrl_UserRegister(username, password); //异步HTTP请求 AsyncHttpRequester ahr = new AsyncHttpRequester(); ahr.PostExecute += (result) => { //获取Json数据 ActionJson ajson = new ActionJson(result); //成功 if (ajson.flag) { //跨线程修改UI MethodInvoker mi = new MethodInvoker(() => { //显示信息 labInfo.ForeColor = Color.LimeGreen; labInfo.Text = "新用户注册成功"; Thread.Sleep(500); //关闭界面 this.Hide(); this.Dispose(); }); this.BeginInvoke(mi); } //失败 else { //提示登陆错误 MessageBox.Show(ajson.message); //跨线程修改UI MethodInvoker mi = new MethodInvoker(() => { btnReg.Enabled = true; }); this.BeginInvoke(mi); } }; btnReg.Enabled = false; ahr.PostHttpResponse(strURL, null); }
private void login() { string username = txtUser.Text; string password = txtPsw.Text; //拼接url string strURL = URL.GetUrl_UserLogin(username, password); //异步HTTP请求 AsyncHttpRequester ahr = new AsyncHttpRequester(); ahr.PostExecute += (result) => { //获取Json数据 ActionJson ajson = new ActionJson(result); //登陆成功 if (ajson.flag) { //content中包含用户token和根目录id JObject json = JObject.Parse(ajson.content); //将用户的数据保存 UserInstance.Instance.Token = json["token"].ToString(); UserInstance.Instance.RootFolderID = (int)json["rootfolder"]; //保存用户名到配置文件中 UserInstance.Instance.Configuration["Username"] = txtUser.Text; //跨线程修改UI MethodInvoker mi = new MethodInvoker(() => { //打开主界面 this.Hide(); MainForm form = new MainForm(); form.Show(); }); this.BeginInvoke(mi); } //登录失败 else { //提示登陆错误 MessageBox.Show(ajson.message); //跨线程修改UI MethodInvoker mi = new MethodInvoker(() => { btnLogin.Enabled = true; }); this.BeginInvoke(mi); } }; btnLogin.Enabled = false; ahr.PostHttpResponse(strURL, null); }
/// <summary> /// 读取指定文件夹ID的文件夹内容 /// </summary> private void readFolderMenu(int folderid) { //异步HTTP请求 AsyncHttpRequester ahr = new AsyncHttpRequester(); ahr.PostExecute += (result) => { ActionJson ajson = new ActionJson(result); if (ajson.flag) { //获取文件夹json数据 JObject jsonContent = JObject.Parse(ajson.content); //读取文件夹json数组 JArray jsonFolders = (JArray)jsonContent.GetValue("folder"); //读取文件json数组 JArray jsonFiles = (JArray)jsonContent.GetValue("file"); //跨线程修改UI MethodInvoker mi = new MethodInvoker(() => { //清空文件图标 flpCurrentFolder.Controls.Clear(); //读取文件夹json数组数据,并添加文件夹图标 for (int i = 0; i < jsonFolders.Count; i++) { JObject jsonFolder = (JObject)jsonFolders[i]; FileItem item = new FileItem(FileItem.Type.FOLDER, jsonFolder, this); flpCurrentFolder.Controls.Add(item); } //将读取到的文件信息显示在界面上 for (int i = 0; i < jsonFiles.Count; i++) { //读取文件json数组数据,并添加文件图标 JObject jsonFile = (JObject)jsonFiles[i]; FileItem item = new FileItem(FileItem.Type.FILE, jsonFile, this); flpCurrentFolder.Controls.Add(item); } System.Windows.Forms.Application.DoEvents(); }); this.BeginInvoke(mi); } else { MessageBox.Show(ajson.message); } }; ahr.PostHttpResponse(URL.GetUrl_FolderMenu(folderid), null); }