12
12
using System . Threading . Tasks ;
13
13
using System . Windows . Forms ;
14
14
using osu_Lyrics . Properties ;
15
+ using System . Threading ;
15
16
16
17
namespace osu_Lyrics
17
18
{
@@ -226,7 +227,7 @@ private static double Now()
226
227
/// </summary>
227
228
/// <param name="data">[HASH]: ... | [ARTIST]: ..., [TITLE]: ...</param>
228
229
/// <returns>List<string></returns>
229
- private static async Task < List < Lyric > > GetLyrics ( IDictionary < string , string > data )
230
+ private static async Task < List < Lyric > > GetLyricsAsync ( IDictionary < string , string > data )
230
231
{
231
232
try
232
233
{
@@ -310,7 +311,7 @@ private List<Lyric> lyricsCache
310
311
}
311
312
}
312
313
313
- private async void Osu_Signal ( string line )
314
+ private void Osu_Signal ( string line )
314
315
{
315
316
var data = line . Split ( '|' ) ;
316
317
if ( data . Length != 5 )
@@ -321,27 +322,50 @@ private async void Osu_Signal(string line)
321
322
// 재생 중인 곡이 바꼈다!
322
323
if ( data [ 1 ] != curAudio . Path )
323
324
{
324
- lyricsCache = new List < Lyric >
325
- {
326
- new Lyric ( 0 , "가사 받는 중..." )
327
- } ;
328
-
329
325
curAudio = new Audio ( data [ 1 ] , data [ 4 ] ) ;
326
+ UpdateLyrics ( ) ;
327
+ }
328
+ curTime = DateTimeOffset . Now . Subtract (
329
+ DateTimeOffset . FromFileTime ( Convert . ToInt64 ( data [ 0 ] , 16 ) )
330
+ ) . TotalSeconds + Convert . ToDouble ( data [ 2 ] ) ;
331
+ _playbackRate = 1 + Convert . ToDouble ( data [ 3 ] ) / 100 ;
332
+ }
333
+
334
+ private CancellationTokenSource cts ;
335
+
336
+ private void UpdateLyrics ( )
337
+ {
338
+ if ( cts != null )
339
+ {
340
+ cts . Cancel ( ) ;
341
+ return ;
342
+ }
330
343
344
+ lyricsCache = new List < Lyric >
345
+ {
346
+ new Lyric ( 0 , "가사 받는 중..." )
347
+ } ;
348
+ cts = new CancellationTokenSource ( ) ;
349
+ Task . Run ( async ( ) =>
350
+ {
351
+ cts . Token . ThrowIfCancellationRequested ( ) ;
331
352
// 파일 해시로 가사 검색
332
- var newLyrics = await GetLyrics ( new Dictionary < string , string >
353
+ var newLyrics = await GetLyricsAsync ( new Dictionary < string , string >
333
354
{
334
355
{ "[HASH]" , curAudio . Hash }
335
356
} ) ;
357
+
336
358
if ( newLyrics == null && curAudio . Beatmap != null )
337
359
{
360
+ cts . Token . ThrowIfCancellationRequested ( ) ;
338
361
// 음악 정보로 가사 검색
339
- newLyrics = await GetLyrics ( new Dictionary < string , string >
362
+ newLyrics = await GetLyricsAsync ( new Dictionary < string , string >
340
363
{
341
364
{ "[TITLE]" , curAudio . Beatmap . TitleUnicode ?? curAudio . Beatmap . Title } ,
342
365
{ "[ARTIST]" , curAudio . Beatmap . ArtistUnicode ?? curAudio . Beatmap . Artist }
343
366
} ) ;
344
367
}
368
+
345
369
if ( newLyrics != null )
346
370
{
347
371
newLyrics . Insert ( 0 , new Lyric ( ) ) ;
@@ -354,12 +378,19 @@ private async void Osu_Signal(string line)
354
378
} ;
355
379
}
356
380
357
- lyricsCache = newLyrics ;
358
- }
359
- curTime = DateTimeOffset . Now . Subtract (
360
- DateTimeOffset . FromFileTime ( Convert . ToInt64 ( data [ 0 ] , 16 ) )
361
- ) . TotalSeconds + Convert . ToDouble ( data [ 2 ] ) ;
362
- _playbackRate = 1 + Convert . ToDouble ( data [ 3 ] ) / 100 ;
381
+ cts . Token . ThrowIfCancellationRequested ( ) ;
382
+ Invoke ( new MethodInvoker ( ( ) =>
383
+ {
384
+ lyricsCache = newLyrics ;
385
+ } ) ) ;
386
+ } , cts . Token ) . ContinueWith ( result => Invoke ( new MethodInvoker ( ( ) =>
387
+ {
388
+ cts = null ;
389
+ if ( result . IsCanceled )
390
+ {
391
+ UpdateLyrics ( ) ;
392
+ }
393
+ } ) ) ) ;
363
394
}
364
395
365
396
0 commit comments