Skip to content

Commit 75ac908

Browse files
committed
more updates
1 parent fbc3e9f commit 75ac908

File tree

13 files changed

+551
-512
lines changed

13 files changed

+551
-512
lines changed

Samples/Controls.Net4/ClientForm.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
using System.Drawing;
3434
using System.IO;
3535
using System.Reflection;
36+
using System.Threading.Tasks;
3637
using System.Windows.Forms;
3738
using Opc.Ua.Client;
3839
using Opc.Ua.Client.Controls;
3940
using Opc.Ua.Configuration;
41+
using static System.Net.Mime.MediaTypeNames;
4042

4143
namespace Opc.Ua.Sample.Controls
4244
{
@@ -89,7 +91,7 @@ public ClientForm(
8991
EndpointSelectorCTRL.Initialize(m_endpoints, m_configuration);
9092

9193
// initialize control state.
92-
Disconnect();
94+
DisconnectAsync().GetAwaiter().GetResult();
9395
}
9496

9597
/// <summary>
@@ -130,7 +132,7 @@ void Window_FormClosing(object sender, FormClosingEventArgs e)
130132
/// <summary>
131133
/// Disconnect from the server if ths form is closing.
132134
/// </summary>
133-
protected override void OnClosing(CancelEventArgs e)
135+
protected override async void OnClosing(CancelEventArgs e)
134136
{
135137
if (m_masterForm == null && m_forms.Count > 0)
136138
{
@@ -147,14 +149,20 @@ protected override void OnClosing(CancelEventArgs e)
147149
form.Close();
148150
}
149151
}
150-
151-
Disconnect();
152+
try
153+
{
154+
await DisconnectAsync();
155+
}
156+
catch (Exception exception)
157+
{
158+
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
159+
}
152160
}
153161

154162
/// <summary>
155163
/// Disconnects from a server.
156164
/// </summary>
157-
public void Disconnect()
165+
public async Task DisconnectAsync()
158166
{
159167
if (m_session != null)
160168
{
@@ -167,7 +175,7 @@ public void Disconnect()
167175

168176
m_session.KeepAlive -= StandardClient_KeepAlive;
169177

170-
m_session.Close();
178+
await m_session.CloseAsync();
171179
m_session = null;
172180
}
173181

@@ -182,11 +190,11 @@ protected virtual void DoTest(Session session)
182190
MessageBox.Show("A handy place to put test code.");
183191
}
184192

185-
void EndpointSelectorCTRL_ConnectEndpoint(object sender, ConnectEndpointEventArgs e)
193+
private async void EndpointSelectorCTRL_ConnectEndpoint(object sender, ConnectEndpointEventArgs e)
186194
{
187195
try
188196
{
189-
Connect(e.Endpoint);
197+
await ConnectAsync(e.Endpoint);
190198
}
191199
catch (Exception exception)
192200
{
@@ -210,14 +218,14 @@ private void EndpointSelectorCTRL_OnChange(object sender, EventArgs e)
210218
/// <summary>
211219
/// Connects to a server.
212220
/// </summary>
213-
public async void Connect(ConfiguredEndpoint endpoint)
221+
public async Task ConnectAsync(ConfiguredEndpoint endpoint)
214222
{
215223
if (endpoint == null)
216224
{
217225
return;
218226
}
219227

220-
Session session = await SessionsCTRL.Connect(endpoint);
228+
Session session = await SessionsCTRL.ConnectAsync(endpoint);
221229

222230
if (session != null)
223231
{
@@ -339,11 +347,11 @@ private void StandardClient_Server_ReconnectComplete(object sender, EventArgs e)
339347
}
340348
}
341349

342-
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
350+
private async void MainForm_FormClosing(object sender, FormClosingEventArgs e)
343351
{
344352
try
345353
{
346-
SessionsCTRL.Close();
354+
await SessionsCTRL.CloseAsync();
347355

348356
if (m_masterForm == null)
349357
{
@@ -480,7 +488,7 @@ private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
480488
{
481489
try
482490
{
483-
System.Diagnostics.Process.Start(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + "WebHelp" + Path.DirectorySeparatorChar + "index.htm");
491+
System.Diagnostics.Process.Start(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + Path.DirectorySeparatorChar + "WebHelp" + Path.DirectorySeparatorChar + "index.htm");
484492
}
485493
catch (Exception ex)
486494
{

Samples/Controls.Net4/Common/ArgumentListCtrl.cs

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@
3838

3939
using Opc.Ua.Client;
4040
using Opc.Ua.Client.Controls;
41+
using System.Threading.Tasks;
4142

4243
namespace 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

Comments
 (0)