I am trying to do the export in a background worker.
Does anyone have this working? What are the tricks to get it to work.
I implemented it the simple way with a backgroundworker, DoWoker,,,and I get an exception associated with TSA...
Any clues would be appriciated.
I would also like to know answer for this question. Just like jack520 said, using BackgroundWorker or another thread while exporting freezes the UI. AFAIK using UI controls or their content in a thread does that.
After several attempts to tackle this I am came to a conclusion that if we want to use the Infragistics components and their methods you can do exporting at least partially on background. This can be done by extracting data from the datasource or datagrid to a Workbook and then using the Workbook.Save method on background thread. However, the save is a single method call so you wouldn't be getting any information about the progress to the UI.
Another alternative is to create a CSV file on background thread in the traditional way. This works just fine but if for example you want to export only the selected rows or your datagrid supports grouping... you have to support this in your code. However, if you have all those fancy features which allow you to export data by writing only a few lines of code, you probably want to use those which brings us back to the question - i there a better solution for exporting datagrid on background without UI freezing?
is there a solution to this?
Restating my problem a bit clearer:
I have an application that has as one option a xamdatagrid which I am trying to export to. When I export a very large file, I would like to do other things in the application while the file is being saved. The aforementioned code, allows me to access the xamDataGrid and do the export, but it does not achieve the goal of letting me do other things in the application (eg. accessing other functionality that is totally disconnected with this task) . I smiluate a large excel save with the following code, and try to do something else to no avail.
Dispatcher.BeginInvoke(
{
System.Threading.
Thread.Sleep(10000);
}),
);
So, yes, I am running the work in a new thread, yet I do not have access to the remaining code.
Any help would be much appriciated.
Jack
Thanks for the quick reply.
I tried this already, and yes, it does process the code in a new thread, but the previous thread waits for this new thread to be completed.
What I fundamentally need is two processes, one for the main app, and a second one for saving the data to excel. These processes need to be independent(that is why I am calling processes) so, while I am saving to the excel format, I can go back into the main process and do other things.
This example helps group the grid with the saving to ensure that this process is atomic, but it does not provide the parrallelism that I am looking for.
Any help down this path would be appriciated.
Hello,
This is because the background worker will try to execute this on a background thread and the exporting process will try to access the XamDataGrid, which is created on the UI thread. What you need to do is to use Dispatcher.BeginInvoke() when exporting :
BackgroundWorker worker = new BackgroundWorker ();
public MainWindow()
InitializeComponent();
worker.DoWork += new DoWorkEventHandler (worker_DoWork);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
DataPresenterExcelExporter exporter = new DataPresenterExcelExporter ();
new Action (() =>
exporter.Export(this .xamDataGrid1, "exported.xlsx" , WorkbookFormat .Excel2007);
}), null );
private void Button_Click(object sender, RoutedEventArgs e)
worker.RunWorkerAsync();