Simple Async DataBind Example?

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I have a C# WinForm that makes a remote ADO.NET call which "freezes"
the form. I know I want to get the data access off the UI thread, but
can't find a very simple example.

Can anyone post a small/simple C# example that shows how to do async
data acces? In my form, the form has an object with its own dataset
property, and different form actions call this.object.dodata(), so I
want to do that async.

Thanks.
 
Here's your sample.
public class functionality{public delegate void AdoNETDelegate() ;public
void DirtyAdoNETcall()
{
// Processing Logic
}
}

public class caller
{
void main()
{
AsyncCallback callback = new AsyncCallBack(new EventHandler(OnWorkDone)
;
AdoNetDelegate delly = new AdoNetDelegate(new
EventHandler(objectname.DirtyAdonetcall) ;
del.BeginInvoke(callback,delly) ;
}

OnWorkDone
{
}
}

You could obviously instead of AsyncCallBack use threads.

- Sahil Malik
Independent Consultant
You can reach me thru my blog at -
http://www.dotnetjunkies.com/weblog/sahilmalik/
 
Hi,

Just to append Sahil's sample,
In DataBinding we may use async delegation to retrieve data, however we
could not change the DataSet in that thread, since the databinding
mechanism will fire the events on the bounded controls and let them
updates, however if the change is made on another thread, the updates code
will also be run on that threas which will cause "freeze" problem, Windows
Form or Control methods cannot be called on a thread other than the one
that created the form or control.

Here is a modified version of the sample:
<code>
public class MyForm : Form
{
public delegate void AdoNETDelegate() ;
public delegate void UpdateDelegate(DataSet deltaDS);

public void RetrieveAndProcessData()
{
//Not in the UI thread.

//create a temporary dataset /datatable etc to process the data
//so that we will not mess the databinding events.
DataSet deltaDS = new DataSet();

// Your Processing Logic

//Executes a delegate asynchronously on the thread that
//the control's underlying handle was created on.
this.BeginInvoke(new UpdateDelegate(OnWorkDone),new object[]{deltaDS});
}

public void Button1_Clicked(object sender, EventArgs e)
{
MethodInvoker delly = new MethodInvoker(this.RetrieveAndProcessData);
delly.BeginInvoke(null,null);
}

private void OnWorkDone(DataSet deltaDS)
{
//OK, we are in the right thread context.
//do Merge or some other operations on the bounded dataset
}
}
</code>

Does it resolve your problem?
If you have anything unclear please feel free to post it in this thread.

Have a nice day!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Back
Top