Windows Application freezes when running FemDesignConnection() [SOLVED]

Hey Marco (and others)

We talked in December about how to avoid my WFA to freeze when running the FemDesignConnection(). Unfortunately, time has past, and I can’t really remember anymore :stuck_out_tongue:.

The WFA is constructed as a Window:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent(); 
    }
}

which later runs the FEM-design API, ending with:

using (var femDesign = new FemDesignConnection(keepOpen: true))
{
    femDesign.Open(fEMdesignPath, disconnect: true);
}

But the UI freeze after performing the .Open operation, and never returns to normal. I remember the fix as quite easy, but maybe you can help me and others again?

Hi @AmalieRask :slight_smile:

Have you had a look at the following example?

The example below would allow you to keep the connection open

One other way, it is to simply wrap your code within a Task.Run as shown below

var t = Task.Run((Action)(() =>
{
    var connection = new FemDesign.FemDesignConnection(minimized: true);
    connection.Open(filePath);
    model = connection.GetModel();
    connection.Dispose();
}));

t.ConfigureAwait(false);

try
{
    t.Wait();
}
catch (Exception ex)
{
    throw ex.InnerException;
}

Hey Marco, yes I did, the struggle was that my UI is build in another way, but I have fixed the issue know, remembering our talk about Threading. Using this I could call my methods as an ‘async’ from the UI, and later use the ‘await Task.Run method’:

System.Threading.Tasks;

public async void LoadsToFEM(....)
{

   //... a lot of code

   // OPEN MODEL
   var folderPath = Path.GetDirectoryName(fEMdesignPath);

   // Start a new thread to execute the time-consuming operation asynchronously
   await Task.Run(() =>
   {
       using (var femDesign = new FemDesignConnection(keepOpen: true, outputDir: folderPath))
       {
           femDesign.Open(fEMdesignPath);
       }
   });
}

However, the FemDesignConnection() needed to have an outputDir specified, else the program created an error :slight_smile:

I am glad you have managed to “refactor” your code. It will be easier to work with the new Object class.
Looking forward to get feedback from you :slight_smile:

What error do you get?

outputDir = null should work if you have admin privilege to write in the CurrentDirectory()

It was kind of a wierd error, that did not occur running my code from debug mode. But after creating an .msi to be installed on my colleges computers, the error in the picture occured, when running the program:

FEMdesign

But it worked fine after giving it an output path.

To me it looks like the FEMDesignConnection.Open tries to occupy some space from where the program is located. But as the program is located at my ‘Program Files’ folder, that needs admin access to be written to, it could not - so i simply just gave it the path from where the .struxml was placed :slight_smile:

thanks for the feedback :slight_smile:

Unfortunately, the API rely on sending information to FEM-Design using text file and we need to get access to your disk and you need admin privilege in some of your laptop folders.

Same error should not happen if they install your tool in a different folder such as “Document”

True!

But as I distribute the software through the company software channels, it will end up in the ‘program files’ folder :stuck_out_tongue:

But luckily it was easy enough to fix - and I don’t think it impacts the tool in any negative way :slight_smile:

I am getting curious :slight_smile:

Can the user install your .msi using “RunAsAdministrator” ?
I am try to figured out if it is something that I need to fix or you need to fix :stuck_out_tongue:

Hey Marco,

Jep, they can do that :wink:
But in Niras, our software is also destributed through something called CapaInstaler, where I simply just send my .msi to the software guy and then he enrolls it automatically to the users that already got the program. And others who doesn’t have it yet, can anytime they want visit Capa and install it from there :slight_smile:

It should work though with the little twist giving it another path to occupy space in :slight_smile:

Hope I can just reply here :stuck_out_tongue:

1 Like