Callback function problem

G

Guest

Hi Dear MVP's and Everyone,

I am writing a program that gets some information from a web service using a
callback function. When the callback function receives the necessary
information, it updates the items in the listview first removing the item to
be updated from the list
at the specified index and then inserting it at the top of the list. Unless
the number of updates to be received is zero, the callback function calls the
BeginXXX web method iteratively. The problem occurs when the program tries to
update the last
item. The execution freezes at the step where the last item is removed from
the list. The same thing happens when the item to be updated is the first
item in the list. However there won't be any problem when the item is updated
without being removed or a new item is added. I applied the CF SP3 but this
time I receive WebException when the program tries to send some information
to the web service. I am using Asus MyPal PDA and I extracted the
netcf.all.wce4.arm4.cab to apply the service pack. Inserting a dummy item at
the end of the list won't work. Locking the ListView control while doing the
process won't work, either. The code of the callback function is like the
following:

private void WsGeriÇağrı(IAsyncResult ar)
{
int i;

kahveWR.Siparişİçeriği sipariş; /* the update information received from
the web service */

gws=(kahveWR.KahveWebService)ar.AsyncState;

sipariş=(kahveWR.Siparişİçeriği)gws.EndGetOrder(ar);

i=ListedekiYeri(sipariş.Ürün,1); /* the index of the item to be updated in
the list is found here */

ListViewItem madde=new ListViewItem(sipariş.Ürün);

madde.SubItems.Add(sipariÅŸ.Adet.ToString());

madde.SubItems.Add(this.listView1.Items.SubItems[2].Text);

madde.ImageIndex=2;

this.listView1.Items.RemoveAt(i); /* this is the line where execution
freezes */

this.listView1.Items.Insert(0,madde);

siparişSayısı--; /* variable holding the number of items to be updated */

label1.Text=siparişSayısı.ToString();

if (siparişSayısı>0)

geriSonuç=ws.BeginGetOrder("Melike Öztürk",geriÇağrı,ws); /* calling the
web method again */
else

servisÇağrıldı=false;
}

I don't have any idea what the solution might be. Do you have friends?
 
S

Sergey Bogdanov

Since the method "WsGeriÇağrı" will be invoked in non-UI thread you
can't modify listView1 and all other controls in this thread. To invoke
code in the UI thread use Control.Invoke method hence you should
redesign WsGeriÇağrı in this manner:

....
sipariş=(kahveWR.Siparişİçeriği)gws.EndGetOrder(ar);
Invoke(new EventHandler(SafeUpdateUI));
....


private void SafeUpdateUI(object sender, EventArgs e)
{
i=ListedekiYeri(sipariş.Ürün,1); /* the index of the item to be updated in
the list is found here */

ListViewItem madde=new ListViewItem(sipariş.Ürün);

madde.SubItems.Add(sipariÅŸ.Adet.ToString());

madde.SubItems.Add(this.listView1.Items.SubItems[2].Text);

madde.ImageIndex=2;

...
}



--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


OÄŸuzhan Benli said:
Hi Dear MVP's and Everyone,

I am writing a program that gets some information from a web service using a
callback function. When the callback function receives the necessary
information, it updates the items in the listview first removing the item to
be updated from the list
at the specified index and then inserting it at the top of the list. Unless
the number of updates to be received is zero, the callback function calls the
BeginXXX web method iteratively. The problem occurs when the program tries to
update the last
item. The execution freezes at the step where the last item is removed from
the list. The same thing happens when the item to be updated is the first
item in the list. However there won't be any problem when the item is updated
without being removed or a new item is added. I applied the CF SP3 but this
time I receive WebException when the program tries to send some information
to the web service. I am using Asus MyPal PDA and I extracted the
netcf.all.wce4.arm4.cab to apply the service pack. Inserting a dummy item at
the end of the list won't work. Locking the ListView control while doing the
process won't work, either. The code of the callback function is like the
following:

private void WsGeriÇağrı(IAsyncResult ar)
{
int i;

kahveWR.Siparişİçeriği sipariş; /* the update information received from
the web service */

gws=(kahveWR.KahveWebService)ar.AsyncState;

sipariş=(kahveWR.Siparişİçeriği)gws.EndGetOrder(ar);

i=ListedekiYeri(sipariş.Ürün,1); /* the index of the item to be updated in
the list is found here */

ListViewItem madde=new ListViewItem(sipariş.Ürün);

madde.SubItems.Add(sipariÅŸ.Adet.ToString());

madde.SubItems.Add(this.listView1.Items.SubItems[2].Text);

madde.ImageIndex=2;

this.listView1.Items.RemoveAt(i); /* this is the line where execution
freezes */

this.listView1.Items.Insert(0,madde);

siparişSayısı--; /* variable holding the number of items to be updated */

label1.Text=siparişSayısı.ToString();

if (siparişSayısı>0)

geriSonuç=ws.BeginGetOrder("Melike Öztürk",geriÇağrı,ws); /* calling the
web method again */
else

servisÇağrıldı=false;
}

I don't have any idea what the solution might be. Do you have friends?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top