Hi All,
I am using Async call to populate datagrid by following code.
This code I have written in a class which inherit in other class. This class has a virtual method which is overridden in derived class.
Problem I am facing is, datagrid is populated and displayed sometimes and not every time I load the page.I already checked the rowcount in table that I am assigning to grid. I am getting the proper row count.
I have declaired a delegate and using BegingInvoke and EndEnvoke method for it.
FYI, flgrd is an object of delegate.
private void CallAsyncMethod()
{
flgrd =
new FillGrid(processData);flgrd.BeginInvoke(
new AsyncCallback(this.callAsyncResultMethod), flgrd);}
private void callAsyncResultMethod(IAsyncResult ar){
flgrd = ((
FillGrid)(ar.AsyncState));flgrd.EndInvoke(ar);
}
public virtual void GetData(){
// User will write code in derived class to populate datagrid.
}
private void processData(){
this.GetData();}
AshishPosted Apr 18, 2008, 2:28 AM
Hi, Thanks for u r rlp
Actually my req'nt is bit different, let me explain u...
I am creating a class which willl have a virtual getdata() method that can be overridden in derived class. In base class I am creating a delegate for and using Begin and EndEnvoke method for Async call to DB.
here's my sample class:
public class Class1 : System.Web.UI.WebControls.WebParts.WebPart{
private delegate void FillGrid(); private FillGrid flgrd = null; protected override void OnLoad(EventArgs e){
base.OnLoad(e);CallAsyncMethod();
}
private void CallAsyncMethod(){
flgrd =
new FillGrid(processData);flgrd.BeginInvoke(
new AsyncCallback(this.callAsyncResultMethod), flgrd);}
private void callAsyncResultMethod(IAsyncResult ar){
flgrd = ((
FillGrid)(ar.AsyncState));flgrd.EndInvoke(ar);
}
protected virtual void GetData(){
}
private void processData(){
this.GetData();}
}
this is used to populate grid on web part, above one is my template which I will inherit in my other class and override the getdata() method.
Prob: Grid is not getting displayed every time I load the page.
pls help me.
Thanks in advance....
Matthew CochranPosted Apr 17, 2008, 7:13 PM
I didn't quite understand your architecture. Your async method should be the one retrieving data (which would be the long-running process). Try something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += (snd, arg) => CallAsyncMethod(); // wiring up the event
}
public delegate IEnumerable<String> GetData();
public delegate void ProcessResult(IAsyncResult result);
private void CallAsyncMethod()
{
GetData getter = LongRunningDataFetcher;
getter.BeginInvoke(callAsyncResultMethod, getter);
}
private void callAsyncResultMethod(IAsyncResult ar)
{
// make sure this is running on the main
// form's thread
if (this.InvokeRequired)
{
this.Invoke(new ProcessResult(callAsyncResultMethod), ar);
return; // don't execute on this thread
}
GetData getter = ar.AsyncState as GetData;
// test getter for null
IEnumerable<String> source = getter.EndInvoke(ar);
// test souce for null
dataGridView1.DataSource = source;
}
private IEnumerable<String> LongRunningDataFetcher()
{
// this would be the long-running process method...
// you can make this virtual and delete the implementation
// here in order to make a sub-class implement it.
return new String[]{
"one",
"two",
"three"
};
}
}