我是VSTO Excel外接程序和Winforms的新手。我已经创建了一个插件,它启动了一个winform页面,其中包含几个复选框、一个按钮和一个跟踪状态的标签。
winform一启动,我的excel就没有响应。我想使excel响应,即使与winform打开(并与按钮“完成”点击)。按钮将运行一个长时间运行的进程。我怎样才能做到这一点呢?有什么建议吗?
这是我的Ribbon类:
public partial class Ribbon1
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
private void button1_Click(object sender, RibbonControlEventArgs e)
Form1 fs = new Form1();
fs.ShowDialog();
}
Form类:
public partial class Form1 : Form
public Form1()
InitializeComponent();
private async void button1_Click(object sender, EventArgs e)
label1.Text = "Processing please wait";
await Task.Run(() => { longRunningProcess(); });
label1.Text = "File 1 Processed";
public void longRunningProcess()
Thread.Sleep(5000);
}
更新:使用.show可以工作,但我无法访问label1.Text并得到错误信息:
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.'