当前位置: 首页 > 编程语言 > C#

BackgroundWorker在VSTO中不起作用Share

时间:2023-04-11 00:59:55 C#

BackgroundWorker在VSTO中不起作用我有一个后台工作者。在我打电话给工作人员之前,我禁用了一个按钮并使gif可见。然后我调用runWorkerasync方法,它运行良好,直到完成。在“RunWorkerCompleted()”上,出现跨线程错误。知道为什么吗?.ToString()),textBoxFolderLoc.Text,Properties.Settings.Default.TemplatePath,Properties.Settings.Default.ConnStr);this.buttonRun.Enabled=false;this.pictureBox1.Visible=true;BackgroundWorkerbw=newBackgroundWorker();bw.DoWork+=newDoWorkEventHandler(bw_DoWork);bw.RunWorkerCompleted+=newRunWorkerCompletedEventHandler(bw_RunWorkerCompleted);bw.RunWorkerAsync();//backgroundWorker1.RunWorkerAsync();}catch(Exceptionex){MessageBox.Show("Unabletoprocess.nError:"+ex.Message,Properties.Settings.Default.AppName);}}}voidbw_RunWorkerCompleted(objectsender,RunWorkerCompletedEventArgse){buttonRun.Enabled=true;pictureBox1.Visible=false;}voidbw_DoWork(对象发件人,DoWorkEventArgse){u.Execute();这似乎是VSTO和BackgroundWorker的问题解决方案在这里。基本上你需要调用System.Threading.SynchronizationContext.SetSynchronizationContext(newWindowsFormsSynchronizationContext());在调用RunWorkerAsync之前。好的结果。避免每次在AddIn的主类中有静态成员时都实例化对象并重用它。这样你只需要实例化一次。关于VSTO在与控件相同的线程上运行后台工作人员的事情。不确定。得查InvokeRequired以上是C#学习教程:BackgroundWorkerdoesnotworkinVSTO分享的所有内容,如果对大家有用还需要详细了解C#学习教程,希望大家多多关注—私信voidbuttonRun_Click(objectsender,EventArgse){if(comboBoxFiscalYear.SelectedIndex!=-1&&!string.IsNullOrEmpty(textBoxFolderLoc.Text)){try{u=newUpdateDispositionReports(Convert.ToInt32(comboBoxFiscalYear.SelectedItem.ToString()),textBoxFolderLoct.Text,正确的.Settings.Default.TemplatePath,Properties.Settings.Default.ConnStr);this.buttonRun.Enabled=false;this.pictureBox1.Visible=true;BackgroundWorkerbw=newBackgroundWorker();bw.DoWork+=newDoWorkEventHandler(bw_DoWork);bw.RunWorkerCompleted+=newRunWorkerCompletedEventHandler(bw_RunWorkerCompleted);bw.RunWorkerAsync();//backgroundWorker1.RunWorkerAsync();}catch(Exceptionex){MessageBox.Show("Unabletoprocess.nError:"+ex.Message,Properties.Settings.Default.AppName);}}}委托voidReenableRunCallback();私人无效重新启用eRun(){如果(this.buttonRun.InvokeRequired){ReenableRunCallbackr=newReenableRunCallback(ReenableRun);this.buttonRun.Invoke(r,null);}否则this.buttonRun.Enabled=true;}privatevoidHideProgress(){if(this.pictureBox1.InvokeRequired){ReenableRunCallbackr=newReenableRunCallback(HideProgress);this.pictureBox1.Invoke(r,null);}elsethis.pictureBox1.Visible=false;}voidbw_RunWorkerCompleted(objectsender,RunWorkerCompletedEventArgse){ReenableRun();隐藏进度();}voidbw_DoWork(objectsender,DoWorkEventArgse){u.Execute();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: