/* This is a blink label user control --------------------------------*/
public partial class BlinkLabel : Label {
private CancellationTokenSource ts;
private Task task1;
public BlinkLabel()
{
InitializeComponent();
}
public void StartBlink()
{
ts=new CancellationTokenSource();
CancellationToken ct = ts.Token;
if ( isRunningTask(task1) ) return;
task1 = Task.Factory.StartNew(() =>
{
while (true)
{
ct.ThrowIfCancellationRequested();
Thread.Sleep(500);
Visible = !Visible;
}
}, ct);
task1.ContinueWith(t =>
{
Visible = false;
},TaskContinuationOptions.None);
}
public void StopBlink()
{
ts.Cancel();
}
}/* This is a my main form that use blink label user control--------------------------------*/
public partial class MainForm: Form
{
public MainForm
{
InitializeComponent();
blinkLabel1.Text="Blinking message...";
blinkLabel1.StartBlink(); //Works
blinkLabel1.StopBlink(); //Not works - blink not stop.
}
}I have some question:
1- why cancel request doesn't work?
2- how we can debug parallel program? when i use Ctrl+F5 the program works, when use F5 invoke errors occured.
3- Does exist better way to create blinkLable user control instead Task?
please get a solution for this problem, i don't have enough experience in parallel programming. thanks/.