3838
3939using Opc . Ua . Client ;
4040using Opc . Ua . Client . Controls ;
41+ using System . Threading . Tasks ;
4142
4243namespace Opc . Ua . Sample . Controls
4344{
4445 public partial class ArgumentListCtrl : Opc . Ua . Client . Controls . BaseListCtrl
4546 {
4647 public ArgumentListCtrl ( )
4748 {
48- InitializeComponent ( ) ;
49- SetColumns ( m_ColumnNames ) ;
49+ InitializeComponent ( ) ;
50+ SetColumns ( m_ColumnNames ) ;
5051 }
5152
5253 #region Private Fields
@@ -56,13 +57,13 @@ public ArgumentListCtrl()
5657 /// The columns to display in the control.
5758 /// </summary>
5859 private readonly object [ ] [ ] m_ColumnNames = new object [ ] [ ]
59- {
60- new object [ ] { "Name" , HorizontalAlignment . Left , null } ,
61- new object [ ] { "DataType" , HorizontalAlignment . Left , null } ,
62- new object [ ] { "Value" , HorizontalAlignment . Left , null } ,
63- new object [ ] { "Description" , HorizontalAlignment . Left , null } ,
64- } ;
65- #endregion
60+ {
61+ new object [ ] { "Name" , HorizontalAlignment . Left , null } ,
62+ new object [ ] { "DataType" , HorizontalAlignment . Left , null } ,
63+ new object [ ] { "Value" , HorizontalAlignment . Left , null } ,
64+ new object [ ] { "Description" , HorizontalAlignment . Left , null } ,
65+ } ;
66+ #endregion
6667
6768 #region Public Interface
6869 /// <summary>
@@ -77,17 +78,17 @@ public void Clear()
7778 /// <summary>
7879 /// Sets the nodes in the control.
7980 /// </summary>
80- public bool Update ( Session session , NodeId methodId , bool inputArgs )
81+ public async Task < bool > UpdateAsync ( Session session , NodeId methodId , bool inputArgs )
8182 {
82- if ( session == null ) throw new ArgumentNullException ( "session" ) ;
83+ if ( session == null ) throw new ArgumentNullException ( "session" ) ;
8384 if ( methodId == null ) throw new ArgumentNullException ( "methodId" ) ;
84-
85+
8586 Clear ( ) ;
86-
87+
8788 m_session = session ;
8889
8990 // find the method.
90- MethodNode method = session . NodeCache . Find ( methodId ) as MethodNode ;
91+ MethodNode method = await session . NodeCache . FindAsync ( methodId ) as MethodNode ;
9192
9293 if ( method == null )
9394 {
@@ -96,7 +97,7 @@ public bool Update(Session session, NodeId methodId, bool inputArgs)
9697
9798 // select the property to find.
9899 QualifiedName browseName = null ;
99-
100+
100101 if ( inputArgs )
101102 {
102103 browseName = Opc . Ua . BrowseNames . InputArguments ;
@@ -107,15 +108,15 @@ public bool Update(Session session, NodeId methodId, bool inputArgs)
107108 }
108109
109110 // fetch the argument list.
110- VariableNode argumentsNode = session . NodeCache . Find ( methodId , ReferenceTypeIds . HasProperty , false , true , browseName ) as VariableNode ;
111+ VariableNode argumentsNode = await session . NodeCache . FindAsync ( methodId , ReferenceTypeIds . HasProperty , false , true , browseName ) as VariableNode ;
111112
112113 if ( argumentsNode == null )
113114 {
114115 return false ;
115116 }
116117
117118 // read the value from the server.
118- DataValue value = m_session . ReadValue ( argumentsNode . NodeId ) ;
119+ DataValue value = await m_session . ReadValueAsync ( argumentsNode . NodeId ) ;
119120
120121 ExtensionObject [ ] argumentsList = value . Value as ExtensionObject [ ] ;
121122
@@ -130,7 +131,7 @@ public bool Update(Session session, NodeId methodId, bool inputArgs)
130131 AdjustColumns ( ) ;
131132
132133 return ItemsLV . Items . Count > 0 ;
133- }
134+ }
134135
135136 /// <summary>
136137 /// Returns the argument values
@@ -172,8 +173,8 @@ public void SetValues(VariantCollection values)
172173
173174 AdjustColumns ( ) ;
174175 }
175- #endregion
176-
176+ #endregion
177+
177178 #region Overridden Methods
178179 /// <see cref="BaseListCtrl.PickItems" />
179180 protected override void PickItems ( )
@@ -184,66 +185,73 @@ protected override void PickItems()
184185
185186 /// <see cref="BaseListCtrl.EnableMenuItems" />
186187 protected override void EnableMenuItems ( ListViewItem clickedItem )
187- {
188- EditMI . Enabled = ItemsLV . SelectedItems . Count == 1 ;
188+ {
189+ EditMI . Enabled = ItemsLV . SelectedItems . Count == 1 ;
189190 ClearValueMI . Enabled = EditMI . Enabled ;
190- }
191-
191+ }
192+
192193 /// <see cref="BaseListCtrl.UpdateItem" />
193- protected override void UpdateItem ( ListViewItem listItem , object item )
194+ protected override async void UpdateItem ( ListViewItem listItem , object item )
194195 {
195- Argument argument = item as Argument ;
196+ try
197+ {
198+ Argument argument = item as Argument ;
196199
197- if ( argument == null )
198- {
199- base . UpdateItem ( listItem , item ) ;
200- return ;
201- }
200+ if ( argument == null )
201+ {
202+ base . UpdateItem ( listItem , item ) ;
203+ return ;
204+ }
202205
203- listItem . SubItems [ 0 ] . Text = String . Format ( "{0}" , argument . Name ) ;
206+ listItem . SubItems [ 0 ] . Text = String . Format ( "{0}" , argument . Name ) ;
204207
205- INode datatype = m_session . NodeCache . Find ( argument . DataType ) ;
208+ INode datatype = await m_session . NodeCache . FindAsync ( argument . DataType ) ;
206209
207- if ( datatype != null )
208- {
209- listItem . SubItems [ 1 ] . Text = String . Format ( "{0}" , datatype ) ;
210- }
211- else
212- {
213- listItem . SubItems [ 1 ] . Text = String . Format ( "{0}" , argument . DataType ) ;
214- }
215-
216- if ( argument . ValueRank >= ValueRanks . OneOrMoreDimensions )
217- {
218- listItem . SubItems [ 1 ] . Text += "[]" ;
219- }
210+ if ( datatype != null )
211+ {
212+ listItem . SubItems [ 1 ] . Text = String . Format ( "{0}" , datatype ) ;
213+ }
214+ else
215+ {
216+ listItem . SubItems [ 1 ] . Text = String . Format ( "{0}" , argument . DataType ) ;
217+ }
220218
221- if ( argument . Value == null )
222- {
223- argument . Value = TypeInfo . GetDefaultValue ( argument . DataType , argument . ValueRank , m_session . TypeTree ) ;
219+ if ( argument . ValueRank >= ValueRanks . OneOrMoreDimensions )
220+ {
221+ listItem . SubItems [ 1 ] . Text += "[]" ;
222+ }
224223
225224 if ( argument . Value == null )
226225 {
227- Type type = m_session . MessageContext . Factory . GetSystemType ( argument . DataType ) ;
226+ argument . Value = TypeInfo . GetDefaultValue ( argument . DataType , argument . ValueRank , m_session . TypeTree ) ;
228227
229- if ( type ! = null )
228+ if ( argument . Value = = null )
230229 {
231- if ( argument . ValueRank == ValueRanks . Scalar )
232- {
233- argument . Value = new ExtensionObject ( Activator . CreateInstance ( type ) ) ;
234- }
235- else
230+ Type type = m_session . MessageContext . Factory . GetSystemType ( argument . DataType ) ;
231+
232+ if ( type != null )
236233 {
237- argument . Value = new ExtensionObject [ 0 ] ;
234+ if ( argument . ValueRank == ValueRanks . Scalar )
235+ {
236+ argument . Value = new ExtensionObject ( Activator . CreateInstance ( type ) ) ;
237+ }
238+ else
239+ {
240+ argument . Value = new ExtensionObject [ 0 ] ;
241+ }
238242 }
239243 }
240244 }
241- }
242245
243- listItem . SubItems [ 2 ] . Text = String . Format ( "{0}" , argument . Value ) ;
244- listItem . SubItems [ 3 ] . Text = String . Format ( "{0}" , argument . Description . Text ) ;
246+ listItem . SubItems [ 2 ] . Text = String . Format ( "{0}" , argument . Value ) ;
247+ listItem . SubItems [ 3 ] . Text = String . Format ( "{0}" , argument . Description . Text ) ;
245248
246- listItem . Tag = item ;
249+ listItem . Tag = item ;
250+ }
251+ catch ( Exception exception )
252+ {
253+ GuiUtils . HandleException ( this . Text , MethodBase . GetCurrentMethod ( ) , exception ) ;
254+ }
247255 }
248256 #endregion
249257
@@ -269,7 +277,7 @@ private void EditMI_Click(object sender, EventArgs e)
269277 }
270278 catch ( Exception exception )
271279 {
272- GuiUtils . HandleException ( this . Text , MethodBase . GetCurrentMethod ( ) , exception ) ;
280+ GuiUtils . HandleException ( this . Text , MethodBase . GetCurrentMethod ( ) , exception ) ;
273281 }
274282 }
275283
@@ -290,7 +298,7 @@ private void ClearValueMI_Click(object sender, EventArgs e)
290298 }
291299 catch ( Exception exception )
292300 {
293- GuiUtils . HandleException ( this . Text , MethodBase . GetCurrentMethod ( ) , exception ) ;
301+ GuiUtils . HandleException ( this . Text , MethodBase . GetCurrentMethod ( ) , exception ) ;
294302 }
295303 }
296304 }
0 commit comments