31
31
32
32
using System ;
33
33
using System . Diagnostics ;
34
- using System . Threading ;
34
+ using System . Globalization ;
35
+ using System . Runtime . ExceptionServices ;
35
36
using System . Threading . Tasks ;
36
37
37
38
using RabbitMQ . Client ;
39
+ using RabbitMQ . Client . Exceptions ;
38
40
39
41
namespace CreateChannel
40
42
{
@@ -44,11 +46,11 @@ public static class Program
44
46
private const int ChannelsToOpen = 50 ;
45
47
46
48
private static int channelsOpened ;
47
- private static AutoResetEvent doneEvent ;
49
+ private readonly static TaskCompletionSource < bool > s_tcs = new ( ) ;
48
50
49
51
public static async Task Main ( )
50
52
{
51
- doneEvent = new AutoResetEvent ( false ) ;
53
+ AppDomain . CurrentDomain . FirstChanceException += CurrentDomain_FirstChanceException ;
52
54
53
55
var connectionFactory = new ConnectionFactory { } ;
54
56
await using IConnection connection = await connectionFactory . CreateConnectionAsync ( ) ;
@@ -67,26 +69,48 @@ public static async Task Main()
67
69
68
70
for ( int j = 0 ; j < channels . Length ; j ++ )
69
71
{
72
+ if ( j % 2 == 0 )
73
+ {
74
+ try
75
+ {
76
+ await channels [ j ] . QueueDeclarePassiveAsync ( Guid . NewGuid ( ) . ToString ( ) ) ;
77
+ }
78
+ catch ( Exception )
79
+ {
80
+ }
81
+ }
70
82
await channels [ j ] . DisposeAsync ( ) ;
71
83
}
72
84
}
73
85
74
- doneEvent . Set ( ) ;
86
+ s_tcs . SetResult ( true ) ;
75
87
} ) ;
76
88
77
89
Console . WriteLine ( $ "{ Repeats } times opening { ChannelsToOpen } channels on a connection. => Total channel open/close: { Repeats * ChannelsToOpen } ") ;
78
90
Console . WriteLine ( ) ;
79
91
Console . WriteLine ( "Opened" ) ;
80
- while ( ! doneEvent . WaitOne ( 500 ) )
92
+ while ( false == s_tcs . Task . IsCompleted )
81
93
{
94
+ await Task . Delay ( 500 ) ;
82
95
Console . WriteLine ( $ "{ channelsOpened , 5 } ") ;
83
96
}
84
97
watch . Stop ( ) ;
85
98
Console . WriteLine ( $ "{ channelsOpened , 5 } ") ;
86
99
Console . WriteLine ( ) ;
87
100
Console . WriteLine ( $ "Took { watch . Elapsed . TotalMilliseconds } ms") ;
101
+ }
102
+
103
+ private static string Now => DateTime . UtcNow . ToString ( "s" , CultureInfo . InvariantCulture ) ;
88
104
89
- Console . ReadLine ( ) ;
105
+ private static void CurrentDomain_FirstChanceException ( object sender , FirstChanceExceptionEventArgs e )
106
+ {
107
+ if ( e . Exception is OperationInterruptedException )
108
+ {
109
+ }
110
+ else
111
+ {
112
+ Console . Error . WriteLine ( "{0} [ERROR] {1}" , Now , e . Exception ) ;
113
+ }
90
114
}
91
115
}
92
116
}
0 commit comments