0

I am accessing XLSX Files and summing up the [FileSize] Values of a Column, it contains more than 100k rows.

I wanted to use OLEDB in combination with multitasks. After that I want the tasks to return an integer/float value so I can calculate the percentage of each XLSX file compared to the total file size. So far I have this code:

foreach (string subdir in alldir)
{
    Task<int> iSize = Task<int>.Factory.StartNew(() =>
    {
        int result = 0;
        using (OleDbConnection olecnn = new OleDbConnection(GetConnectionString(subdir)))
        {
            try
            {
                olecnn.Open();
                using (OleDbCommand sumcmd = new OleDbCommand("SELECT SUM([File Size]) FROM [FileInfos$]"))
                {
                    sumcmd.Connection = olecnn;
                    result = (int)sumcmd.ExecuteScalar();
                    //BarChart.Series["Memory Usage"].Points.AddXY(Path.GetFileName(subdir), result);
                    return result;
                }
                olecnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString()); return result;
            }

        }
    }).ContinueWith(cnt => cnt.Dispose());
}

There seems to be an syntax error

Cannot implicitly convert type 'System.Threading.Tasks.Tasks' to 'System.Threading.Tasks.Task<int>'. An explicit conversion exists (are you missing a cast)?

I read the docs but can't figure out how to get the result as integer/float, so I can proceed with this value.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
V.Hunon
  • 320
  • 3
  • 12

1 Answers1

0

You start a task that returns an int result, and then follow it with another task in ContinueWith that can only return void - that's where the syntax error comes from.

Also, do you really need to call Dispose() inside ContinueWith? Anything declared as a part of using block will call Dispose on itself even if an exception is thrown (see this blog post for explanation and code samples).

Also-2, answers to this post have some helpful info on why you usually shouldn't use Factory.StartNew (which is semi-obsolete): What is the difference between Task.Run() and Task.Factory.StartNew()

Hope that helps!

Darth Veyda
  • 828
  • 2
  • 12
  • 23