- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Start, Pause Token , CancelSourceToken trong Task Parallelism
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách Start, Pause, Resume và Stop trong Task Parallel C# Winform.
Trong NetFramework, thì Task có hỗ trợ CancelSourceToken, còn PauseTokenSource thì không có hỗ trợ.
Nên chúng ta cần viết thêm hai class PauseToken
và PauseTokenSource
.
Giao diện demo ứng dụng Start, Pause, Resume, Stop Task Parallel C#:
- Tạo Class PauseTokenSource.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PauseTokenSource
{
class PauseTokenSource
{
private volatile TaskCompletionSource<bool> m_paused;
internal static readonly Task s_completedTask = Task.FromResult(true);
public bool IsPaused
{
get { return m_paused != null; }
set
{
if (value)
{
Interlocked.CompareExchange(
ref m_paused, new TaskCompletionSource<bool>(), null);
}
else
{
while (true)
{
var tcs = m_paused;
if (tcs == null) return;
if (Interlocked.CompareExchange(ref m_paused, null, tcs) == tcs)
{
tcs.SetResult(true);
break;
}
}
}
}
}
public PauseToken Token { get { return new PauseToken(this); } }
internal Task WaitWhilePausedAsync()
{
var cur = m_paused;
return cur != null ? cur.Task : s_completedTask;
}
}
}
2. Tiếp tục tạo thêm Class PauseToken.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PauseTokenSource
{
class PauseToken
{
private readonly PauseTokenSource m_source;
internal PauseToken(PauseTokenSource source) { m_source = source; }
public bool IsPaused { get { return m_source != null && m_source.IsPaused; } }
public Task WaitWhilePausedAsync()
{
return IsPaused ?
m_source.WaitWhilePausedAsync() :
PauseTokenSource.s_completedTask;
}
}
}
3. Viết code cho Form Main ứng dụng Demo Task C#:
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PauseTokenSource
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
PauseTokenSource pts;
CancellationTokenSource cts;
private async void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
btnPause.Enabled = true;
btnStop.Enabled = true;
rbResult.Clear();
cts = new CancellationTokenSource();
pts = new PauseTokenSource();
var tokenSource = cts.Token;
await SomeMethodAsync(tokenSource);
}
public async Task SomeMethodAsync(CancellationToken ct)
{
for (int i = 0; i <= 100; i++)
{
this.BeginInvoke(new Action(()=>
{
rbResult.Text += "i = " + i + "\r\n";
progressBar1.Value = i;
lbl_status.Text = i + "%";
if(i == 100)
{
MessageBox.Show("Finish");
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
}
}));
if (ct.IsCancellationRequested)
{
break;
}
await Task.Delay(200);
await pts.WaitWhilePausedAsync();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
pts.IsPaused = !pts.IsPaused;
if (pts.IsPaused)
{
btnPause.Text = "Resume";
}
else
{
btnPause.Text = "Pause";
}
}
private void btnStop_Click(object sender, EventArgs e)
{
cts.Cancel();
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
}
}
}
Các bạn có thể download source code bên dưới về để tham khảo.
Thanks for watching!