csharBeginer
New member
- Joined
- Sep 1, 2018
- Messages
- 3
- Programming Experience
- Beginner
There is
- the main form is "Main.cs".
- form with "progressBar" - "progressBar.cs" Scenario:
User. The form is "Main.cs". Presses the "Start" button three times.
Program. Opens three forms of "progressBar.cs"
How to make the data from the progress percentage "progressBar" get in real time in the corresponding "lblStatus_1", "lblStatus_3", "lblStatus_3" form "" Main.cs "?
I tried to do it, but until the end I can not figure it out.
"Main.cs".
progressBar.cs
- the main form is "Main.cs".
- form with "progressBar" - "progressBar.cs" Scenario:
User. The form is "Main.cs". Presses the "Start" button three times.
Program. Opens three forms of "progressBar.cs"
How to make the data from the progress percentage "progressBar" get in real time in the corresponding "lblStatus_1", "lblStatus_3", "lblStatus_3" form "" Main.cs "?
I tried to do it, but until the end I can not figure it out.
"Main.cs".
C#:
namespace BackgroundWorkerSample
{
public partial class Main : Form
{
int numOfDisc; // Number of discoveries
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar prgrBar = new progressBar();
prgrBar.Show();
numOfDisc++;
switch (numOfDisc)
{
case 1:
prgrBar.m_oWorker.RunWorkerAsync();
lblStatus_1.Text = "Processing......" + prgrBar.prgrBar1Val.ToString() + "%";
break;
case 2:
prgrBar.m_oWorker.RunWorkerAsync();
lblStatus_2.Text = "Processing......" + prgrBar.prgrBar1Val.ToString() + "%";
break;
case 3:
prgrBar.m_oWorker.RunWorkerAsync();
lblStatus_3.Text = "Processing......" + prgrBar.prgrBar1Val.ToString() + "%";
break;
}
}
}
}
progressBar.cs
C#:
namespace BackgroundWorkerSample
{
public partial class progressBar : Form
{
/// <summary>
/// The backgroundworker object on which the time consuming operation shall be executed
/// </summary>
public BackgroundWorker m_oWorker;
public int prgrBar1Val;
public progressBar()
{
InitializeComponent();
m_oWorker = new BackgroundWorker();
m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
m_oWorker.WorkerReportsProgress = true;
m_oWorker.WorkerSupportsCancellation = true;
}
/// <summary>
/// On completed do the appropriate task
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//If it was cancelled midway
if (e.Cancelled)
{
lblStatus.Text = "Task Cancelled.";
}
else if (e.Error != null)
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
lblStatus.Text = "Task Completed...";
}
btnStartAsyncOperation.Enabled = true;
btnCancel.Enabled = false;
}
/// <summary>
/// Notification is performed here to the progress bar
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Here you play with the main UI thread
progressBar1.Value = e.ProgressPercentage;
// *** change ***
// progressBar1.Value
prgrBar1Val = progressBar1.Value;
// *** change. End ***
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
/// <summary>
/// Time consuming operations go here </br>
/// i.e. Database operations,Reporting
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
//NOTE : Never play with the UI thread here...
//time consuming operation
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
m_oWorker.ReportProgress(i);
//If cancel button was pressed while the execution is in progress
//Change the state from cancellation ---> cancel'ed
if (m_oWorker.CancellationPending)
{
e.Cancel = true;
m_oWorker.ReportProgress(0);
return;
}
}
//Report 100% completion on operation completed
m_oWorker.ReportProgress(100);
}
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
btnStartAsyncOperation.Enabled = false;
btnCancel.Enabled = true;
//Start the async operation here
m_oWorker.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (m_oWorker.IsBusy)
{
//Stop/Cancel the async operation here
m_oWorker.CancelAsync();
}
}
}
}