3333using System . Data ;
3434using System . Text ;
3535using System . Windows . Forms ;
36+ using System . Threading . Tasks ;
37+ using System . Threading ;
3638
3739namespace Opc . Ua . Gds . Client . Controls
3840{
@@ -350,7 +352,7 @@ private string SelectDiscoveryUrl(ApplicationDescription server)
350352 return url ;
351353 }
352354
353- private void DiscoveryTreeView_BeforeExpand ( object sender , TreeViewCancelEventArgs e )
355+ private async void DiscoveryTreeView_BeforeExpand ( object sender , TreeViewCancelEventArgs e )
354356 {
355357 if ( e . Node . Nodes . Count != 1 || ! String . IsNullOrEmpty ( e . Node . Nodes [ 0 ] . Text ) )
356358 {
@@ -361,21 +363,15 @@ private void DiscoveryTreeView_BeforeExpand(object sender, TreeViewCancelEventAr
361363
362364 if ( RootFolders . LocalMachine . Equals ( e . Node . Tag ) )
363365 {
364- m_lds . BeginFindServers (
365- OnFindServersComplete ,
366- new ExpandNodeData ( ) { Parent = e . Node , Lds = m_lds } ) ;
367-
366+ // async replace for BeginFindServers
367+ await PopulateServersNodeAsync ( e . Node , null ) ;
368368 return ;
369369 }
370370
371371 if ( RootFolders . LocalNetwork . Equals ( e . Node . Tag ) )
372372 {
373- m_lds . BeginFindServersOnNetwork (
374- 0 ,
375- 100 ,
376- OnFindServersOnNetworkComplete ,
377- new ExpandNodeData ( ) { Parent = e . Node , Lds = m_lds } ) ;
378-
373+ // async replace for BeginFindServersOnNetwork
374+ await PopulateServersOnNetworkNodeAsync ( e . Node , 0 , 100 ) ;
379375 return ;
380376 }
381377
@@ -405,32 +401,61 @@ private void DiscoveryTreeView_BeforeExpand(object sender, TreeViewCancelEventAr
405401
406402 if ( e . Node . Tag is Uri )
407403 {
408- m_lds . BeginFindServers (
409- e . Node . Tag . ToString ( ) ,
410- null ,
411- null ,
412- null ,
413- null ,
414- OnFindServersComplete ,
415- new ExpandNodeData ( ) { Parent = e . Node , Lds = m_lds } ) ;
416-
404+ await PopulateServersNodeAsync ( e . Node , e . Node . Tag . ToString ( ) ) ;
417405 return ;
418406 }
419407 }
420408
421- private void OnFindServersComplete ( IAsyncResult result )
409+ private async Task PopulateServersOnNetworkNodeAsync ( TreeNode parent , uint startingRecordId , uint maxRecordsToReturn )
422410 {
423- if ( InvokeRequired )
411+ try
424412 {
425- BeginInvoke ( new AsyncCallback ( OnFindServersComplete ) , result ) ;
426- return ;
413+ var ( servers , lastCounterResetTime ) = await m_lds . FindServersOnNetworkAsync (
414+ null ,
415+ null ,
416+ startingRecordId ,
417+ maxRecordsToReturn ,
418+ null ,
419+ CancellationToken . None ) ;
420+
421+ foreach ( ServerOnNetwork server in servers )
422+ {
423+ if ( server . ServerCapabilities != null && server . ServerCapabilities . Contains ( "LDS" ) )
424+ {
425+ continue ;
426+ }
427+
428+ TreeNode node = new TreeNode ( String . Format ( "{0}" , server . ServerName ) ) ;
429+ node . SelectedImageIndex = node . ImageIndex = ImageIndex . Server ;
430+ node . Tag = server ;
431+ node . Nodes . Add ( new TreeNode ( ) ) ;
432+ parent . Nodes . Add ( node ) ;
433+ }
434+
435+ if ( DiscoveryTreeView . SelectedNode == parent )
436+ {
437+ ShowServerOnNetworks ( parent . Nodes ) ;
438+ }
439+ else
440+ {
441+ parent . Expand ( ) ;
442+ }
443+ }
444+ catch ( Exception ex )
445+ {
446+ Opc . Ua . Client . Controls . ExceptionDlg . Show ( Text , ex ) ;
427447 }
448+ }
428449
450+ private async Task PopulateServersNodeAsync ( TreeNode parent , string discoveryUrl )
451+ {
429452 try
430453 {
431- ExpandNodeData data = ( ExpandNodeData ) result . AsyncState ;
432-
433- List < ApplicationDescription > servers = data . Lds . EndFindServers ( result ) ;
454+ List < ApplicationDescription > servers = new List < ApplicationDescription > ( ) ;
455+ foreach ( var s in await m_lds . FindServersAsync ( discoveryUrl , null ) )
456+ {
457+ servers . Add ( s ) ;
458+ }
434459
435460 foreach ( ApplicationDescription server in servers )
436461 {
@@ -443,24 +468,24 @@ private void OnFindServersComplete(IAsyncResult result)
443468 node . SelectedImageIndex = node . ImageIndex = ( server . ApplicationType == ApplicationType . DiscoveryServer ) ? ImageIndex . LocalNetwork : ImageIndex . Server ;
444469 node . Tag = server ;
445470 node . Nodes . Add ( new TreeNode ( ) ) ;
446- data . Parent . Nodes . Add ( node ) ;
471+ parent . Nodes . Add ( node ) ;
447472 }
448473
449- if ( DiscoveryTreeView . SelectedNode == data . Parent )
474+ if ( DiscoveryTreeView . SelectedNode == parent )
450475 {
451- ShowApplicationDescriptions ( data . Parent . Nodes ) ;
476+ ShowApplicationDescriptions ( parent . Nodes ) ;
452477 }
453478 else
454479 {
455- data . Parent . Expand ( ) ;
480+ parent . Expand ( ) ;
456481 }
457482 }
458- catch ( Exception e )
483+ catch ( Exception ex )
459484 {
460- Opc . Ua . Client . Controls . ExceptionDlg . Show ( Text , e ) ;
485+ Opc . Ua . Client . Controls . ExceptionDlg . Show ( Text , ex ) ;
461486 }
462487 }
463-
488+
464489 private void ShowApplicationDescriptions ( TreeNodeCollection nodes )
465490 {
466491 ServersTable . Rows . Clear ( ) ;
@@ -509,50 +534,6 @@ private void ShowApplicationDescriptions(TreeNodeCollection nodes)
509534 }
510535 }
511536
512- private void OnFindServersOnNetworkComplete ( IAsyncResult result )
513- {
514- if ( InvokeRequired )
515- {
516- BeginInvoke ( new AsyncCallback ( OnFindServersOnNetworkComplete ) , result ) ;
517- return ;
518- }
519-
520- try
521- {
522- ExpandNodeData data = ( ExpandNodeData ) result . AsyncState ;
523-
524- DateTime lastCounterResetTime ;
525- List < ServerOnNetwork > servers = data . Lds . EndFindServersOnNetwork ( result , out lastCounterResetTime ) ;
526-
527- foreach ( ServerOnNetwork server in servers )
528- {
529- if ( server . ServerCapabilities . Contains ( "LDS" ) )
530- {
531- continue ;
532- }
533-
534- TreeNode node = new TreeNode ( String . Format ( "{0}" , server . ServerName ) ) ;
535- node . SelectedImageIndex = node . ImageIndex = ImageIndex . Server ;
536- node . Tag = server ;
537- node . Nodes . Add ( new TreeNode ( ) ) ;
538- data . Parent . Nodes . Add ( node ) ;
539- }
540-
541- if ( DiscoveryTreeView . SelectedNode == data . Parent )
542- {
543- ShowServerOnNetworks ( data . Parent . Nodes ) ;
544- }
545- else
546- {
547- data . Parent . Expand ( ) ;
548- }
549- }
550- catch ( Exception e )
551- {
552- Opc . Ua . Client . Controls . ExceptionDlg . Show ( Text , e ) ;
553- }
554- }
555-
556537 private void ShowServerOnNetworks ( TreeNodeCollection nodes )
557538 {
558539 ServersTable . Rows . Clear ( ) ;
@@ -601,37 +582,6 @@ private void ShowServerOnNetworks(TreeNodeCollection nodes)
601582 }
602583 }
603584
604- private class GetEndpointsData
605- {
606- public TreeNode Parent ;
607- public LocalDiscoveryServerClient Lds ;
608- }
609-
610- private void OnGetEndpointsComplete ( IAsyncResult result )
611- {
612- if ( InvokeRequired )
613- {
614- BeginInvoke ( new AsyncCallback ( OnGetEndpointsComplete ) , result ) ;
615- return ;
616- }
617-
618- GetEndpointsData data = ( GetEndpointsData ) result . AsyncState ;
619-
620- try
621- {
622- List < EndpointDescription > endpoints = data . Lds . EndGetEndpoints ( result ) ;
623-
624- if ( DiscoveryTreeView . SelectedNode == data . Parent )
625- {
626- ShowEndpointDescriptions ( endpoints ) ;
627- }
628- }
629- catch ( Exception e )
630- {
631- Opc . Ua . Client . Controls . ExceptionDlg . Show ( Text , e ) ;
632- }
633- }
634-
635585 private void ShowEndpointDescriptions ( List < EndpointDescription > endpoints )
636586 {
637587 EndpointsTable . Rows . Clear ( ) ;
@@ -845,7 +795,7 @@ private void ShowPanel(bool list)
845795 }
846796 }
847797
848- private void DiscoveryTreeView_AfterSelect ( object sender , TreeViewEventArgs e )
798+ private async void DiscoveryTreeView_AfterSelect ( object sender , TreeViewEventArgs e )
849799 {
850800 try
851801 {
@@ -858,9 +808,7 @@ private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
858808 {
859809 e . Node . Nodes . Clear ( ) ;
860810
861- m_lds . BeginFindServers (
862- OnFindServersComplete ,
863- new ExpandNodeData ( ) { Parent = e . Node , Lds = m_lds } ) ;
811+ await PopulateServersNodeAsync ( e . Node , null ) ;
864812 }
865813 else
866814 {
@@ -879,11 +827,7 @@ private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
879827 {
880828 e . Node . Nodes . Clear ( ) ;
881829
882- m_lds . BeginFindServersOnNetwork (
883- 0 ,
884- 1000 ,
885- OnFindServersOnNetworkComplete ,
886- new ExpandNodeData ( ) { Parent = e . Node , Lds = m_lds } ) ;
830+ await PopulateServersOnNetworkNodeAsync ( e . Node , 0 , 1000 ) ;
887831 }
888832 else
889833 {
@@ -945,11 +889,7 @@ private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
945889
946890 if ( discoveryUrl != null )
947891 {
948- m_lds . BeginGetEndpoints (
949- discoveryUrl ,
950- null ,
951- OnGetEndpointsComplete ,
952- new GetEndpointsData ( ) { Parent = e . Node , Lds = m_lds } ) ;
892+ await LoadEndpointsAndShowAsync ( e . Node , discoveryUrl ) ;
953893 }
954894 }
955895
@@ -969,11 +909,7 @@ private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
969909 {
970910 Cursor = Cursors . WaitCursor ;
971911
972- m_lds . BeginGetEndpoints (
973- server . DiscoveryUrl ,
974- null ,
975- OnGetEndpointsComplete ,
976- new GetEndpointsData ( ) { Parent = e . Node , Lds = m_lds } ) ;
912+ await LoadEndpointsAndShowAsync ( e . Node , server . DiscoveryUrl ) ;
977913 }
978914 finally
979915 {
@@ -993,11 +929,7 @@ private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
993929 ApplicationUriTextBox . Text = "---" ;
994930 ProductUriTextBox . Text = "---" ;
995931
996- m_lds . BeginGetEndpoints (
997- server . EndpointUrl . ToString ( ) ,
998- null ,
999- OnGetEndpointsComplete ,
1000- new GetEndpointsData ( ) { Parent = e . Node , Lds = m_lds } ) ;
932+ await LoadEndpointsAndShowAsync ( e . Node , server . EndpointUrl . ToString ( ) ) ;
1001933 }
1002934 }
1003935 catch ( Exception ex )
@@ -1006,6 +938,23 @@ private void DiscoveryTreeView_AfterSelect(object sender, TreeViewEventArgs e)
1006938 }
1007939 }
1008940
941+ private async Task LoadEndpointsAndShowAsync ( TreeNode parent , string discoveryUrl )
942+ {
943+ try
944+ {
945+ var endpoints = await m_lds . GetEndpointsAsync ( discoveryUrl , null ) ;
946+
947+ if ( DiscoveryTreeView . SelectedNode == parent )
948+ {
949+ ShowEndpointDescriptions ( endpoints ) ;
950+ }
951+ }
952+ catch ( Exception e )
953+ {
954+ Opc . Ua . Client . Controls . ExceptionDlg . Show ( Text , e ) ;
955+ }
956+ }
957+
1009958 private void FilterTextBox_TextChanged ( object sender , EventArgs e )
1010959 {
1011960 try
0 commit comments