Creating AutoCAD objects in an Async process extremely slow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Probably due to a lack of fully understanding an Async process, and normally I can figure out myself by reading and testing. But now I I'm stuck in something I can't find a solution for.
Short story: I have an WPF application with a wizard and that generates geometry inside AutoCAD. If I use a non-async method, the speed is impressive. 10,000 circles in 0,3 seconds.
But such process blocks the current threat, thus the UI and there is no breakout option.
So, using an async method, the UI is not blocked, the progress bar is filling nicely and the user can press escape to stop processing. But drawing 10,000 circles suddenly takes much longer, almost 11 seconds, more than 30 times longer.
The difference in calling the code is this:
private async Task<bool> NextStepAsync()
{
bool returnValue = true;
try
{
await Task.Run(() => returnValue = DrawGeometry());
}
catch (System.Exception)
{
returnValue = false;
}
return returnValue;
}
private bool NextStep()
{
bool returnValue = true;
try
{
returnValue = DrawGeometry();
}
catch (System.Exception)
{
returnValue = false;
}
return returnValue;
}
And this is the function to create geometry:
private bool DrawGeometry()
{
bool returnValue = true;
ProgressPercent = 0;
ElapsedTime = string.Empty;
DateTime startTime = DateTime.Now;
int numberOfGeometry = 10000;
try
{
for (int i = 0; i < numberOfGeometry; i++)
{
ProgressPercent = (100.0 / (double)numberOfGeometry) * i;
acDbServ.Database db = acAppServ.Application.DocumentManager.MdiActiveDocument.Database;
using (acDbServ.Transaction tr = db.TransactionManager.StartTransaction())
{
using (acDbServ.BlockTableRecord space = (acDbServ.BlockTableRecord)tr.GetObject(db.CurrentSpaceId, acDbServ.OpenMode.ForWrite))
{
using (acDbServ.Circle circle = new acDbServ.Circle())
{
circle.SetDatabaseDefaults();
circle.Center = new acGeom.Point3d(0.0, 0.0, 0.0);
circle.Radius = i + 1;
circle.Layer = "0";
space.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
}
}
tr.Commit();
}
}
}
catch (System.Exception)
{
returnValue = false;
}
ElapsedTime = $"{numberOfGeometry} Circles in {(DateTime.Now - startTime).TotalMilliseconds:0} ms";
return returnValue;
}
The buttons code:
private void BtnNoAsync_Click(object sender, RoutedEventArgs e)
{
NextStep();
}
private async void BtnAsync_Click(object sender, RoutedEventArgs e)
{
await NextStepAsync();
}
I have the feeling it is something in the Task.Run method but I can't find anything about such an issue. Maybe it is a wrong use of a Task or Async, but I have the feeling it does not have to be such a big difference in time processing.
Included is a test application for who wants to try things out, or maybe someone can point me in the right direction where I can find a solution?