rajyraman / mscrmexporttoexcel

XrmToolBox tool to export all records from a view
MIT License
8 stars 15 forks source link

Error Cross-thread operation not valid - when running in Visual Studio #1

Open drosenba opened 8 years ago

drosenba commented 8 years ago

I get this error when running in Visual Studio: An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code Additional information: Cross-thread operation not valid: Control 'lvViews' accessed from a thread other than the thread it was created on.

It happens here:

private void BwFillViewsDoWork(object sender, DoWorkEventArgs e)

... lvViews.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); }

drosenba commented 8 years ago

I moved this line from BwFillViewsDoWork lvViews.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); to the end of the BwFillViewsRunWorkerCompleted method

and made this change private void ExportCurrentViewToExcel(string fileName) { WorkAsync(new WorkAsyncInfo("Retrieving records..", (w, e) => { // ---- if (lvViews.SelectedItems.Count == 0 || fetchXml == string.Empty) if (fetchXml == string.Empty)

and it now works OK.