Author Topic: How to update AutoCAD GUI from other threads?  (Read 239 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
How to update AutoCAD GUI from other threads?
« on: January 21, 2024, 01:22:39 PM »
Windows 10 x64.
AutoCAD 2024.
.Net Framework 4.8.1
Visual Studio 2022

Custom plugin does some long work in the parallel thread. How to initialize updating the StatusBar custom Pane icon when that thread will be finished? In WPF or WinForms I am to update the GUI from other thead via Dispatcher. But how to get something like Dispatcher for AutoCAD?

Code - C#: [Select]
  1. using System.Threading.Tasks;
  2. using AcWin = Autodesk.AutoCAD.Windows;
  3. using acad = Autodesk.AutoCAD.ApplicationServices.Application;
  4. using System.IO;
  5. using System.Drawing;
  6. using System.Threading;
  7.  
  8. namespace Bushman.AutoCAD.Examples.Gui {
  9.     public static class StatusBarExamples {
  10.  
  11.         static readonly Icon updateIcon = new Icon(Path.Combine(Path.GetDirectoryName(typeof(StatusBarExamples).Assembly.Location), @"Icons\update_32.ico"));
  12.         static readonly Icon downloadIcon = new Icon(Path.Combine(Path.GetDirectoryName(typeof(StatusBarExamples).Assembly.Location), @"Icons\download_32.ico"));
  13.         static bool isUpdateIcon = true;
  14.  
  15.         public static void AddCustomPane() {
  16.  
  17.             var statusBar = acad.StatusBar;
  18.             var doc = acad.DocumentManager.CurrentDocument;
  19.  
  20.             if (doc == null) return;
  21.  
  22.             var ed = doc.Editor;
  23.  
  24.             Icon getIcon() {
  25.                 return isUpdateIcon ? updateIcon : downloadIcon;
  26.             };
  27.  
  28.             string getTooltip() {
  29.                 return isUpdateIcon ? "Updating is avaliable" : "Updating is downloaded";
  30.             };
  31.  
  32.             var customPane = new AcWin.Pane {
  33.                 ToolTipText = getTooltip(),
  34.                 Style = AcWin.PaneStyles.Normal | AcWin.PaneStyles.Command,
  35.                 Icon = getIcon(),
  36.             };
  37.  
  38.             // It works fine
  39.             /* customPane.MouseDown += (o, e) => {
  40.                 var pane = o as AcWin.Pane;
  41.                 pane.Icon = getIcon();
  42.                 pane.ToolTipText = getTooltip();
  43.                 isUpdateIcon = !isUpdateIcon;
  44.                 statusBar.Update();
  45.             }; */
  46.  
  47.             // It doesn't change the icon and tooltip:
  48.             customPane.MouseDown += (o, e) => {
  49.                 Task.Run(() => {
  50.                     Thread.Sleep(3000); // some long work...
  51.  
  52.                     // In WPF or WinForms I am to update GUI from other thead via Dispatcher.
  53.                     // But how to get something like Dispatcher for AutoCAD?
  54.                     var pane = o as AcWin.Pane;
  55.                     pane.Icon = getIcon();
  56.                     pane.ToolTipText = getTooltip();
  57.                     isUpdateIcon = !isUpdateIcon;
  58.                     statusBar.Update();
  59.                 });
  60.             };
  61.  
  62.             statusBar.Panes.Add(customPane);
  63.         }
  64.     }
  65. }
  66.  

Thank you.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: How to update AutoCAD GUI from other threads?
« Reply #1 on: January 22, 2024, 12:02:15 PM »
The problem is solved. Sorry, I can't remove this topic.  :-(
« Last Edit: January 22, 2024, 12:12:43 PM by Andrey Bushman »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2071
  • class keyThumper<T>:ILazy<T>
Re: How to update AutoCAD GUI from other threads?
« Reply #2 on: January 22, 2024, 02:02:04 PM »
What was the solution Andrey ?
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.