Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- DoCount();
- }
- public void DoCount()
- {
- for (int i = 0; i < 100; i++)
- {
- objTextBox.Text = i.ToString();
- Thread.Sleep(100);
- }
- }
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker backgroundWorker = (BackgroundWorker)sender;
- for (int i = 0; i < 100; i++)
- {
- backgroundWorker.ReportProgress(i);
- Thread.Sleep(100);
- }
- }
- private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- textBox1.Text = e.ProgressPercentage.ToString();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- backgroundWorker1.RunWorkerAsync();
- }
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- DoCount();
- }
- public void DoCount()
- {
- Thread t = new Thread(new ThreadStart(delegate
- {
- for (int i = 0; i < 100; i++)
- {
- this.Invoke((Action) delegate { objTextBox.Text = i.ToString(); });
- Thread.Sleep(1000);
- }
- }));
- t.IsBackground = true;
- t.Start();
- }
- }
- public partial class Form1 : Form
- {
- private SynchronizationContext c;
- private Thread t;
- private EventWaitHandle pause =
- new EventWaitHandle(false, EventResetMode.ManualReset);
- public Form1()
- {
- this.InitializeComponent();
- this.c = SynchronizationContext.Current;
- }
- private void Form1Activated(object sender, EventArgs e)
- {
- this.t = new Thread(new ThreadStart(delegate
- {
- this.pause.Reset();
- while (this.t.IsAlive && !this.pause.WaitOne(1000))
- {
- this.c.Post(
- state => this.label1.Text = DateTime.Now.ToString(),
- null);
- }
- }));
- this.t.IsBackground = true;
- this.t.Start();
- }
- private void Form1Deactivate(object sender, EventArgs e)
- {
- this.pause.Set();
- this.t.Join();
- }
- /// <summary>
- /// Button1s the click.
- /// </summary>
- /// <param name="sender">The sender.</param>
- /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
- private void Button1Click(object sender, EventArgs e)
- {
- this.Close();
- }
- }
- this.Update();
- if (this.textBox1.InvokeRequired)
- {
- SetTextCallback d = new SetTextCallback(SetText);
- this.Invoke(d, new object[] { text });
- }
- using System.ComponentModel;
- using System.Threading;
- using System.Windows.Forms;
- namespace tester
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- if (!backgroundWorker1.IsBusy)
- backgroundWorker1.RunWorkerAsync();
- }
- /// <summary>
- /// This delegate enables asynchronous calls for setting the text property on a control.
- /// </summary>
- delegate void SetTextCallback(string status);
- private void BackgroundWorker1DoWork(object sender, DoWorkEventArgs e)
- {
- for (var i = 0; i < 100; i++)
- {
- backgroundWorker1.ReportProgress(i);
- Thread.Sleep(100);
- }
- }
- private void BackgroundWorker1ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- if (label1.InvokeRequired)
- Invoke(new SetTextCallback(SetLabelText), new object[] { e.ProgressPercentage.ToString()});
- else
- SetLabelText(e.ProgressPercentage.ToString());
- }
- private void SetLabelText(string text)
- {
- label1.Text = text;
- }
- }
- }
- for (int i = 0; i < 100; i++)
- {
- objTextBox.Text = i.ToString();
- Application.DoEvents();
- Thread.Sleep(100);
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Action countUp = this.CountUp;
- countUp.BeginInvoke(null, null);
- }
- private void CountUp()
- {
- for (int i = 0; i < 100; i++)
- {
- this.Invoke(new Action<string>(UpdateTextBox), new object[] { i.ToString() });
- Thread.Sleep(100);
- }
- }
- private void UpdateTextBox(string text)
- {
- this.textBox1.Text = text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement