473,385 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Asynchronous call, but not really

Hello,

I'm working on an application that grabs some data from the web via
HttpWebRequest. I'm using a local objects method to get the data, but
the problem is that my form doesn't load until this method has
finished what it's doing.. which takes about 10-15 seconds. Here is
what I have:

Using System.Threading;

public class MyClass{
private MyObject mynewobject= new MyObject();
...
private delegate void GetDataDelegate(string url);

public MyClass(){
InitalizeComponent();
GetWebData(); // <- form isn't visible until after this
returns
this.lblStatus.Text = "finished";
}

private GetWebData(){
GetDataDelegate gdd = new
GetDataDelegate(mynewobject.RetrieveData);

// Initiate the asynchronous call
IAsyncResult ar = gdd.BeginInvoke("http://www.whatever.com/
file.txt", null, null);
Thread.Sleep(0);
this.lblStatus.Text = "getting data";
mld.EndInvoke(ar);
}
}

Again, what's happening is that the application starts, but I get no
visual cue that it has started, until after the GetWebData() call has
returned, sometimes it takes up to 30 seconds. I need the form and
components to be loaded first, but I'd also like to be pulling data as
soon as the program runs. What am I doing wrong? Thanks
Dec 7 '07 #1
5 1684
On Dec 7, 4:25 pm, tcomer <tco...@gmail.comwrote:

<snip>
Again, what's happening is that the application starts, but I get no
visual cue that it has started, until after the GetWebData() call has
returned, sometimes it takes up to 30 seconds. I need the form and
components to be loaded first, but I'd also like to be pulling data as
soon as the program runs. What am I doing wrong? Thanks
You're calling EndInvoke, which will wait until GetData returns. So
yes, you're doing things asynchronously - but you're not actually
achieving any benefits.

Instead of calling EndInvoke, specify a callback in the call to
BeginInvoke so that your UI thread will be free while the web request
is fetching data.

Jon
Dec 7 '07 #2
Well, technically, the form doesn't show until after the constructor is
called. It's just that what you do after the call to GetWebData is so quick
that it isn't perceptable (there are also some other calls that are made
under the covers).

As for what is causing your problem, you are calling the request
asynchronously, but you are then calling EndInvoke right after. EndInvoke
will block until the call is complete.

The call to Sleep doesn't do anything useful in this case, btw.

What you need to do is pass a delegate to the BeginInvoke method which
will get executed when the call is complete. Then call EndInvoke in that
method, and you will then be able to access your data.

You would have to make sure that if you are updating the UI though, you
are doing so by calling Invoke on the control, as your callback will not be
executed on the UI thread.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tcomer" <tc****@gmail.comwrote in message
news:30**********************************@i29g2000 prf.googlegroups.com...
Hello,

I'm working on an application that grabs some data from the web via
HttpWebRequest. I'm using a local objects method to get the data, but
the problem is that my form doesn't load until this method has
finished what it's doing.. which takes about 10-15 seconds. Here is
what I have:

Using System.Threading;

public class MyClass{
private MyObject mynewobject= new MyObject();
...
private delegate void GetDataDelegate(string url);

public MyClass(){
InitalizeComponent();
GetWebData(); // <- form isn't visible until after this
returns
this.lblStatus.Text = "finished";
}

private GetWebData(){
GetDataDelegate gdd = new
GetDataDelegate(mynewobject.RetrieveData);

// Initiate the asynchronous call
IAsyncResult ar = gdd.BeginInvoke("http://www.whatever.com/
file.txt", null, null);
Thread.Sleep(0);
this.lblStatus.Text = "getting data";
mld.EndInvoke(ar);
}
}

Again, what's happening is that the application starts, but I get no
visual cue that it has started, until after the GetWebData() call has
returned, sometimes it takes up to 30 seconds. I need the form and
components to be loaded first, but I'd also like to be pulling data as
soon as the program runs. What am I doing wrong? Thanks

Dec 7 '07 #3
You're calling EndInvoke, which will wait until GetData returns. So
yes, you're doing things asynchronously - but you're not actually
achieving any benefits.

Instead of calling EndInvoke, specify a callback in the call to
BeginInvoke so that your UI thread will be free while the web request
is fetching data.

Jon
That makes complete sense now that you pointed it out, funny how that
works :) Exactly what I was looking for, thanks a lot.

Dec 7 '07 #4
Thanks for the help.. I think I've got it figured out.

Although, I do have one other question. Hopefully someone could just
point me in the right direction.

Within the asynchronous call, I'd like to start one other thread that
will Start, process a chunk of data, update the datagrid and then
return to get another chunk of data(I'm not going thread-happy here,
trust me). Basically, when the data is being retrieved.. my datagrid
should be updating as the application receives the data. Could someone
please point me in the right direction? I'm a newbie when it comes to
threading.. sorta.
Dec 7 '07 #5
On Fri, 07 Dec 2007 10:50:54 -0800, tcomer <tc****@gmail.comwrote:
Within the asynchronous call, I'd like to start one other thread that
will Start, process a chunk of data, update the datagrid and then
return to get another chunk of data(I'm not going thread-happy here,
trust me). Basically, when the data is being retrieved.. my datagrid
should be updating as the application receives the data. Could someone
please point me in the right direction? I'm a newbie when it comes to
threading.. sorta.
As Nicholas says, you'll need to use Control.Invoke() to actually update
your user interface. Other than that, it's simply a matter of organizing
the processing logic so that it does things when you want it to.

On that topic, while you're not specific about how you're doing your i/o,
it's likely at some point you're using a built-in class that already has
an asynchronous API. If so, it'd be my recommendation that you forget
doing any threading yourself, whether explicitly starting a new thread or
using the thread pool via the Delegate.BeginInvoke() method. Instead, it
would be better to just write the data-handling class so that it's using
the asynchronous API on the i/o class you're using. You can code it to
call the "Begin..." to start retrieval of a "chunk of data", then when
that chunk completes you update your UI to reflect the data and start
another chunk, again with a call to "Begin...".

IMHO, this is much cleaner than creating a bunch of threads and managing
it yourself. This is especially true given the apparent flow of data
through your logic that you've described here.

Pete

Dec 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ... | last post by:
Does anyone know a good tutorial on asynchronous programming in .net AsyncCallback And IASyncResult are driving me crazy. And the msdn documentation is not really helpful on this topic I...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
0
by: Jonathan Trevor | last post by:
Hi, I've found what appears to be a bug with ASP.NET web service method invocation - making it impossible to invoke and get the result of a synchronous web call after an asynchronous call has...
3
by: usenetaccount | last post by:
In a newly created test app, to maximize client performance I tried to make two SOAP method calls in tandem (the soap methods execute some specified query), as each call includes a large amount of...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
0
by: r1 | last post by:
I am relatively inexperienced in using delegates and asynchronous methods. I read several articles, and then I developed my own code with a mission to improve the performance. Wow! I cannot...
1
by: dba123 | last post by:
I need to perform Asynchronous Inserts using DAAB. So far I have a method which does an insert but how can I do this Asyncronously so that it does not affect the load on our public production...
2
by: dba123 | last post by:
I need a way to do this insert below asynchronously and I'm pretty much lost at this point. Is there some example out there using Enterprise Library 2.0? We don't care about the return, it's...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.