Label从另一个线程更新a的最简单方法是什么?
我有一Form对thread1,并从我开始另一个线程(thread2).虽然thread2在处理一些文件,我想更新Label在Form用的当前状态thread2的工作.
我怎样才能做到这一点?
如果我有这样的方法:
public void Show()
{
Form1 f = new Form1();
f.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)
即使它超出范围,我仍然需要在表单上调用dispose,这将有资格进行垃圾回收.
从一些测试中,多次调用此Show()..在某些时候,似乎GC收集它,因为我可以看到内存尖峰然后它在某个时间点下降.
从MSDN,它似乎说你必须在不再需要表单时调用dispose.
所以在我的应用程序中,我倾向于动态创建表单的新实例,然后使用Form.Show()来显示它们(非模态).
private void test_click(object sender, EventArgs e)
{
var form = new myForm();
form.Show();
}
Run Code Online (Sandbox Code Playgroud)
但是,Code Cracker告诉我应该处理这些表格.所以,我用"使用"语句包装它们,但随后它们在打开后立即关闭.
using (var form = new myForm())
{
form.Show();
}
Run Code Online (Sandbox Code Playgroud)
我不想使用Form.ShowDialog(),因为在少数情况下我打开只显示报告的新窗口; 我不需要它们是模态的.
我有一个带有backgroundWorker的WinForm:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SeoTools.Utils;
namespace SeoTools.UI
{
public partial class UIProgress : Form
{
public UIProgress(DoWorkEventHandler doWorkEventHandler, RunWorkerCompletedEventHandler runWorkerCompletedEventHandler)
{
InitializeComponent();
this.backgroundWorker.WorkerReportsProgress = true;
this.backgroundWorker.WorkerSupportsCancellation = true;
this.backgroundWorker.DoWork += doWorkEventHandler;
this.backgroundWorker.RunWorkerCompleted += runWorkerCompletedEventHandler;
}
public void Start()
{
var foo = SynchronizationContext.Current;
backgroundWorker.RunWorkerAsync();
}
private void btnStop_Click(object sender, EventArgs e)
{
btnStop.Enabled = false;
btnStop.Text = "Stopping...";
backgroundWorker.CancelAsync();
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Windows 窗体应用程序中使用 Simple Injector。不幸的是,https://simpleinjector.readthedocs.org/en/latest/windowsformsintegration.html上的文档不正确或已过时。
当您实际运行该示例时,会导致以下错误:
The configuration is invalid. The following diagnostic warnings were reported:
-[Disposable Transient Component] MainView is registered as transient, but implements IDisposable.
此外,我正在构建的应用程序是一个 Winforms MVP(被动视图)项目。我可以将 MainView 的范围更改为单例并且它可以工作。但是对于我的一生,由于这个范围界定问题,我无法弄清楚如何打开其他窗口。有没有人在具有多个窗口的真实 MVP winforms 应用程序中成功使用 SimpleInjector?我很想知道 Presenters、Forms/Views 和 Main 入口点是如何配置的,以及它们的 Lifestyle 范围是什么。
仅供参考,我曾尝试使用 LifetimeScoping 和 ExecutionContextScopeing 扩展,但绝对没有任何效果。也许这只是一个 PEBKAC 问题。
谢谢,埃里克
我正在使用c#开发winforms应用程序.我有一个mdi容器,左边有一个菜单,按下按钮,然后可以看到相应的表格.如果我点击打开Form1的按钮3次,则打开表单的6个实例.因此我认为我必须编写一个处理任何其他Form1实例的方法.使用以下方法我循环通过MDI childer但我想要一些帮助如何关闭除新的实例以外的所有其他实例.
public void DisposeAllButThis(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm == form)
{
frm.Dispose();
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)