1
1
using System ;
2
2
using System . IO ;
3
3
using System . Text ;
4
+ using System . Threading . Tasks ;
4
5
5
6
namespace CeVIOAIProxy . Servers
6
7
{
@@ -47,10 +48,9 @@ public void Start()
47
48
this . watcher . Path = Path . GetDirectoryName ( Config . Instance . CommentTextFilePath ) ;
48
49
this . watcher . Filter = Path . GetFileName ( Config . Instance . CommentTextFilePath ) ;
49
50
this . watcher . IncludeSubdirectories = false ;
50
- this . watcher . NotifyFilter = NotifyFilters . CreationTime | NotifyFilters . LastWrite ;
51
+ this . watcher . NotifyFilter = NotifyFilters . LastWrite ;
51
52
this . watcher . InternalBufferSize = 64 * 1024 ;
52
53
53
- this . watcher . Created += this . Watcher_Created ;
54
54
this . watcher . Changed += this . Watcher_Changed ;
55
55
56
56
this . watcher . EnableRaisingEvents = true ;
@@ -70,8 +70,13 @@ public void Stop()
70
70
}
71
71
}
72
72
73
- private async void Watcher_Created ( object sender , FileSystemEventArgs e )
73
+ private string lastComment = string . Empty ;
74
+ private DateTime lastCommentTimestamp = DateTime . MinValue ;
75
+
76
+ private async void Watcher_Changed ( object sender , FileSystemEventArgs e )
74
77
{
78
+ var comment = string . Empty ;
79
+
75
80
lock ( this )
76
81
{
77
82
if ( ! Config . Instance . IsEnabledTextPolling )
@@ -83,28 +88,46 @@ private async void Watcher_Created(object sender, FileSystemEventArgs e)
83
88
{
84
89
return ;
85
90
}
91
+
86
92
}
87
93
88
- var comment = File . ReadAllText ( e . FullPath , new UTF8Encoding ( false ) ) ;
89
- await CeVIO . SpeakAsync ( comment ) ;
90
- }
94
+ var interval = 10 ;
95
+ var timeout = 1000 ;
91
96
92
- private async void Watcher_Changed ( object sender , FileSystemEventArgs e )
93
- {
94
- lock ( this )
97
+ for ( int i = 0 ; i < ( timeout / interval ) ; i ++ )
95
98
{
96
- if ( ! Config . Instance . IsEnabledTextPolling )
99
+ try
97
100
{
98
- return ;
101
+ comment = File . ReadAllText ( e . FullPath , new UTF8Encoding ( false ) ) ;
102
+ break ;
103
+ }
104
+ catch ( IOException )
105
+ {
106
+ await Task . Delay ( TimeSpan . FromMilliseconds ( interval ) ) ;
99
107
}
108
+ }
100
109
101
- if ( this . watcher == null )
110
+ if ( string . IsNullOrEmpty ( comment ) )
111
+ {
112
+ return ;
113
+ }
114
+
115
+ lock ( this )
116
+ {
117
+ var now = DateTime . Now ;
118
+
119
+ if ( ( now - this . lastCommentTimestamp ) < TimeSpan . FromSeconds ( 1 ) )
102
120
{
103
- return ;
121
+ if ( this . lastComment . Equals ( comment ) )
122
+ {
123
+ return ;
124
+ }
104
125
}
126
+
127
+ this . lastComment = comment ;
128
+ this . lastCommentTimestamp = now ;
105
129
}
106
130
107
- var comment = File . ReadAllText ( e . FullPath , new UTF8Encoding ( false ) ) ;
108
131
await CeVIO . SpeakAsync ( comment ) ;
109
132
}
110
133
}
0 commit comments