Samples | System instance.SuppressUI = false; instance.MainWindow.Visible = true; return instance; }); Console.WriteLine("DTE instance created. Creating solution..."); // Step 2 - Create a new solution and add a TwinCAT project from a template. await AsyncFactory.StartNew(() => { string solutionDir = @"C:\tmp\Test\AsyncSolution"; string solutionName = "AsyncSolution"; Solution sol = dte.Solution; sol.Create(solutionDir, solutionName); string tcTemplate = @"C:
\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\PrjTemplate\TwinCAT Project.tsproj"; sol.AddFromTemplate(tcTemplate, Path.Combine(solutionDir, "TwinCATProject1"), "TwinCATProject1"); sol.SaveAs(Path.Combine(solutionDir, $"{solutionName}.sln")); Console.WriteLine("Solution saved."); }); // Step 3 - Access the TwinCAT System Manager and query the PLC tree item. string plcPath = await AsyncFactory.StartNew(() => { ITcSysManager sysManager = dte.Solution.Projects.Item(1).Object as ITcSysManager; ITcSmTreeItem plc = sysManager.LookupTreeItem("TIPC"); return plc.PathName; }); Console.WriteLine($"PLC path:
{plcPath}"); // Step 4 - Run a long-running COM operation while doing other work concurrently. var comTask = AsyncFactory.StartNew(() => { // Simulate a time-consuming operation (e.g. code generation) System.Threading.Thread.Sleep(2000); return dte.Solution.FullName; }); Console.WriteLine("Performing other work while COM operation is in progress..."); await Task.Delay(500); Console.WriteLine("Other work completed."); string fullName = await comTask; Console.WriteLine($"Solution: {fullName}"); // Step 5 - Clean up: close the DTE instance and revoke the message filter.
await AsyncFactory.StartNew(() => { dte.Quit(); MessageFilter.Revoke(); }); Console.WriteLine("Done."); } Background TwinCAT COM objects such as ITcSysManager , ITcSmTreeItem , etc., are STA-registered. All calls must come from the same STA thread in which the object was created. BackgroundWorker and Task.Run use thread pool threads, which are MTA by default. The Message Filter ( IMessageFilter ) is registered on the STA thread and handles temporary “Please wait” responses from the COM server. For asynchronous scenarios, an explicit STA thread is more stable than a message filter. In PowerShell scripts, -Sta resolves the same issue when the PS process starts. TE1000 208 Version: 1.7.0