public void Enablebuttons() { Action <String> AsyncUIDelegate = delegate(string n) { ChooseFilesButton.Enabled = true; StartButton.Enabled = true; ClearFilesButton.Enabled = true; ChooseFilesButton.Enabled = true; FilesListBox.Enabled = true; }; ResultListBox.Invoke(AsyncUIDelegate, new object[] { "" }); }
private void Start() { try { int TotalDuration = 0; int INTERCVAL = 0; int START = 0; int STOP = 0; INTERCVAL = int.Parse(IntervalTextBox.Text); START = int.Parse(StartTextBox.Text); STOP = int.Parse(StopTextBox.Text); int NowDuration = 0; if (START == 0 && STOP == 0) { foreach (string file in FilesListBox.Items) { ACRCloudExtrTool ACRET = new ACRCloudExtrTool(); TotalDuration = TotalDuration + ACRET.GetDurationMillisecondByFile(file); STOP = TotalDuration; } } else { TotalDuration = (TotalDuration + STOP - START) * 1000; } if (INTERCVAL < 5) { MessageBox.Show("Interval must be greater than 5 seconds"); } string configtext = System.IO.File.ReadAllText(@"acrcloud_config.json"); dynamic configjson = JsonConvert.DeserializeObject(configtext); string host = configjson.host; string access_key = configjson.access_key; string access_secret = configjson.access_secret; var config = new Dictionary <string, object>(); config.Add("host", host.Trim()); config.Add("access_key", access_key.Trim()); config.Add("access_secret", access_secret.Trim()); config.Add("timeout", 10); // seconds ACRCloudRecognizer re = new ACRCloudRecognizer(config); foreach (string file in FilesListBox.Items) { int retry = 3; for (; START < STOP; START += INTERCVAL) { if (host.Trim() == "") { MessageBox.Show("Host can not be empty"); Enablebuttons(); break; } if (access_key.Trim() == "") { MessageBox.Show("Key can not be empty"); Enablebuttons(); break; } if (access_secret.Trim() == "") { MessageBox.Show("Secret can not be empty"); Enablebuttons(); break; } string result = re.RecognizeByFile(file, START); dynamic stuff; try { stuff = JsonConvert.DeserializeObject(result); } catch (Exception) { stuff = JsonConvert.DeserializeObject(ACRCloudStatusCode.NO_RESULT); } int code; try { code = stuff.status.code; } catch (Exception) { code = 1001; } if (result == "empty") { if (file.Equals(FilesListBox.Items[FilesListBox.Items.Count - 1].ToString())) { Action <String> AsyncUIDelegate = delegate(string n) { ExportResultsButton.Enabled = true; ProgressBar.Value = ProgressBar.Maximum; }; ResultListBox.Invoke(AsyncUIDelegate, new object[] { "" }); } START = 0; break; } else if (code == 3000) { retry -= 1; if (retry == 0) { if (file.Equals(FilesListBox.Items[FilesListBox.Items.Count - 1].ToString())) { MessageBox.Show("Please Check Your Network"); Action <String> AsyncUIDelegate = delegate(string n) { ExportResultsButton.Enabled = true; ProgressBar.Value = 0; }; ResultListBox.Invoke(AsyncUIDelegate, new object[] { "" }); Enablebuttons(); } break; } } else { if (code == 0) { dynamic metadata = stuff.metadata; Data d = new Data(); d = resdata(metadata, file, START); SaveData.Save(row_i, d); row_i++; result = START.ToString() + "\t" + d.Custom_Files_Title + "\t" + d.Title; } else if (code == 1001) { result = START.ToString() + "\t" + "NoResult"; } else if (code == 3001) { MessageBox.Show("Missing/Invalid Access Key"); Enablebuttons(); break; } else if (code == 3002) { MessageBox.Show("Invalid ContentType. valid Content-Type is multipart/form-data"); Enablebuttons(); break; } else if (code == 3003) { MessageBox.Show("Limit exceeded"); Enablebuttons(); break; } else if (code == 3006) { MessageBox.Show("Invalid parameters"); Enablebuttons(); break; } else if (code == 3014) { MessageBox.Show("InvalidSignature"); Enablebuttons(); break; } else if (code == 3015) { MessageBox.Show("Could not generate fingerprint"); Enablebuttons(); break; } else { result = START.ToString() + "\t" + stuff.status.msg; } Action <String> AsyncUIDelegate = delegate(string n) { ResultListBox.Items.Add(result); ResultListBox.Refresh(); ResultListBox.SelectedIndex = this.ResultListBox.Items.Count - 1; }; ResultListBox.Invoke(AsyncUIDelegate, new object[] { "" }); Action <String> pb = delegate(string n) { float ProgressBarValue = (float)NowDuration / (float)TotalDuration * 1000000; if (ProgressBarValue > 1000) { this.ProgressBar.Value = 1000 - INTERCVAL; } else { this.ProgressBar.Value = (int)ProgressBarValue; } }; this.ProgressBar.Invoke(pb, new object[] { "" }); } NowDuration += INTERCVAL; } } Action <String> AsyncUI = delegate(string n) { ProgressBar.Value = ProgressBar.Maximum; ExportResultsButton.Enabled = true; }; ExportResultsButton.Invoke(AsyncUI, new object[] { "" }); MessageBox.Show("Done!"); Enablebuttons(); } catch (Exception e) { MessageBox.Show(e.ToString()); } }