I have a problem with timerTick() because it executes every 0.5 seconds, for me it's ok because he checks every 0.5 seconds for connectetCard(), now if so he should assign it The 5 seconds that I in _bw.DoWork assigned to run, how can I set timerTick() to fire again, but only when the process in backgroundWorker is completed.
This is my code:
public FormMenu()
{
InitializeComponent();
SelectDevice();
establishContext();
timerBatch.Start();
_bw = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
}
private void timerBatch_Tick(object sender, EventArgs e)
{
if (connectCard())
{
cardUID = getcardUID();
if (cardUID != "Error")
{
using (DBEntities db = new DBEntities())
{
try
{
personData = db.Persons.Where(x => x.BATCH_ID == cardUID).FirstOrDefault();
if (maDaten == null)
{
labelInfo.Text = "Batch invalid.";
return;
}
else
{
person = db.Person.Where(x => x.LPE_ID == personData.MA_ID).FirstOrDefault();
_bw.DoWork += _bw_DoWork;
_bw.ProgressChanged += bw_ProgressChanged;
_bw.RunWorkerCompleted += bw_RunWorkerCompleted;
_bw.RunWorkerAsync();
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
};
}
}
}
private void _bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 5; i >= 0; i -= 1)
{
if (_bw.CancellationPending) {
e.Cancel = true; return;
}
if (i == 0)
{
e.Cancel = true; return;
}
_bw.ReportProgress(i);
Thread.Sleep(1000);
}
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
labelInfo.Text = "Hello " + person.FirstName + " " + person.LastName + " " + e.ProgressPercentage.ToString();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
labelInfo.Text = labelInfo.Text.Trim();
}
Try something like this:
public FormMenu()
{
InitializeComponent();
SelectDevice();
establishContext();
timerBatch.Start();
_bw = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
}
//CHANGE:
bool backgroundworkerRunning = false;
private void timerBatch_Tick(object sender, EventArgs e)
{
//CHANGE:
if (backgroundworkerRunning)
return;
if (connectCard())
{
cardUID = getcardUID();
if (cardUID != "Error")
{
using (DBEntities db = new DBEntities())
{
try
{
personData = db.Persons.Where(x => x.BATCH_ID == cardUID).FirstOrDefault();
if (maDaten == null)
{
labelInfo.Text = "Batch invalid.";
return;
}
else
{
person = db.Person.Where(x => x.LPE_ID == personData.MA_ID).FirstOrDefault();
_bw.DoWork += _bw_DoWork;
_bw.ProgressChanged += bw_ProgressChanged;
_bw.RunWorkerCompleted += bw_RunWorkerCompleted;
//CHANGE:
backgroundworkerRunning = true;
_bw.RunWorkerAsync();
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
};
}
}
}
private void _bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 5; i >= 0; i -= 1)
{
if (_bw.CancellationPending) {
e.Cancel = true; return;
}
if (i == 0)
{
e.Cancel = true; return;
}
_bw.ReportProgress(i);
Thread.Sleep(1000);
}
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
labelInfo.Text = "Hello " + person.FirstName + " " + person.LastName + " " + e.ProgressPercentage.ToString();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//CHANGE:
backgroundworkerRunning = false;
labelInfo.Text = labelInfo.Text.Trim();
}