Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task dalay #27

Closed
grizoood opened this issue Feb 23, 2024 · 21 comments
Closed

Task dalay #27

grizoood opened this issue Feb 23, 2024 · 21 comments

Comments

@grizoood
Copy link

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
Copy link
Owner

KennanChan commented Feb 23, 2024

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

@KennanChan
Copy link
Owner

Or you can create a transaction in every loop

@grizoood
Copy link
Author

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
Copy link
Owner

How about delaying after the transaction has been committed?

@grizoood
Copy link
Author

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

@KennanChan
Copy link
Owner

What is the exception?

@grizoood
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

KennanChan commented Feb 23, 2024

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
Copy link
Author

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

@KennanChan
Copy link
Owner

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

@grizoood
Copy link
Author

With your example I get this exception at this line:

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

@KennanChan
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

Is the issue still ongoing?

@grizoood grizoood closed this as completed Mar 4, 2024
@grizoood
Copy link
Author

grizoood commented Mar 4, 2024

Yes but I close the issue

@grizoood grizoood reopened this Mar 4, 2024
@grizoood grizoood closed this as completed Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants