TaskEx class for helper Task methods

This commit is contained in:
Jaex 2014-05-23 18:39:14 +03:00
parent 8cd5a8d86e
commit 881d24c70a
17 changed files with 28 additions and 30 deletions

View file

@ -133,7 +133,7 @@ private void SendPing(string ip)
{
btnPingPrimary.Enabled = btnPingSecondary.Enabled = false;
Task.Run(() =>
TaskEx.Run(() =>
{
PingResult pingResult = PingHelper.PingHost(ip);
MessageBox.Show(pingResult.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

View file

@ -471,7 +471,7 @@ public static void OpenURL(string url)
{
if (!string.IsNullOrEmpty(url))
{
Task.Run(() =>
TaskEx.Run(() =>
{
try
{
@ -598,7 +598,7 @@ public static object Clone(object obj)
public static void PlaySoundAsync(Stream stream)
{
Task.Run(() =>
TaskEx.Run(() =>
{
using (stream)
using (SoundPlayer soundPlayer = new SoundPlayer(stream))
@ -694,7 +694,7 @@ public static void WaitWhileAsync(Func<bool> check, int interval, int timeout, A
{
bool result = false;
Task.Run(() =>
TaskEx.Run(() =>
{
if (waitStart > 0)
{

View file

@ -116,7 +116,7 @@
<Compile Include="PingHelper.cs" />
<Compile Include="PingResult.cs" />
<Compile Include="ProxyInfo.cs" />
<Compile Include="Task.cs" />
<Compile Include="TaskEx.cs" />
<Compile Include="UITypeEditors\EnumDescriptionConverter.cs" />
<Compile Include="UITypeEditors\DirectoryNameEditor.cs" />
<Compile Include="UITypeEditors\MyColorConverter.cs" />

View file

@ -66,7 +66,7 @@ private void Save()
public void SaveAsync(string filePath)
{
Task.Run(() => Save(filePath));
TaskEx.Run(() => Save(filePath));
}
private void SaveAsync()

View file

@ -29,22 +29,20 @@ You should have received a copy of the GNU General Public License
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HelpersLib
{
public static class Task
public static class TaskEx
{
public static void Run(Action action)
public static Task Run(Action action)
{
ThreadPool.QueueUserWorkItem(state => action());
return Task.Factory.StartNew(action);
}
public static void Run(Action action, Action completed)
public static Task Run(Action action, Action completed)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (sender, e) => action();
bw.RunWorkerCompleted += (sender, e) => completed();
bw.RunWorkerAsync();
return Run(action).ContinueWith(task => completed(), TaskScheduler.FromCurrentSynchronizationContext());
}
}
}

View file

@ -73,7 +73,7 @@ public List<HistoryItem> GetHistoryItems()
public static void AddHistoryItemAsync(string historyPath, HistoryItem historyItem)
{
Task.Run(() =>
TaskEx.Run(() =>
{
HistoryManager history = new HistoryManager(historyPath);
history.AppendHistoryItem(historyItem);

View file

@ -108,7 +108,7 @@ private void RefreshSourcesAsync()
btnRefreshSources.Enabled = false;
DirectShowDevices devices = null;
Task.Run(() =>
TaskEx.Run(() =>
{
using (FFmpegHelper ffmpeg = new FFmpegHelper(Options))
{

View file

@ -42,7 +42,7 @@ public partial class MainForm
private void InitHotkeys()
{
Task.Run(() =>
TaskEx.Run(() =>
{
if (Program.HotkeysConfig == null)
{
@ -196,7 +196,7 @@ private void DoCapture(ScreenCaptureDelegate capture, CaptureType captureType, T
if (taskSettings.CaptureSettings.IsDelayScreenshot && taskSettings.CaptureSettings.DelayScreenshot > 0)
{
Task.Run(() =>
TaskEx.Run(() =>
{
int sleep = (int)(taskSettings.CaptureSettings.DelayScreenshot * 1000);
Thread.Sleep(sleep);
@ -477,7 +477,7 @@ private void PrepareCaptureMenuAsync(ToolStripMenuItem tsmiWindow, EventHandler
WindowsList windowsList = new WindowsList();
List<WindowInfo> windows = null;
Task.Run(() =>
TaskEx.Run(() =>
{
windows = windowsList.GetVisibleWindowsList();
},

View file

@ -134,7 +134,7 @@ public void StartRecording(TaskSettings TaskSettings)
ScreenRegionManager screenRegionManager = new ScreenRegionManager();
screenRegionManager.Start(captureRectangle);
Task.Run(() =>
TaskEx.Run(() =>
{
try
{

View file

@ -313,7 +313,7 @@ private void openURLToolStripMenuItem_Click(object sender, EventArgs e)
if (task != null && task.Info != null && task.Info.Result != null && !string.IsNullOrEmpty(task.Info.Result.ToString()))
{
Task.Run(() => Process.Start(task.Info.Result.ToString()));
TaskEx.Run(() => Process.Start(task.Info.Result.ToString()));
}
}
}

View file

@ -316,7 +316,7 @@ private static void Run(string[] args)
SettingsResetEvent = new ManualResetEvent(false);
UploaderSettingsResetEvent = new ManualResetEvent(false);
HotkeySettingsResetEvent = new ManualResetEvent(false);
Task.Run(() => LoadSettings());
TaskEx.Run(() => LoadSettings());
DebugHelper.WriteLine("MainForm init started");
MainForm = new MainForm();
@ -535,7 +535,7 @@ public static void UploadersConfigSaveAsync()
{
if (uploaderConfigWatcher != null) uploaderConfigWatcher.EnableRaisingEvents = false;
Task.Run(() =>
TaskEx.Run(() =>
{
UploadersConfig.Save(Program.UploadersConfigFilePath);
},

View file

@ -337,7 +337,7 @@ public static void DownloadAndUploadFile(string url, string filename, TaskSettin
string downloadPath = null;
bool isDownloaded = false;
Task.Run(() =>
TaskEx.Run(() =>
{
downloadPath = TaskHelpers.CheckFilePath(taskSettings.CaptureFolder, filename, taskSettings);

View file

@ -554,7 +554,7 @@ private void openURLToolStripMenuItem_Click(object sender, EventArgs e)
FtpItem file = lvFTPList.SelectedItems[0].Tag as FtpItem;
if (file != null && file.ItemType == FtpItemType.File)
{
Task.Run(() => Process.Start(Account.GetUriPath("@" + file.FullPath)));
TaskEx.Run(() => Process.Start(Account.GetUriPath("@" + file.FullPath)));
}
}
}

View file

@ -76,7 +76,7 @@ public override void StopUpload()
{
stopUpload = true;
Task.Run(() => ftpClient.StopUpload());
TaskEx.Run(() => ftpClient.StopUpload());
}
}

View file

@ -65,7 +65,7 @@ public void OpenDirectory(string path)
DropboxContentInfo contentInfo = null;
Task.Run(() =>
TaskEx.Run(() =>
{
contentInfo = dropbox.GetMetadata(path, true);
},

View file

@ -136,7 +136,7 @@ public void TranslateAsync(GoogleTranslateInfo info)
btnTranslate.Enabled = false;
btnTranslateTo.Enabled = false;
Task.Run(() =>
TaskEx.Run(() =>
{
info = new GoogleTranslate(Config.APIKey).TranslateText(info);
},

View file

@ -881,7 +881,7 @@ public void TestFTPAccountAsync(FTPAccount acc)
{
ucFTPAccounts.btnTest.Enabled = false;
Task.Run(() =>
TaskEx.Run(() =>
{
TestFTPAccount(acc, false);
},
@ -1500,7 +1500,7 @@ private void TestCustomUploader(CustomUploaderType type, CustomUploaderItem item
txtCustomUploaderLog.ResetText();
Task.Run(() =>
TaskEx.Run(() =>
{
try
{