I am using BackGround Worker for Loading data from DB(MS Access).
In my Form_Load i use:
bgw.RunWorkerAsync();
In my DoWork event I laod the data from DB
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
int iResult = OpenDB();
if (iResult != 0)
{
MessageBox.Show("Error in Opening DataBase", Constants.TITLE);
return ;
}
DataSet ds = GetAllUserInfo();
e.Result = ds;
}
And in My RunWorkerCompleted i assign data to DataGridView.
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dgUsers.Rows[0].Cells[0].Value = e.Result;
}
//Error Comes here after executing above code
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
Any help is appreciated.
Thanks.
Use SyncronizationContext mechanism for UI update . Example here http://www.codeproject.com/KB/threads/SynchronizationContext.aspx
1 .Have this as a global object :
Instantiate the object “synchronizationContext” on Form_Load event :
synchronizationContext = System.Threading.SynchronizationContext.Current;
Modify bgw_RonWorkerCompleted to :
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}