Make it
async
all the way down:
private async void button2_Click(object sender, EventArgs e)
{
...
else
{
await Callmethod();
}
}
private async Task Callmethod()
{
Hide();
List<string> filesToZip = searchpattern
.AsParallel()
.SelectMany(pattern => Directory.EnumerateFiles(sourcepath, pattern, SearchOption.AllDirectories))
.ToList()
;
int count = await ReadFile.FILE_C(filesToZip);
if (count == filesToZip.Count)
{
Directory.Delete(Unzip.destinationpath, true);
bool directoryExists = Directory.Exists(Unzip.destinationpath);
Close();
}
}
public class ReadFile
{
protected internal static async Task<int> FILE_C(IEnumerable<string> zipfilepath)
{
int count = 0;
if (!Directory.Exists(Unzip.destinationpath))
{
Directory.CreateDirectory(Unzip.destinationpath);
}
foreach (string filepath in zipfilepath)
{
await Task.Run(()=> Unzip.data(filepath)).ConfigureAwait(false);
count += 1;
}
return count;
}
}
NB: If you
really need to re-throw an exception, use
throw;
, not
throw ex;
- the latter destroys the stack trace.
However, in this instance, since all you're doing is re-throwing the exception, there's no point in catching it in the first place.