Last active
March 14, 2023 13:09
-
-
Save lansalot/0c6446d0c2a5fe06c86742c8b954e5b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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