KennanChan / Revit.Async

Use task-based asynchronous pattern (TAP) to run Revit API code from any execution context.
MIT License
223 stars 51 forks source link

Task dalay #27

Closed grizoood closed 6 months ago

grizoood commented 6 months ago

Hello

I'm trying to add a 1sec delay between each instance insertion but I'm having a problem, my transaction is rollback.

public ICommand InsertCommand
        {
            get
            {
                return _insertCommand ?? (_insertCommand = new RelayCommand(
                   async (obj) =>
                   {
                       await RevitTask.RunAsync(
                            async app =>
                            {
                                try
                                {
                                    var document = app.ActiveUIDocument.Document;

                                    using (Transaction transaction = new Transaction(document))
                                    {
                                        transaction.Start("Insert");

                                        int count = 20;

                                        for (int i = 0; i < count; i++)
                                        {
                                            string family_2d = "Family";
                                            string symbol_2d = "Symbol";

                                            var symbol = document.FindSymbol(family_2d, symbol_2d);

                                            FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);

                                            await Task.Delay(1000);

                                            Informations = $"Insert {i + 1}/{count}";
                                        }

                                        transaction.Commit();
                                    }

                                    Informations = "Finished";
                                }
                                catch (Exception ex)
                                {

                                }
                            });
                   },
                    (obj) =>
                    {
                        return obj as Window != null;
                    }));
            }
        }
KennanChan commented 6 months ago

try to dispose the transaction manually rather than relying on the "using" statement.

KennanChan commented 6 months ago

Or you can create a transaction in every loop

grizoood commented 6 months ago

On the second pass through the loop I get this exception : image

 await RevitTask.RunAsync(
                            async app =>
                            {
                                try
                                {
                                    var document = app.ActiveUIDocument.Document;

                                    int count = 20;

                                    for (int i = 0; i < count; i++)
                                    {
                                        string family_2d = "Family";
                                        string symbol_2d = "Symbol";

                                        using (Transaction transaction = new Transaction(document))
                                        {
                                            transaction.Start("Insert");

                                            var symbol = document.FindSymbol(family_2d, symbol_2d);

                                            FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);

                                            await Task.Delay(1000);

                                            transaction.Commit();
                                        }

                                        Informations = $"Insert {i + 1}/{count}";
                                    }

                                    Informations = "Finished";
                                }
                                catch (Exception ex)
                                { }
                            });
KennanChan commented 6 months ago

How about delaying after the transaction has been committed?

grizoood commented 6 months ago

I tried too but it causes the same exception, for your information it happens on this line: " transaction.Start("Insert");"

KennanChan commented 6 months ago

What is the exception?

grizoood commented 6 months ago

The 1st transaction is commited but the next one does not work.

The exception is: "Starting a transaction from an external application running outside of API context is not allowed." and this happens at the line: " transaction.Start("Insert");"

await RevitTask.RunAsync(
                            async app =>
                            {
                                try
                                {
                                    var document = app.ActiveUIDocument.Document;

                                    int count = 20;

                                    for (int i = 0; i < count; i++)
                                    {
                                        string family_2d = "Family";
                                        string symbol_2d = "Symbol";

                                        using (Transaction transaction = new Transaction(document))
                                        {
                                            transaction.Start("Insert");

                                            var symbol = document.FindSymbol(family_2d, symbol_2d);

                                            FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);

                                            transaction.Commit();
                                        }

                                        await Task.Delay(1000);

                                        Informations = $"Insert {i + 1}/{count}";
                                    }

                                    Informations = "Finished";
                                }
                                catch (Exception ex)
                                { }
                            });
KennanChan commented 6 months ago

Exactly. The Task.Delay() results in the rest code to be running in a worker thread. You can try to use RevitTask.RunAsync in the loop to execute Revit API

grizoood commented 6 months ago

It works by doing this:

try
{
    int count = 20;

    for (int i = 0; i < count; i++)
    {
        string family_2d = "Family";
        string symbol_2d = "Symbol";

        await RevitTask.RunAsync(
            app =>
            {
                Informations = $"Insert {i + 1}/{count}";

                var document = app.ActiveUIDocument.Document;

                using (Transaction transaction = new Transaction(document))
                {
                    transaction.Start("Insert");

                    var symbol = document.FindSymbol(family_2d, symbol_2d);

                    FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);

                    transaction.Commit();
                }
            });

        await Task.Delay(1000);
    }

    Informations = "Finished";
}
catch (Exception ex)
{
}

I was thinking of creating a single transaction for the loop how can I do that? Will creating multiple transactions impact processing time?

Another question: If I want to update the "Information" property of my viewmodel, in RevitTask.RunAsync, I have to use the Dispatcher of my interface and do this:

public void UpdateInformations(string message)
{
    _dispatcher.Invoke(() =>
  {
      Informations = message;
  }, DispatcherPriority.Background);
}
KennanChan commented 6 months ago

As far as I know, you can update viewmodel properties without caring about which thread your code is currently running. The viewmodel fires the "PropertyChanged" event to notify the UI to update in proper time.

KennanChan commented 6 months ago
try
{
  RevitTask.RunAsync(async (app) =>
  {
    int count = 20;

    var document = app.ActiveUIDocument.Document;
    Transaction transaction = new Transaction(document);
    transaction.Start("Insert");

    var symbol = document.FindSymbol(family_2d, symbol_2d);

    for (int i = 0; i < count; i++)
    {
      string family_2d = "Family";
      string symbol_2d = "Symbol";

      await RevitTask.RunAsync(
          app =>
          {
            Informations = $"Insert {i + 1}/{count}";
            FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
          });

      await Task.Delay(1000);
    }

    transaction.Commit();
    transaction.Dispose();

    Informations = "Finished";
  });
}
catch (Exception ex)
{
}

How about this?

grizoood commented 6 months ago

I get this exception: "System.Runtime.InteropServices.SEHException : 'Un composant externe a levé une exception.'"

KennanChan commented 6 months ago

How did you implement your viewmodel? Especially in the "Information" property

grizoood commented 6 months ago

With your example I get this exception at this line:

FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
KennanChan commented 6 months ago
try
{
  RevitTask.RunAsync(async (app) =>
  {
    int count = 20;

    var document = app.ActiveUIDocument.Document;
    Transaction transaction = new Transaction(document);
    transaction.Start("Insert");

    for (int i = 0; i < count; i++)
    {
      string family_2d = "Family";
      string symbol_2d = "Symbol";

      await RevitTask.RunAsync(
          app =>
          {
            Informations = $"Insert {i + 1}/{count}";
            var document = app.ActiveUIDocument.Document;
            var symbol = document.FindSymbol(family_2d, symbol_2d);
            FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
          });

      await Task.Delay(1000);
    }

    transaction.Commit();
    transaction.Dispose();

    Informations = "Finished";
  });
}
catch (Exception ex)
{
}

Sorry, I don't have access to Revit at the moment. Please try entering the code again.

grizoood commented 6 months ago

It doesn't change anything, same exception

try
{
    RevitTask.RunAsync(async (app) =>
    {
        int count = 20;

        var document = app.ActiveUIDocument.Document;
        Transaction transaction = new Transaction(document);
        transaction.Start("Insert");

        for (int i = 0; i < count; i++)
        {
            string family_2d = "Family";
            string symbol_2d = "Symbol";

            await RevitTask.RunAsync(
                app2 =>
                {
                    Informations = $"Insert {i + 1}/{count}";
                    var document2 = app2.ActiveUIDocument.Document;
                    var symbol = document2.FindSymbol(family_2d, symbol_2d);
                    FamilyInstance instance = document2.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);
                });

            await Task.Delay(1000);
        }

        transaction.Commit();
        transaction.Dispose();

        Informations = "Finished";
    });
}
catch (Exception ex)
{
}
KennanChan commented 6 months ago

I haven't used Revit in years. It seems that the beginning and committing of a transaction cannot be separated in different Revit contexts.

grizoood commented 6 months ago

After this is only a part of code in fact I encounter another problem, when I execute my command my Information property is not updated in my interface, I only see the last message of the loop

KennanChan commented 6 months ago
try
{
  int count = 20;

  for (int i = 0; i < count; i++)
  {
    string family_2d = "Family";
    string symbol_2d = "Symbol";

    // update information right before you enter an external event handler
    Informations = $"Insert {i + 1}/{count}";

    await RevitTask.RunAsync(
        app =>
        {

          var document = app.ActiveUIDocument.Document;

          using (Transaction transaction = new Transaction(document))
          {
            transaction.Start("Insert");

            var symbol = document.FindSymbol(family_2d, symbol_2d);

            FamilyInstance instance = document.Create.NewFamilyInstance(new XYZ(0, 0, 0), symbol, StructuralType.NonStructural);

            transaction.Commit();
          }
        });

    await Task.Delay(1000);
  }

  Informations = "Finished";
}
catch (Exception ex)
{
}

Slight changes to the working code.

KennanChan commented 6 months ago

Is the issue still ongoing?

grizoood commented 6 months ago

Yes but I close the issue