private void DisposeManagedResources() { try { Stop(); // Timer var dtStart = DateTime.Now; var timeSpan = new TimeSpan(); while (IsExecute && timeSpan.TotalMilliseconds <= WaitCloseMiliSeconds) { Application.DoEvents(); Thread.Sleep(25); timeSpan = DateTime.Now - dtStart; } } catch (Exception exception) { InvokeControl.SetText(_richTextBox, @"Error DisposeManagedResources! " + Environment.NewLine + exception.Message); } finally { _objectMemorySize = null; _richTextBox = null; } }
public void Select_DoesNotThrow() { Utils.MethodStart(); while (_controls.TryDequeue(out Control control)) { Assert.DoesNotThrow(() => InvokeControl.Select(control)); Assert.DoesNotThrowAsync(async() => await Task.Run(() => InvokeControl.Select(control))); } Utils.MethodComplete(); }
private void StartProgress() { InvokeProgressBar.SetValue(progressBar, 0); InvokeControl.SetText(labelValue, $"{progressBar.Value} %"); while (progressBar.Value < 100) { InvokeProgressBar.SetValue(progressBar, progressBar.Value + 1); InvokeControl.SetText(labelValue, $"{progressBar.Value} %"); System.Threading.Thread.Sleep(100); } InvokeControl.SetText(labelValue, $"{progressBar.Value} %"); }
private async void buttonMoreMemory_Click(object sender, EventArgs e) { await Task.Run(() => { InvokeControl.SetText(richTextBoxMoreMemory, string.Empty); var str = string.Empty; for (var i = 1; i <= Convert.ToInt32(numericUpDown.Value * 1024); i++) { str += i + @" "; } InvokeControl.AddText(richTextBoxMoreMemory, str); }); }
public void SetVisible_DoesNotThrow() { Utils.MethodStart(); while (_controls.TryDequeue(out Control control)) { foreach (var value in EnumValues.GetBool()) { Assert.DoesNotThrow(() => InvokeControl.SetVisible(control, value)); Assert.DoesNotThrowAsync(async() => await Task.Run(() => InvokeControl.SetVisible(control, value))); } } Utils.MethodComplete(); }
private static void DoInvoke(Control control, Action action, Control context, bool synchronous) { if (action == null) { throw new ArgumentNullException("action"); } if (context == null) { throw new ArgumentNullException("context"); } if (control == null) { control = context; } var invokeControl = new InvokeControl(control, action, synchronous); if (synchronous) { // if we're already on the right thread, just execute if (control.IsHandleCreated && !control.InvokeRequired) { SafeInvoke(invokeControl); if (invokeControl.Exception != null) { invokeControl.Exception.PreserveExceptionStackTrace(); throw invokeControl.Exception; } return; } } if (synchronous) { context.Invoke((WaitCallback)SafeInvoke, invokeControl); if (invokeControl.Exception != null) { invokeControl.Exception.PreserveExceptionStackTrace(); throw invokeControl.Exception; } } else { context.BeginInvoke((WaitCallback)SafeInvoke, invokeControl); } }
//此方法支持在自动判断是否在非创建线程中被调用 private void UpdateUi(string message, DateTime messageAt) { string value = string.Format("{0}{1}", message, AutoAppendLine ? Environment.NewLine : string.Empty); if (WithDatePrefix) { value = messageAt.ToString("yyyy-MM-dd HH:mm:ss:fff") + " => " + value; } if (InvokeControl.InvokeRequired) { InvokeControl.BeginInvoke(UpdateUiMessage, value); //InvokeControl.Invoke(UpdateUiMessage, value); } else { UpdateUiMessage(value); } }
//此方法支持在自动判断是否在非创建线程中被调用 private void UpdateUi(string message, DateTime messageAt) { string value = string.Format("{0}{1}", message, AutoAppendLine ? Environment.NewLine : string.Empty); if (WithDatePrefix) { value = messageAt.ToString("yyyy-MM-dd HH:mm:ss:fff") + " => " + value; } if (InvokeControl.InvokeRequired) { //https://www.cnblogs.com/Rustle/articles/11301.html //https://www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html //InvokeControl.Invoke(UpdateUiMessage, value); InvokeControl.BeginInvoke(UpdateUiMessage, value); } else { UpdateUiMessage(value); } }
//此方法支持在自动判断是否在非创建线程中被调用 private void UpdateUi(AsyncFormMessageEvent messageEvent) { if (!AsyncFormEventBus.ShouldRaise()) { return; } string value = string.Format("{0}{1}", messageEvent.Message, AutoAppendLine ? Environment.NewLine : string.Empty); if (WithPrefix) { value = messageEvent.DateTimeEventOccurred.ToString("yyyy-MM-dd HH:mm:ss:fff") + " => " + value; } if (InvokeControl.InvokeRequired) { InvokeControl.Invoke(UpdateMessage, value); } else { UpdateMessage(value); } }
public virtual void Dispose() { lock (this) { if (!IsDisposed) { // Dispose managed resources DisposeManagedResources(); // Dispose unmanaged resources DisposeUnmanagedResources(); // Log InvokeControl.SetText(_richTextBox, @"Task memory is disposed."); // Dispose flag IsDisposed = true; } // Disable the garbage collector from calling the destructor GC.SuppressFinalize(this); } }
public InvokeProvider(InvokeControl invokable) : base(invokable.Control, InvokePattern.Pattern) { _invokable = invokable.Invoke; }
private void FormMain_Load(object sender, System.EventArgs e) { comboBoxProgressBarStyle.SelectedIndex = 0; InvokeControl.SetText(labelValue, $"{progressBar.Value} %"); }
public Task Execute() { return(Task.Run(() => { CheckIfDisposed(); try { var limitBytes = _objectMemorySize.Limit.Bytes; var limitMegaBytes = _objectMemorySize.Limit.GetMegaBytes(); while (IsExecute) { _objectMemorySize.Physical.Bytes = (ulong)Process.GetCurrentProcess().WorkingSet64; _objectMemorySize.Virtual.Bytes = (ulong)Process.GetCurrentProcess().PrivateMemorySize64; IsLimitOverload = !(_objectMemorySize.Physical.Bytes < limitBytes || limitBytes <= 0); if (_richTextBox != null) { InvokeControl.SetText(_richTextBox, @"Task memory ID: " + Task.CurrentId + Environment.NewLine + (limitMegaBytes <= 0 ? @"Limit memory: " + @"0 MB - is not set" : @"Limit memory: " + $"{limitMegaBytes:N0}" + @" MB - is set") + Environment.NewLine + @"Physical memory: " + $"{_objectMemorySize.Physical.GetMegaBytes():N0}" + @" MB" + Environment.NewLine + @"Virtual memory: " + $"{_objectMemorySize.Virtual.GetMegaBytes():N0}" + @" MB" + Environment.NewLine + (SimulateAction ? @"Simulate mode is enabled." : @"Simulate mode is disabled.")); } Thread.Sleep(SleepMiliSeconds); if (IsLimitOverload && !SimulateAction) { Dispose(); switch (_enumMemoryLimitAction) { case EnumMemoryLimitAction.Restart: MessageBox.Show(@"Memory limit is exceeded!" + Environment.NewLine + @"Application will be restart.", Application.ProductName); Application.Restart(); break; default: MessageBox.Show(@"Memory limit is exceeded!" + Environment.NewLine + @"Application will be exit.", Application.ProductName); Application.Exit(); break; } } } InvokeControl.SetText(_richTextBox, @"Task memory is finished."); } catch (Exception exception) { InvokeControl.SetText(_richTextBox, @"Error execute Task memory! " + Environment.NewLine + exception.Message); } finally { IsExecute = false; } })); }