Skip to content

Instantly share code, notes, and snippets.

@lansalot
Last active March 14, 2023 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lansalot/0c6446d0c2a5fe06c86742c8b954e5b9 to your computer and use it in GitHub Desktop.
Save lansalot/0c6446d0c2a5fe06c86742c8b954e5b9 to your computer and use it in GitHub Desktop.
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.Threading;
namespace threads
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int val = 1;
Thread thread;
private void btnStart_Click(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(UpdatePB));
thread.Start();
}
private void UpdatePB()
{
while (true)
{
if (val >= 99) val = 1;
UpdateProgress(val++);
Thread.Sleep(50);
}
}
private void UpdateProgress(int val)
{
if (pbProgress.InvokeRequired)
{
pbProgress.Invoke(new Action(() => UpdateProgress(val)));
} else
{
pbProgress.Value = val;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
if (thread != null)
{
thread.Abort();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment