Code:
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.Security;
using System.Security.Principal;
namespace WindowsFormsApplication0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.RunWorkerAsync();
}
public void Code()
{
MessageBox.Show("Code", "Code", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
try
{
//code
Code();
}
catch
{
//code
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
backgroundWorker1.CancelAsync();
}
}
}