Sunday, 11 August 2013

Working on the form in `Worker.DoWork`

Working on the form in `Worker.DoWork`

I am working on an application that uses a BackgroundWorker to complete
some operations. I have a progress bar that is supposed to mark that my
workier is currently working. (This progressbar's normal status is hidden.
It should show itself when the worker starts working -here is my problem
and to hide itself when the worker finished working - I've already done
this)
How can I make my programm work?
I know you will ask me what I have aleady tried, and I have to show up
this block of code:
(here is some more code, in case this help)
public MainWindow(){
try
{
OleDbConnection databaseConnection = new
OleDbConnection(Properties.Settings.Default.AccountingServicesConnectionString);
databaseConnection.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = databaseConnection;
cmd.CommandText = "SELECT [Luna] FROM Cheltuieli";
OleDbDataReader DatabaseReader1 = cmd.ExecuteReader();
while (DatabaseReader1.Read())
{
this.periodSelector.Items.Add(DatabaseReader1["Luna"]);
}
databaseConnection.Close();
}
catch (OleDbException oex)
{
MessageBox.Show(oex.Message);
Application.Exit();
}
if (periodSelector.Items.Count > 0)
{
periodSelector.SelectedIndex = 0;
string Period = this.periodSelector.Text;
Period_Month = (Period.Split(' '))[0];
Period_Year = (Period.Split(' '))[1];
dataLoader.RunWorkerAsync();
}
}
// that takes about 3 seconds.
private void periodSelector_SelectionChangeCommitted(object sender,
EventArgs e)
{
dataLoadingProgress.Show(); // my progress bar
dataLoader.RunWorkerAsync(); // my worker
}
The code above doesn't show th progressbar, but the worker makes its job
correctly. What can I do?
EDIT: I have tried to put the dataLoadinProgress.Show(); in the DoWork()
event handler, but I get an exception: it sounds something like
Cross-thread operation not valid: Control 'dataLoadingProgress' accessed
from a thread other than the thread it was created on.

No comments:

Post a Comment