private void UpdateUi(UpdateUiDelegate d, object state = null) { _syncContext.Post(o => { d(); }, state); }
public MainForm() { InitializeComponent(); spex = new SerialPortEx(); splashScreen = new SplashScreenForm(3); connectBoard = new ConnectBoardForm(spex.Port); scmd = new SerialCommand(); spex.AsyncDataReceiveHandler += new SerialPortAsyncDataReceiveHandler(ComReceiveDataEvent); spex.DisableSerialPortAsycnReceive(); update_ui_delegate = new UpdateUiDelegate(UpdateUiEvent); }
public void DoBatchJob(BindingList<DanbooruBatchJob> batchJob) { ToggleBatchJobButtonDelegate bjd = new ToggleBatchJobButtonDelegate(ToggleBatchJobButton); UpdateUiDelegate del = new UpdateUiDelegate(UpdateUi); UpdateUiDelegate2 del2 = new UpdateUiDelegate2(UpdateUi); ExtendedWebClient _clientPost = new ExtendedWebClient(); if (batchJob != null) { UpdateStatus2("Starting Batch Job"); for (int i = 0; i < batchJob.Count; i++) { batchJob[i].CurrentPage = 0; if (!batchJob[i].isCompleted) { UpdateLog("DoBatchJob", "Processing Batch Job#" + i); DanbooruPostDao prevDao = null; bool flag = true; int currPage = 0; int postCount = 0; do { // stop/pause event handling outside _pauseEvent.WaitOne(Timeout.Infinite); if (_shutdownEvent.WaitOne(0)) { batchJob[i].Status = " ==> Stopped."; // toggle button BeginInvoke(bjd, new object[] { true }); UpdateLog("DoBatchJob", "Batch Job Stopped."); UpdateStatus2("Batch Job Stopped."); return; } DanbooruPostDao d = null; int imgCount = 0; int skipCount = 0; string url; string query = ""; #region Construct the searchParam if (batchJob[i].Provider.BoardType == BoardType.Danbooru || batchJob[i].Provider.BoardType == BoardType.Shimmie2) { currPage = batchJob[i].CurrentPage; } else if (batchJob[i].Provider.BoardType == BoardType.Gelbooru) { if (batchJob[i].Provider.Preferred == PreferredMethod.Html) { currPage = batchJob[i].CurrentPage * postCount; } else { currPage = batchJob[i].CurrentPage; } } DanbooruSearchParam searchParam = GetSearchParamsFromJob(batchJob[i], currPage); url = batchJob[i].Provider.GetQueryUrl(searchParam); #endregion Construct the searchParam try { #region Get and load the image list batchJob[i].Status = "Getting list for page: " + searchParam.Page; BeginInvoke(del); UpdateLog("DoBatchJob", "Downloading list: " + url); d = GetBatchImageList(url, query, batchJob[i]); if (d == null) { // Cannot get list. UpdateLog("DoBatchJob", "Cannot load list"); batchJob[i].Status = "Cannot load list."; batchJob[i].isCompleted = false; batchJob[i].isError = true; flag = false; } else if (d.Posts == null || d.Posts.Count == 0) { // No more image UpdateLog("DoBatchJob", "No more image."); batchJob[i].Status = "No more image."; flag = false; //break; } else { if (prevDao != null) { // identical data returned, probably no more new image. if (prevDao.RawData != null && prevDao.RawData.Equals(d.RawData)) { UpdateLog("DoBatchJob", "Identical list, probably last page."); batchJob[i].Status = "Identical list, probably last page."; flag = false; //break; } } prevDao = d; batchJob[i].Total = d.PostCount; batchJob[i].CurrentPageTotal = d.Posts.Count; batchJob[i].CurrentPageOffset = d.Offset; #endregion Get and load the image list postCount = d.Posts.Count; foreach (DanbooruPost post in d.Posts) { // Update progress bar object[] myArray = new object[2]; myArray[0] = batchJob[i].ProcessedTotal; myArray[1] = d.PostCount < batchJob[i].Limit ? d.PostCount : batchJob[i].Limit; BeginInvoke(del2, myArray); // thread handling _pauseEvent.WaitOne(Timeout.Infinite); if (_shutdownEvent.WaitOne(0)) { batchJob[i].Status = " ==> Stopped."; // toggle button BeginInvoke(bjd, new object[] { true }); UpdateLog("DoBatchJob", "Batch Job Stopped."); UpdateStatus2("Batch Job Stopped."); return; } // check if have url and post is not deleted if (string.IsNullOrWhiteSpace(post.FileUrl) && (post.Status != "deleted" || chkProcessDeletedPost.Checked)) { ResolveFileUrlBatch(_clientPost, post); } //Choose the correct urls var targetUrl = post.FileUrl; if (_ImageSize == "Thumb" && !String.IsNullOrWhiteSpace(post.PreviewUrl)) { targetUrl = post.PreviewUrl; } else if (_ImageSize == "Jpeg" && !String.IsNullOrWhiteSpace(post.JpegUrl)) { targetUrl = post.JpegUrl; } else if (_ImageSize == "Sample" && !String.IsNullOrWhiteSpace(post.SampleUrl)) { targetUrl = post.SampleUrl; } batchJob[i].Status = "Downloading: " + targetUrl; BeginInvoke(del); //if (post.Provider == null) post.Provider = cbxProvider.Text; //if (post.Query == null) post.Query = txtQuery.Text; //if (post.SearchTags == null) post.SearchTags = txtTags.Text; bool download = true; // check if blacklisted if (download && post.Hidden) { ++skipCount; ++batchJob[i].Skipped; download = false; UpdateLog("DoBatchJob", "Download skipped, contains blacklisted tag: " + post.Tags + " Url: " + targetUrl); } string filename = ""; if (download && !string.IsNullOrWhiteSpace(targetUrl)) { var format = new DanbooruFilenameFormat() { FilenameFormat = batchJob[i].SaveFolder, Limit = Convert.ToInt32(txtFilenameLength.Text), BaseFolder = txtSaveFolder.Text, MissingTagReplacement = txtTagReplacement.Text, ArtistGroupLimit = Convert.ToInt32(txtArtistTagGrouping.Text), CharacterGroupLimit = Convert.ToInt32(txtCharaTagGrouping.Text), CopyrightGroupLimit = Convert.ToInt32(txtCopyTagGrouping.Text), CircleGroupLimit = Convert.ToInt32(txtCircleTagGrouping.Text), FaultsGroupLimit = Convert.ToInt32(txtFaultsTagGrouping.Text), IgnoredTags = DanbooruTagsDao.Instance.ParseTagsString(txtIgnoredTags.Text.Replace(Environment.NewLine, " ")), IgnoredTagsRegex = txtIgnoredTags.Text.Trim().Replace(Environment.NewLine, "|"), IgnoreTagsUseRegex = chkIgnoreTagsUseRegex.Checked, IsReplaceMode = chkReplaceMode.Checked, IgnoredTagsOnlyForGeneral = chkIgnoreForGeneralTag.Checked, TagReplaceUnderscoreToSpace = chkIsReplaceUnderscoreTag.Checked }; string extension = Helper.getFileExtensions(targetUrl); filename = Helper.MakeFilename(format, post) + extension; } // check if exist if (download && !chkOverwrite.Checked) { if (File.Exists(filename)) { ++skipCount; ++batchJob[i].Skipped; download = false; UpdateLog("DoBatchJob", "Download skipped, file exists: " + filename); } } if (download && String.IsNullOrWhiteSpace(targetUrl)) { ++skipCount; ++batchJob[i].Skipped; download = false; UpdateLog("DoBatchJob", "Download skipped, ID: " + post.Id + " No file_url, probably deleted"); } Uri uri = null; if (download && !Uri.TryCreate(targetUrl, UriKind.RelativeOrAbsolute, out uri)) { ++skipCount; ++batchJob[i].Skipped; download = false; UpdateLog("DoBatchJob", "Download skipped, ID: " + post.Id + " Invalid URL: " + targetUrl); } #region download if (download) { imgCount = DoDownloadBatch(targetUrl, batchJob[i], post, filename); } #endregion download // check if more than available post if (batchJob[i].ProcessedTotal >= d.PostCount && d.PostCount != 0) { UpdateLog("DoBatchJob", "No more post."); flag = false; break; } // check if over given limit if (batchJob[i].ProcessedTotal >= batchJob[i].Limit) { UpdateLog("DoBatchJob", "Limit Reached."); flag = false; break; } // check batch job delay int delay = 0; Int32.TryParse(Properties.Settings.Default.BatchJobDelay, out delay); if ((Properties.Settings.Default.DelayIncludeSkipped || download) && delay > 0) { UpdateLog("DoBatchJob", String.Format("Waiting for {0}ms for the next post.", delay)); Thread.Sleep(delay); } } } batchJob[i].Status = " ==> Done."; } catch (Exception ex) { string message = ex.Message; string responseMessage = ""; if (ex.InnerException != null) { message += Environment.NewLine + "Inner: " + ex.InnerException.Message; } message += Environment.NewLine + "Stack Trace: " + Environment.NewLine + ex.StackTrace; message += Environment.NewLine + "Query: " + batchJob[i].TagQuery; batchJob[i].isError = true; batchJob[i].isCompleted = false; if (ex.GetType() == typeof(System.Net.WebException)) { System.Net.WebException wex = (System.Net.WebException)ex; if (wex.Status == WebExceptionStatus.ProtocolError && wex.Response.Headers.AllKeys.Contains("Status") && wex.Response.Headers["Status"].ToString() == "500") { using (var response = wex.Response.GetResponseStream()) { if (response != null) { var option = new DanbooruPostDaoOption() { Provider = _currProvider, Query = query, SearchTags = batchJob[i].TagQuery, Url = url, Referer = "", BlacklistedTags = TagBlacklist, BlacklistedTagsRegex = TagBlacklistRegex, BlacklistedTagsUseRegex = chkBlacklistTagsUseRegex.Checked, IgnoredTags = TagIgnore, IgnoredTagsRegex = TagIgnoreRegex, IgnoredTagsUseRegex = chkIgnoreTagsUseRegex.Checked, IsBlacklistOnlyForGeneral = chkBlacklistOnlyGeneral.Checked }; var resp = new DanbooruPostDao(response, option); responseMessage = resp.ResponseMessage; flag = false; } } } } if (ex.Message.Contains("(400)") || ex.Message.Contains("(403)") || ex.Message.Contains("(500)") || ex.Message.Contains("resolved")) { flag = false; } batchJob[i].Status = " ==> Error: " + (string.IsNullOrWhiteSpace(responseMessage) ? ex.Message : responseMessage) + Environment.NewLine; if (!string.IsNullOrWhiteSpace(responseMessage)) UpdateLog("DoBatchJob", "Server Message: " + responseMessage, ex); else UpdateLog("DoBatchJob", "Error: " + message, ex); if (cbxAbortOnError.Checked) { MessageBox.Show(message, "Batch Download"); break; } } finally { BeginInvoke(del); { // Update progress bar object[] myArray = new object[2]; myArray[0] = batchJob[i].ProcessedTotal; if (d != null) { myArray[1] = d.PostCount < batchJob[i].Limit ? d.PostCount : batchJob[i].Limit; } else { myArray[1] = -1; } BeginInvoke(del2, myArray); } } ++batchJob[i].CurrentPage; } while (flag); UpdateLog("DoBatchJob", "Batch Job #" + i + ": Done"); if (batchJob[i].isError) { batchJob[i].isCompleted = false; } else { batchJob[i].isCompleted = true; } BeginInvoke(del); } } } BeginInvoke(bjd, new object[] { true }); UpdateStatus2("Batch Job Completed!", true); { // hide progress bar object[] myArray = new object[2]; myArray[0] = -1; myArray[1] = -1; BeginInvoke(del2, myArray); } }
public MainWindow() { InitializeComponent(); //Initialize The Colorscheme var materialSkinManager = MaterialSkinManager.Instance; materialSkinManager.AddFormToManage(this); //Loads colorscheme if (Properties.Settings.Default.nightMode == true) { materialSkinManager.Theme = MaterialSkinManager.Themes.DARK; } else { materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT; } //Load saved colorscheme if (Properties.Settings.Default.colorscheme == "bluegrey") { materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE); } else if (Properties.Settings.Default.colorscheme == "pink") { materialSkinManager.ColorScheme = new ColorScheme(Primary.Pink800, Primary.Pink900, Primary.Pink500, Accent.Pink200, TextShade.WHITE); } //Loads saved settings emuDir = Properties.Settings.Default.EmuDir; addgamesDir = Properties.Settings.Default.gamesDir; //Initilization isoPanel.AutoScroll = true; isoPanel.AllowDrop = true; UpdateUiEvent += new UpdateUiDelegate(addIso); artScrapper.WorkerSupportsCancellation = true; QueueThread.WorkerSupportsCancellation = true; QueueThread.WorkerReportsProgress = true; QueueThread.DoWork += QueueThread_DoWork; //QueueThread.RunWorkerCompleted += QueueThread_DoWork; //Loads last window size if ((Properties.Settings.Default.lastSize == "null") == false) { string _size = Properties.Settings.Default.lastSize; _size = _size.Replace("{Width=", String.Empty); _size = _size.Replace("}", String.Empty); _size = _size.Replace(" Height=", String.Empty); this.Width = Convert.ToInt32(_size.Split(',')[0]); this.Height = Convert.ToInt32(_size.Split(',')[1]); } //Creates required directories Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\resources\configs\"); Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\resources\logs\"); //If blacklist file doesn't exist, create one if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\resources\logs\blacklist.txt") == false) { FileStream newblacklist = null; newblacklist = File.Create(AppDomain.CurrentDomain.BaseDirectory + @"\resources\logs\blacklist.txt"); newblacklist.Close(); } //Adds items to region list regionList.Add("SLUS"); regionList.Add("SCUS"); regionList.Add("SCES"); regionList.Add("SLES"); regionList.Add("SCPS"); regionList.Add("SLPS"); regionList.Add("SLPM"); regionList.Add("PSRM"); regionList.Add("SCED"); regionList.Add("SLPM"); regionList.Add("SIPS"); //Integrity Checks if (emuDir == "null") { FirstTimeSetup(true); } else { copyDLL(); //Searches game folders in resources directory string[] gamesDir = Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory + @"\resources\configs\"); foreach (string dir in gamesDir) { //Removes symbols from game title string _title = dir; string _name = dir.Remove(0, dir.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1); _name = _name.Trim(new Char[] { ' ', '*', '.', '\\', '/' }); //_name = _name.Remove(0, dir.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1); if (File.Exists(_title + @"\art.jpg")) { var gameIni = new IniFile(_title + @"\spectabis.ini"); var _isoDir = gameIni.Read("isoDirectory", "Spectabis"); if (File.Exists(_isoDir)) { //Creates a group for game tile FlowLayoutPanel gameGroupBox = new FlowLayoutPanel(); gameGroupBox.FlowDirection = FlowDirection.TopDown; gameGroupBox.AutoSize = true; gameGroupBox.WrapContents = true; gameGroupBox.Name = _name; //Adds file to game file list gamelist.Add(_isoDir); //Creates a game picture PictureBox gameBox = new PictureBox(); gameBox.Height = 200; gameBox.Width = 150; gameBox.SizeMode = PictureBoxSizeMode.StretchImage; gameBox.ImageLocation = _title + @"\art.jpg"; //isoPanel.Controls.Add(gameBox); gameBox.MouseDown += gameBox_Click; gameBox.Tag = _isoDir; gameBox.Name = _name; gameGroupBox.Controls.Add(gameBox); //If showtitle is selected, create a label object if (Properties.Settings.Default.showtitle == true) { MaterialLabel gamelabel = new MaterialLabel(); gamelabel.Text = _name; gamelabel.Width = 180; gameGroupBox.Controls.Add(gamelabel); } isoPanel.Controls.Add(gameGroupBox); Debug.WriteLine(_name + " has been added"); } } } } //Empty list cannot be foreached gamelist.Add("null"); //Every startup look for new .iso files if (addgamesDir != "null") { scanDir(); } }