Asynchronous problem

  • Thread starter Thread starter bernardpace
  • Start date Start date
B

bernardpace

Hi,

I am having the following situation. The snippet of algorithm shown is
being executed on a new thread.

while (....)
{
.....
.....

myDataGrid.DataSource = myDataTable;
myDataGrid.Refresh();

// Here I need to update some windows components, mainly a datagrid
// Updating it directly from the thread is giving me an error, shown
below
}


Error given: "
An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: Controls created on one thread cannot be
parented to a control on a different thread.
"
From previous reads I made, this is happeining due to the fact that UI
things should not be updated from workers thread but only from the UI
thread.

Now to solve the problem, I was thinking of make it asynchronously.


Can someone help me out to solve my problem.
Thanks in Advance
 
delegate void DoUpdate(DataTable tab);

while(...)
{
...
DoUpdate d = new DoUpdate(UpdateMethod);
myDataGrid.BeginInvoke(d, new object[]{myDataTable});
}

void UpdateMethod(DataTable tab)
{
myDataGrid.DataSource = myDataTable;
myDataGrid.Refresh();
}

If the loop needs to wait for the update to happen then use Invoke rather than BeginInvoke

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I am having the following situation. The snippet of algorithm shown is
being executed on a new thread.

while (....)
{
.....
.....

myDataGrid.DataSource = myDataTable;
myDataGrid.Refresh();

// Here I need to update some windows components, mainly a datagrid
// Updating it directly from the thread is giving me an error, shown
below
}


Error given: "
An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: Controls created on one thread cannot be
parented to a control on a different thread.
"
From previous reads I made, this is happeining due to the fact that UI
things should not be updated from workers thread but only from the UI
thread.

Now to solve the problem, I was thinking of make it asynchronously.


Can someone help me out to solve my problem.
Thanks in Advance


[microsoft.public.dotnet.languages.csharp]
 
Back
Top