2

I am having trouble writing to an excel file from ArcGIS Pro Sdk add-in that I created, I have a basic understanding of multithread, I am using task but the main thread does not wait for the excel to finish before trying to move ahead.

here is the helper I created.

public class ReportHelper
{
    public ReportHelper()
    {

    }

    public async Task WriteReport(string path)
    {
        await Task.Run(() =>
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Visible = false;
            excel.DisplayAlerts = false;
            Workbook wrkbook = excel.Workbooks.Add(Type.Missing);
            string filename = Path.GetFileName(path).Replace(".gdb", "_importreport.xlsx");
            string sheetname = new string(filename.Take(11).ToArray());
            string directoryname = Path.GetDirectoryName(path);
            string savename = Path.Combine(filename, directoryname);
            Worksheet wrksheet = (Worksheet)wrkbook.Sheets.Add(After: wrkbook.Sheets[wrkbook.Sheets.Count]);
            //MessageBox.Show(sheetName);
            wrksheet.Name = sheetname;
            int x = 1;
            List<DataRecord> dRecords = Model.FeatureRecord;
            if (dRecords == null || dRecords.Count() < 1) return;
            foreach(DataRecord drecord in dRecords)
            {
                wrksheet.Cells[x, 1] = drecord.FeatureType;
                wrksheet.Cells[x, 2] = drecord.FeatureCount;
                wrksheet.Cells[x, 3] = drecord.ImportCount;
            }
            wrkbook.SaveAs(savename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            wrkbook.Close();
            excel.Quit();
        });

    }
}

I have found async streamwriter methods but I cannot find any awaitable methods for excel.

I am just trying to call these from button clock, everything works fine until I try to write the excel.

private  void button1_Click(object sender, EventArgs e)
{
    panel1.Enabled = false;

    Loader loader = new Loader();
    ReportHelper rpthelper = new ReportHelper();

    QueuedTask.Run(async () =>
    {
        await QueuedTask.Run( async () =>
        {
            foreach (string path in Model.DailyGdbs)
            {

                await loader.ReturnMasterPointList(Model.MasterGdb);

                await loader.Load(path);

                await rpthelper.WriteReport(path);
            }
        });
        await AsyncMessageBox();

    });
    //await AsyncMessageBox();
    //MessageBox.Show("The data has been loaded");
}
  • Shouldn't the first QueuedTask.Run() be awaited? And BTW, why do you have two nested QueuedTasks, I don't really think that should be necessary. And if you're calling WriteReport() only from QueuedTasks, it doesn't need to be async either. Finally, I'd move the call to the MessageBox outside QueuedTask.Run(), since it should be on the UI thread anyway. – Berend Jan 10 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.