|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Multithreading_Guide_1
{
public partial class frmExample1 : Form
{
private void btnProcess_Click(object sender, EventArgs e)
{
int nStart = Convert.ToInt32(txtStart.Text);
int nEnd = Convert.ToInt32(txtEnd.Text);
prgProcess.Minimum = nStart;
prgProcess.Maximum = nEnd;
for (int nValue = nStart; nValue <= nEnd; nValue++)
{
lblStatus.Text = "Processing item: " + Convert.ToString(nValue);
prgProcess.Value = nValue;
// Try uncommenting following line, the application
// would be responsive, as explained in article.
// Application.DoEvents();
}
}
delegate void DelegateType(int x);
DelegateType TheDelegate;
int StartFrom, EndTo;
private void btnStartThreaded_Click(object sender, EventArgs e)
{
TheDelegate = MessageHandler;
StartFrom = Convert.ToInt32(txtStart.Text);
EndTo = Convert.ToInt32(txtEnd.Text);
prgProcess.Minimum = StartFrom;
prgProcess.Maximum = EndTo;
btnStartThreaded.Enabled = false;
Thread MyThread = new Thread(ProcessRoutine);
MyThread.Start();
}
void MessageHandler(int nProgress)
{
lblStatus.Text = "Processing item: " + Convert.ToString(nProgress);
prgProcess.Value = nProgress;
}
void ProcessRoutine()
{
// Do processing here.
// We better make this method 'static'
// But we'll take those things later.
// Here we can use all member of class directly (since we have 'this')
for (int nValue = StartFrom; nValue <= EndTo; nValue++)
{
this.Invoke(this.TheDelegate, nValue);
}
}
public frmExample1()
{
InitializeComponent();
// TheDelegate = MessageHandler;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!
Touched COBOL and Quick Basic for a while.
Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.
I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!