Witam!
Mam taki oto kod:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void watek1()
{
for (int a1 = 5; a1 >= 0; a1--)
{
for (int b1 = 59; b1 >= 0; b1--)
{
if (b1 < 10)
{
label1.Invoke(new Action(delegate() { label1.Text = a1.ToString() + ":0" + b1.ToString(); }));
}
else
{
label1.Invoke(new Action(delegate() { label1.Text = a1.ToString() + ":" + b1.ToString(); }));
}
Thread.Sleep(1000);
if (a1 == 0 && b1 == 0)
{
label13.Invoke(new Action(delegate() { label13.Text = "Zrobione!"; }));
}
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
Thread thread1;
thread1 = new Thread(watek1);
thread1.IsBackground = true;
if (thread1.IsAlive == true)
{
thread1.Interrupt();
thread1.Abort();
thread1.Start();
}
else
{
thread1.Start();
}
}
}
}
}
I mam pytanie, czemu nie działają funkcje thread1.Interrupt() oraz thread1.Abort()? Wątek startuje od nowa, jednak liczy 2 razy, po dwóch kliknięciach klawisza F2, label1 mruga, zmienia liczby i gdy drugie naciśnięcie nastąpi po np. 30 sekundach, label 13 zmienia się na Zrobione po upłynięciu 5 minut od pierwszego naciśnięcia.
Przy okazji tego wątku, jak zrobić, aby program odczytywał naciśnięcie klawisza F2 nawet, jeśli nie jest na nim aktualnie focus? Czyli moja forma jest nawet zminimalizowana, ale po naciśnięciu F2 odliczanie się rozpoczyna.