public static BitmapImage ToBitmapImage(this Bitmap bitmap) { if (bitmap == null) { return(null); } BitmapImage bImg = null; try { ThreadDispatcher.Invoke(() => { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); bImg = new BitmapImage(); ms.Position = 0; bImg.BeginInit(); bImg.StreamSource = ms; bImg.EndInit(); }); return(bImg); } catch { return(null); } }
protected void NotifyPropertyChanging([CallerMemberName] string propertyName = "") { if (PropertyChanging != null) { ThreadDispatcher.Invoke(() => PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName))); } }
public void Dispatch(Action action) { if (IsReadyToDispatch) { ThreadDispatcher.Invoke(DispatcherPriority.Send, new TimeSpan(0, 0, 0, 0, 500), action); } else { _queuedActions.Enqueue(action); } }
void manager_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { e.Handled = true; ThreadDispatcher.Invoke(() => { try { MessageBox.Show(e.Exception.ToString()); } catch { } }); }
/// <summary> /// CommentString the item. /// </summary> private void CommentItem() { try { var postData = "comment_text=" + this.comment; this.httpContainer.InstagramPost(string.Format(CommentUri, this.imageId), this.csrfToken, this.referrer, postData, true); } catch (InstagramCommentException ex) { ThreadDispatcher.Invoke(() => this.CurrentContext.HandleException(ex)); ThreadDispatcher.Invoke(() => this.CurrentContext.Comment = false); this.log.Error(ex.Message); } }
private void RunTask() { try { _method.Invoke(_progress); } catch (Exception e) { ThreadDispatcher.Invoke(() => MessageBox.Show(e.Message)); } finally { ThreadDispatcher.Invoke(Close); } }
/// <summary> /// Unloads the loaded <see cref="GLFW" /> interface implementation. /// </summary> public static void Unload() { GLFW.Value.Terminate(); GLFW = new Lazy <Glfw>(() => { var glfw = Glfw.GetApi(); ThreadDispatcher.Invoke(() => { glfw.Init(); glfw.SetErrorCallback(Glfw.ErrorCallback); }); return(glfw); }); }
/// <summary> /// Main strategy function. Explores instagram by keyword, likes, comments, followes /// based on settings coming from UI. /// </summary> public void Explore() { try { ThreadDispatcher.Invoke(() => this.CurrentContext.ProcessState = ProcessState.Running); if (!this.httpContainer.InstagramLogin(this.CurrentContext.UserName, this.CurrentContext.Password)) { throw new InstagramException("An error occured during login!"); } this.ExploreKeywords(); } catch (Exception ex) { ThreadDispatcher.Invoke(() => this.CurrentContext.HandleException(ex)); } }
/// <summary> /// Creates a new instance of the GlfwProvider class. /// </summary> static GlfwProvider() { ThreadDispatcher = new Dispatcher(); GLFW = new Lazy <Glfw>(() => { if (ThreadDispatcher == null) { ThreadDispatcher = new Dispatcher(); } var glfw = Glfw.GetApi(); ThreadDispatcher.Invoke(() => { glfw.Init(); glfw.SetErrorCallback(Glfw.ErrorCallback); }); return(glfw); }); }
/// <summary> /// Explores the keywords. /// </summary> private void ExploreKeywords() { foreach (var keyword in this.CurrentContext.Keywords.Split('|')) { this.currentPage = 1; this.log.Info("Working on keyword: " + keyword); if (this.ProcessStopped()) { return; } var exploreResponse = this.httpContainer.InstagramGet(string.Format(ExploreUri, keyword)); if (exploreResponse == string.Empty) { continue; } var mediaJson = Regex.Matches(exploreResponse, "<script type=\"text/javascript\">window._sharedData =(.*?);</script>")[0].Groups[1] .Value; var keywordCsrf = Regex.Match(exploreResponse.Replace(" ", string.Empty), "\"csrf_token\":\"(\\w+)\"").Groups[1].Value; dynamic dyn = JsonConvert.DeserializeObject(mediaJson); foreach (var node in dyn.entry_data.TagPage[0].tag.media.nodes) { this.log.Info("Working on image: " + node.code.ToString()); if (this.ProcessStopped()) { return; } this.SetNewImage(node); this.imageCode = node.code.ToString(); this.GetDetails(); Thread.Sleep(this.GetRandomTimeout()); } if (this.CurrentContext.Paging && this.currentPage < this.CurrentContext.MaxPages && Convert.ToBoolean(dyn.entry_data.TagPage[0].tag.media.page_info.has_next_page)) { this.currentPage++; var postData = PageQueryPostString.Replace("%0%", keyword) .Replace("%1%", dyn.entry_data.TagPage[0].tag.media.page_info.end_cursor.ToString()); var json = this.httpContainer.InstagramPost(QueryUri, keywordCsrf, string.Format(ExploreUri, keyword), postData); if (json == string.Empty) { continue; } dyn = JsonConvert.DeserializeObject(json); while (Convert.ToBoolean(dyn.media.page_info.has_next_page) == true) { foreach (var node in dyn.media.nodes) { this.log.Info("Working on image: " + node.code.ToString()); if (this.ProcessStopped()) { return; } this.SetNewImage(node); this.imageCode = node.code.ToString(); this.GetDetails(); Thread.Sleep(this.GetRandomTimeout()); } if (this.ProcessStopped()) { return; } Thread.Sleep(this.GetRandomTimeout()); this.currentPage++; postData = PageQueryPostString.Replace("%0%", keyword) .Replace("%1%", dyn.media.page_info.end_cursor.ToString()); json = this.httpContainer.InstagramPost(QueryUri, keywordCsrf, string.Format(ExploreUri, keyword), postData); if (json == string.Empty) { continue; } dyn = JsonConvert.DeserializeObject(json); } } } this.log.Info("Finished."); ThreadDispatcher.Invoke(() => this.CurrentContext.ProcessState = ProcessState.Finished); }
/// <summary> /// Sets the current image in main window (thread safe). /// </summary> /// <param name="node">The node.</param> private void SetNewImage(dynamic node) { var imageUri = node.display_src.ToString().Replace("\\", string.Empty); ThreadDispatcher.Invoke(() => this.CurrentContext.UpdateCurrentImage(imageUri)); }
protected override void ClearItems() { ThreadDispatcher.Invoke(() => base.ClearItems()); }
protected override void SetItem(int index, T item) { ThreadDispatcher.Invoke(() => base.SetItem(index, item)); }
protected override void RemoveItem(int index) { ThreadDispatcher.Invoke(() => base.RemoveItem(index)); }
protected override void MoveItem(int oldIndex, int newIndex) { ThreadDispatcher.Invoke(() => base.MoveItem(oldIndex, newIndex)); }
public void Unfollow(bool all) { try { ThreadDispatcher.Invoke(() => this.CurrentContext.ProcessState = ProcessState.Running); if (!this.httpContainer.InstagramLogin(this.CurrentContext.UserName, this.CurrentContext.Password)) { throw new InstagramException("An error occured during login!"); } var detailResponse = this.httpContainer.InstagramGet(string.Format(UserUri, this.CurrentContext.UserName)); if (detailResponse == string.Empty) { throw new InstagramException("Could not get followed users from instagram!"); } this.referrer = string.Format(UserUri, this.CurrentContext.UserName); this.csrfToken = Regex.Match(detailResponse.Replace(" ", string.Empty), "\"csrf_token\":\"(\\w+)\"").Groups[1].Value; var profileJson = Regex.Matches(detailResponse, "<script type=\"text/javascript\">window._sharedData =(.*?);</script>")[0].Groups[1].Value; dynamic dyn = JsonConvert.DeserializeObject(profileJson); dynamic dynPage = JsonConvert.DeserializeObject(dyn.entry_data.ProfilePage[0].ToString()); this.userId = dynPage.user.id.ToString(); var postData = FollowingQueryPostString.Replace("%0%", this.userId); var json = this.httpContainer.InstagramPost(QueryUri, this.csrfToken, string.Format(UserUri, this.CurrentContext.UserName), postData); if (json == string.Empty) { throw new InstagramException("Could not get followed users from instagram!"); } dyn = JsonConvert.DeserializeObject(json); foreach (var node in dyn.follows.nodes) { var pic = node.profile_pic_url.ToString(); ThreadDispatcher.Invoke(() => this.CurrentContext.UpdateCurrentImage(pic)); if (this.ProcessStopped()) { return; } Thread.Sleep(this.GetRandomTimeout()); if (all || !this.Follows(node.username.ToString())) { this.log.Info("Unfollwing user: "******"%0%", this.userId).Replace("%1%", dyn.follows.page_info.end_cursor.ToString()); json = this.httpContainer.InstagramPost(QueryUri, this.csrfToken, string.Format(UserUri, this.CurrentContext.UserName), postData); if (json == string.Empty) { throw new InstagramException("Could not get followed users from instagram!"); } dyn = JsonConvert.DeserializeObject(json); foreach (var node in dyn.follows.nodes) { var pic = node.profile_pic_url.ToString(); ThreadDispatcher.Invoke(() => this.CurrentContext.UpdateCurrentImage(pic)); if (this.ProcessStopped()) { return; } Thread.Sleep(this.GetRandomTimeout()); if (all || !this.Follows(node.username.ToString())) { this.log.Info("Unfollwing user: "******"Finished."); ThreadDispatcher.Invoke(() => this.CurrentContext.ProcessState = ProcessState.Finished); } catch (Exception ex) { ThreadDispatcher.Invoke(() => this.CurrentContext.HandleException(ex)); } }