11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Reflection ;
45using System . Threading ;
56using System . Windows . Media . Imaging ;
@@ -22,7 +23,7 @@ class App : IExternalApplication
2223 private Ui _mMyForm ;
2324
2425 // Separate thread to run Ui on
25- private Thread _UiThread ;
26+ private Thread _uiThread ;
2627
2728 public Result OnStartup ( UIControlledApplication a )
2829 {
@@ -32,31 +33,31 @@ public Result OnStartup(UIControlledApplication a)
3233 // Method to add Tab and Panel
3334 RibbonPanel panel = RibbonPanel ( a ) ;
3435 string thisAssemblyPath = Assembly . GetExecutingAssembly ( ) . Location ;
35- PushButton button =
36- panel . AddItem (
37- new PushButtonData ( "WPF Template" , "WPF Template" , thisAssemblyPath ,
38- "RevitTemplate.EntryCommand" ) ) as
39- PushButton ;
4036
41- // defines the tooltip displayed when the button is hovered over in Revit's ribbon
42- button . ToolTip = "Visual interface for debugging applications." ;
43-
44- // defines the icon for the button in Revit's ribbon - note the string formatting
45- Uri uriImage = new Uri ( "pack://application:,,,/RevitTemplate;component/Resources/code-small.png" ) ;
46- BitmapImage largeImage = new BitmapImage ( uriImage ) ;
47- button . LargeImage = largeImage ;
48-
49- PushButton button2 =
50- panel . AddItem (
51- new PushButtonData ( "WPF Template Multi-Thread" , "WPF Template Multi-Thread" , thisAssemblyPath ,
52- "RevitTemplate.EntryCommandSeparateThread" ) ) as
53- PushButton ;
37+ // BUTTON FOR THE SINGLE-THREADED WPF OPTION
38+ if ( panel . AddItem (
39+ new PushButtonData ( "WPF Template" , "WPF Template" , thisAssemblyPath ,
40+ "RevitTemplate.EntryCommand" ) ) is PushButton button )
41+ {
42+ // defines the tooltip displayed when the button is hovered over in Revit's ribbon
43+ button . ToolTip = "Visual interface for debugging applications." ;
44+ // defines the icon for the button in Revit's ribbon - note the string formatting
45+ Uri uriImage = new Uri ( "pack://application:,,,/RevitTemplate;component/Resources/code-small.png" ) ;
46+ BitmapImage largeImage = new BitmapImage ( uriImage ) ;
47+ button . LargeImage = largeImage ;
48+ }
5449
55- // defines the tooltip displayed when the button is hovered over in Revit's ribbon
56- button2 . ToolTip = "Visual interface for debugging applications." ;
50+ // BUTTON FOR THE MULTI-THREADED WPF OPTION
51+ if ( panel . AddItem (
52+ new PushButtonData ( "WPF Template\n Multi-Thread" , "WPF Template\n Multi-Thread" , thisAssemblyPath ,
53+ "RevitTemplate.EntryCommandSeparateThread" ) ) is PushButton button2 )
54+ {
55+ button2 . ToolTip = "Visual interface for debugging applications." ;
56+ Uri uriImage = new Uri ( "pack://application:,,,/RevitTemplate;component/Resources/code-small.png" ) ;
57+ BitmapImage largeImage = new BitmapImage ( uriImage ) ;
58+ button2 . LargeImage = largeImage ;
59+ }
5760
58- // defines the icon for the button in Revit's ribbon - note the string formatting
59- button2 . LargeImage = largeImage ;
6061
6162 // listeners/watchers for external events (if you choose to use them)
6263 a . ApplicationClosing += a_ApplicationClosing ; //Set Application to Idling
@@ -82,16 +83,14 @@ public Result OnShutdown(UIControlledApplication a)
8283 public void ShowForm ( UIApplication uiapp )
8384 {
8485 // If we do not have a dialog yet, create and show it
85- if ( _mMyForm == null || _mMyForm != null ) // || m_MyForm.IsDisposed
86- {
87- //EXTERNAL EVENTS WITH ARGUMENTS
88- EventHandlerWithStringArg evString = new EventHandlerWithStringArg ( ) ;
89- EventHandlerWithWpfArg evWpf = new EventHandlerWithWpfArg ( ) ;
90-
91- // The dialog becomes the owner responsible for disposing the objects given to it.
92- _mMyForm = new Ui ( uiapp , evString , evWpf ) ;
93- _mMyForm . Show ( ) ;
94- }
86+ if ( _mMyForm != null && _mMyForm == null ) return ;
87+ //EXTERNAL EVENTS WITH ARGUMENTS
88+ EventHandlerWithStringArg evStr = new EventHandlerWithStringArg ( ) ;
89+ EventHandlerWithWpfArg evWpf = new EventHandlerWithWpfArg ( ) ;
90+
91+ // The dialog becomes the owner responsible for disposing the objects given to it.
92+ _mMyForm = new Ui ( uiapp , evStr , evWpf ) ;
93+ _mMyForm . Show ( ) ;
9594 }
9695
9796 /// <summary>
@@ -103,28 +102,26 @@ public void ShowForm(UIApplication uiapp)
103102 public void ShowFormSeparateThread ( UIApplication uiapp )
104103 {
105104 // If we do not have a thread started or has been terminated start a new one
106- if ( _UiThread is null || ! _UiThread . IsAlive )
105+ if ( ! ( _uiThread is null ) && _uiThread . IsAlive ) return ;
106+ //EXTERNAL EVENTS WITH ARGUMENTS
107+ EventHandlerWithStringArg evStr = new EventHandlerWithStringArg ( ) ;
108+ EventHandlerWithWpfArg evWpf = new EventHandlerWithWpfArg ( ) ;
109+
110+ _uiThread = new Thread ( ( ) =>
107111 {
108- //EXTERNAL EVENTS WITH ARGUMENTS
109- EventHandlerWithStringArg evStr = new EventHandlerWithStringArg ( ) ;
110- EventHandlerWithWpfArg eDatabaseStore = new EventHandlerWithWpfArg ( ) ;
111-
112- _UiThread = new Thread ( ( ) =>
113- {
114- SynchronizationContext . SetSynchronizationContext (
115- new DispatcherSynchronizationContext (
116- Dispatcher . CurrentDispatcher ) ) ;
117- // The dialog becomes the owner responsible for disposing the objects given to it.
118- _mMyForm = new Ui ( uiapp , evStr , eDatabaseStore ) ;
119- _mMyForm . Closed += ( s , e ) => Dispatcher . CurrentDispatcher . InvokeShutdown ( ) ;
120- _mMyForm . Show ( ) ;
121- Dispatcher . Run ( ) ;
122- } ) ;
123-
124- _UiThread . SetApartmentState ( ApartmentState . STA ) ;
125- _UiThread . IsBackground = true ;
126- _UiThread . Start ( ) ;
127- }
112+ SynchronizationContext . SetSynchronizationContext (
113+ new DispatcherSynchronizationContext (
114+ Dispatcher . CurrentDispatcher ) ) ;
115+ // The dialog becomes the owner responsible for disposing the objects given to it.
116+ _mMyForm = new Ui ( uiapp , evStr , evWpf ) ;
117+ _mMyForm . Closed += ( s , e ) => Dispatcher . CurrentDispatcher . InvokeShutdown ( ) ;
118+ _mMyForm . Show ( ) ;
119+ Dispatcher . Run ( ) ;
120+ } ) ;
121+
122+ _uiThread . SetApartmentState ( ApartmentState . STA ) ;
123+ _uiThread . IsBackground = true ;
124+ _uiThread . Start ( ) ;
128125 }
129126
130127 #region Idling & Closing
@@ -157,27 +154,26 @@ public RibbonPanel RibbonPanel(UIControlledApplication a)
157154 {
158155 a . CreateRibbonTab ( tab ) ;
159156 }
160- catch
157+ catch ( Exception ex )
161158 {
159+ Util . HandleError ( ex ) ;
162160 }
163161
164162 // Try to create ribbon panel.
165163 try
166164 {
167165 RibbonPanel panel = a . CreateRibbonPanel ( tab , "Develop" ) ;
168166 }
169- catch
167+ catch ( Exception ex )
170168 {
169+ Util . HandleError ( ex ) ;
171170 }
172171
173172 // Search existing tab for your panel.
174173 List < RibbonPanel > panels = a . GetRibbonPanels ( tab ) ;
175- foreach ( RibbonPanel p in panels )
174+ foreach ( RibbonPanel p in panels . Where ( p => p . Name == "Develop" ) )
176175 {
177- if ( p . Name == "Develop" )
178- {
179- ribbonPanel = p ;
180- }
176+ ribbonPanel = p ;
181177 }
182178
183179 //return panel
0 commit comments