Use a lightweight timer to delay task

This should also fix CID18182
This commit is contained in:
_aLfa_ 2014-09-24 16:57:00 +02:00
parent 57f23a69c0
commit a53c8dbe45
2 changed files with 18 additions and 10 deletions

View File

@ -14,6 +14,7 @@ using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
@ -7351,17 +7352,11 @@ namespace Nikse.SubtitleEdit.Forms
{
var last = _lastAdditions[_lastAdditions.Count - 1];
numericUpDownStartNumber.Value = last.Index + 1;
Timer t = new Timer();
t.Interval = 200;
t.Tick += t_Tick;
t.Start();
}
}
private void t_Tick(object sender, EventArgs e)
{
(sender as Timer).Stop();
ButtonStartOcrClick(null, null);
// Simulate a click on ButtonStartOcr in 200ms.
var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
Utilities.TaskDelay(200).ContinueWith(_ => ButtonStartOcrClick(null, null), uiContext);
}
}
private void VobSubOcr_Resize(object sender, EventArgs e)

View File

@ -12,6 +12,7 @@ using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
@ -4115,5 +4116,17 @@ namespace Nikse.SubtitleEdit.Logic
return text;
}
/// <summary>
/// Creates a task that will complete after a time delay.
/// </summary>
/// <param name="millisecondsDelay">The number of milliseconds to wait before completing the returned task.</param>
/// <returns>A task that represents the time delay.</returns>
public static Task TaskDelay(int millisecondsDelay)
{
var tcs = new TaskCompletionSource<object>();
var t = new System.Threading.Timer(_ => tcs.SetResult(null));
t.Change(millisecondsDelay, -1);
return tcs.Task;
}
}
}