By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,455 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,455 IT Pros & Developers. It's quick & easy.

delegate

P: n/a
Hi,

I have the following code in a winform:

....
private delegate void DownloadDelegate(string src, string dest);
....
private void DownloadFile(string src, string dest)
{
WebClient wc = new WebClient();
wc.DownloadFile(src, dest);
}
....
DownloadDelegate download = new DownloadDelegate(DownloadFile);
BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
"C:\\file.exe"});
....

I am using delegate so that it won't freeze my winform while I am doing
other stuff. The download process is good (downloading the files), but my
winform is still frozen, not until all download is done.

Anyone know why? Or... any better way to do this - downloading and updating
the winform in the same time?

Please advice, thanks!
-P
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Paul <Pa**@discussions.microsoft.com> wrote:
I have the following code in a winform:

...
private delegate void DownloadDelegate(string src, string dest);
...
private void DownloadFile(string src, string dest)
{
WebClient wc = new WebClient();
wc.DownloadFile(src, dest);
}
...
DownloadDelegate download = new DownloadDelegate(DownloadFile);
BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
"C:\\file.exe"});
...

I am using delegate so that it won't freeze my winform while I am doing
other stuff. The download process is good (downloading the files), but my
winform is still frozen, not until all download is done.

Anyone know why? Or... any better way to do this - downloading and updating
the winform in the same time?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
Paul,

You are calling BeginInvoke on the form, and not the delegate itself.
Your code should be as follows:

DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke("http://www.a.com/file.exe", "C:\\file.exe", null,
null);

When you call BeginInvoke on the form, you end up passing the delegate
to the UI thread to be called, which is why it is blocking.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
Hi,

I have the following code in a winform:

...
private delegate void DownloadDelegate(string src, string dest);
...
private void DownloadFile(string src, string dest)
{
WebClient wc = new WebClient();
wc.DownloadFile(src, dest);
}
...
DownloadDelegate download = new DownloadDelegate(DownloadFile);
BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
"C:\\file.exe"});
...

I am using delegate so that it won't freeze my winform while I am doing
other stuff. The download process is good (downloading the files), but my
winform is still frozen, not until all download is done.

Anyone know why? Or... any better way to do this - downloading and
updating
the winform in the same time?

Please advice, thanks!
-P

Nov 16 '05 #3

P: n/a
Nicholas,

Thanks for the reply. It didn't lock the winform anymore but strange thing
happened. download.BeginInvoke() sometimes called DownloadFile(), sometimes
don't. So, basically, I have a list of files to download and I create
download delegate in the for-loop for each file to download.

....
for(int i = 0; i < Source.Count; i++)
{
DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke(Source[i].ToString(), Destination[i].ToString()});
}
....

Is that because several "download.BeginInvoke()" are called and .NET
framework can't handle them all at once? BeginInvoke() should be asynch
process right? I mean... once called it just run by itself, no one is
waiting.

Any idea why?

Thanks,
-P

"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

You are calling BeginInvoke on the form, and not the delegate itself.
Your code should be as follows:

DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke("http://www.a.com/file.exe", "C:\\file.exe", null,
null);

When you call BeginInvoke on the form, you end up passing the delegate
to the UI thread to be called, which is why it is blocking.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
Hi,

I have the following code in a winform:

...
private delegate void DownloadDelegate(string src, string dest);
...
private void DownloadFile(string src, string dest)
{
WebClient wc = new WebClient();
wc.DownloadFile(src, dest);
}
...
DownloadDelegate download = new DownloadDelegate(DownloadFile);
BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
"C:\\file.exe"});
...

I am using delegate so that it won't freeze my winform while I am doing
other stuff. The download process is good (downloading the files), but my
winform is still frozen, not until all download is done.

Anyone know why? Or... any better way to do this - downloading and
updating
the winform in the same time?

Please advice, thanks!
-P


Nov 16 '05 #4

P: n/a
Paul,

If you do that, you end up kicking off Source.Count asynchronous
operations. These are serviced by the thread pool, which might be used by
other operations, and could be put in a waiting state.

You might want to consider passing the array of files to download to a
function, and then just downloading them one by one.

Also, if the files come from the same site, you might have a limit of 2
or 4 connections for that site, depending on the protocol (HTTP 1.0 or 1.1),
I believe.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
Nicholas,

Thanks for the reply. It didn't lock the winform anymore but strange
thing
happened. download.BeginInvoke() sometimes called DownloadFile(),
sometimes
don't. So, basically, I have a list of files to download and I create
download delegate in the for-loop for each file to download.

...
for(int i = 0; i < Source.Count; i++)
{
DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke(Source[i].ToString(), Destination[i].ToString()});
}
...

Is that because several "download.BeginInvoke()" are called and .NET
framework can't handle them all at once? BeginInvoke() should be asynch
process right? I mean... once called it just run by itself, no one is
waiting.

Any idea why?

Thanks,
-P

"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

You are calling BeginInvoke on the form, and not the delegate itself.
Your code should be as follows:

DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke("http://www.a.com/file.exe", "C:\\file.exe", null,
null);

When you call BeginInvoke on the form, you end up passing the
delegate
to the UI thread to be called, which is why it is blocking.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
> Hi,
>
> I have the following code in a winform:
>
> ...
> private delegate void DownloadDelegate(string src, string dest);
> ...
> private void DownloadFile(string src, string dest)
> {
> WebClient wc = new WebClient();
> wc.DownloadFile(src, dest);
> }
> ...
> DownloadDelegate download = new DownloadDelegate(DownloadFile);
> BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
> "C:\\file.exe"});
> ...
>
> I am using delegate so that it won't freeze my winform while I am doing
> other stuff. The download process is good (downloading the files), but
> my
> winform is still frozen, not until all download is done.
>
> Anyone know why? Or... any better way to do this - downloading and
> updating
> the winform in the same time?
>
> Please advice, thanks!
> -P


Nov 16 '05 #5

P: n/a
Nicholas,

Thanks again for your reply. I do as you suggested below. Another thing I
found in MSDN is to figure out if the asynch operation is done or not -
IAsyncResult

....
DownloadDelegate download = new DownloadDelegate(DownloadFile);
IAsyncResult aResult = download.BeginInvoke(null, null); // start download
asynch.
aResult.AsyncWaitHandle.WaitOne(); // wait for download completes
download.EndInvoke(aResult); // download all files are done
....

But now I am having the original problem - winform is locked (because of the
WaitOne()).

So, how do I wait and see if an async. operation is done and not locking UI
thread?

Thanks in advance,
-P
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

If you do that, you end up kicking off Source.Count asynchronous
operations. These are serviced by the thread pool, which might be used by
other operations, and could be put in a waiting state.

You might want to consider passing the array of files to download to a
function, and then just downloading them one by one.

Also, if the files come from the same site, you might have a limit of 2
or 4 connections for that site, depending on the protocol (HTTP 1.0 or 1.1),
I believe.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
Nicholas,

Thanks for the reply. It didn't lock the winform anymore but strange
thing
happened. download.BeginInvoke() sometimes called DownloadFile(),
sometimes
don't. So, basically, I have a list of files to download and I create
download delegate in the for-loop for each file to download.

...
for(int i = 0; i < Source.Count; i++)
{
DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke(Source[i].ToString(), Destination[i].ToString()});
}
...

Is that because several "download.BeginInvoke()" are called and .NET
framework can't handle them all at once? BeginInvoke() should be asynch
process right? I mean... once called it just run by itself, no one is
waiting.

Any idea why?

Thanks,
-P

"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

You are calling BeginInvoke on the form, and not the delegate itself.
Your code should be as follows:

DownloadDelegate download = new DownloadDelegate(DownloadFile);
download.BeginInvoke("http://www.a.com/file.exe", "C:\\file.exe", null,
null);

When you call BeginInvoke on the form, you end up passing the
delegate
to the UI thread to be called, which is why it is blocking.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
> Hi,
>
> I have the following code in a winform:
>
> ...
> private delegate void DownloadDelegate(string src, string dest);
> ...
> private void DownloadFile(string src, string dest)
> {
> WebClient wc = new WebClient();
> wc.DownloadFile(src, dest);
> }
> ...
> DownloadDelegate download = new DownloadDelegate(DownloadFile);
> BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
> "C:\\file.exe"});
> ...
>
> I am using delegate so that it won't freeze my winform while I am doing
> other stuff. The download process is good (downloading the files), but
> my
> winform is still frozen, not until all download is done.
>
> Anyone know why? Or... any better way to do this - downloading and
> updating
> the winform in the same time?
>
> Please advice, thanks!
> -P


Nov 16 '05 #6

P: n/a
Paul,

For the second null parameter, you pass a callback (AsyncCallback I
believe) which is called when the operation is complete. Then, in the
callback, you call EndInvoke to get the results. The thread that the
callback comes in on is the thread that did the work asynchronously, not the
calling thread, so be careful if you make UI calls from that thread.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
Nicholas,

Thanks again for your reply. I do as you suggested below. Another thing
I
found in MSDN is to figure out if the asynch operation is done or not -
IAsyncResult

...
DownloadDelegate download = new DownloadDelegate(DownloadFile);
IAsyncResult aResult = download.BeginInvoke(null, null); // start download
asynch.
aResult.AsyncWaitHandle.WaitOne(); // wait for download completes
download.EndInvoke(aResult); // download all files are done
...

But now I am having the original problem - winform is locked (because of
the
WaitOne()).

So, how do I wait and see if an async. operation is done and not locking
UI
thread?

Thanks in advance,
-P
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

If you do that, you end up kicking off Source.Count asynchronous
operations. These are serviced by the thread pool, which might be used
by
other operations, and could be put in a waiting state.

You might want to consider passing the array of files to download to
a
function, and then just downloading them one by one.

Also, if the files come from the same site, you might have a limit of
2
or 4 connections for that site, depending on the protocol (HTTP 1.0 or
1.1),
I believe.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
> Nicholas,
>
> Thanks for the reply. It didn't lock the winform anymore but strange
> thing
> happened. download.BeginInvoke() sometimes called DownloadFile(),
> sometimes
> don't. So, basically, I have a list of files to download and I create
> download delegate in the for-loop for each file to download.
>
> ...
> for(int i = 0; i < Source.Count; i++)
> {
> DownloadDelegate download = new DownloadDelegate(DownloadFile);
> download.BeginInvoke(Source[i].ToString(), Destination[i].ToString()});
> }
> ...
>
> Is that because several "download.BeginInvoke()" are called and .NET
> framework can't handle them all at once? BeginInvoke() should be
> asynch
> process right? I mean... once called it just run by itself, no one is
> waiting.
>
> Any idea why?
>
> Thanks,
> -P
>
>
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> Paul,
>>
>> You are calling BeginInvoke on the form, and not the delegate
>> itself.
>> Your code should be as follows:
>>
>> DownloadDelegate download = new DownloadDelegate(DownloadFile);
>> download.BeginInvoke("http://www.a.com/file.exe", "C:\\file.exe",
>> null,
>> null);
>>
>> When you call BeginInvoke on the form, you end up passing the
>> delegate
>> to the UI thread to be called, which is why it is blocking.
>>
>> Hope this helps.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - mv*@spam.guard.caspershouse.com
>>
>> "Paul" <Pa**@discussions.microsoft.com> wrote in message
>> news:34**********************************@microsof t.com...
>> > Hi,
>> >
>> > I have the following code in a winform:
>> >
>> > ...
>> > private delegate void DownloadDelegate(string src, string dest);
>> > ...
>> > private void DownloadFile(string src, string dest)
>> > {
>> > WebClient wc = new WebClient();
>> > wc.DownloadFile(src, dest);
>> > }
>> > ...
>> > DownloadDelegate download = new DownloadDelegate(DownloadFile);
>> > BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
>> > "C:\\file.exe"});
>> > ...
>> >
>> > I am using delegate so that it won't freeze my winform while I am
>> > doing
>> > other stuff. The download process is good (downloading the files),
>> > but
>> > my
>> > winform is still frozen, not until all download is done.
>> >
>> > Anyone know why? Or... any better way to do this - downloading and
>> > updating
>> > the winform in the same time?
>> >
>> > Please advice, thanks!
>> > -P
>>
>>
>>


Nov 16 '05 #7

P: n/a
Thanks again Nicholas!

-P
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

For the second null parameter, you pass a callback (AsyncCallback I
believe) which is called when the operation is complete. Then, in the
callback, you call EndInvoke to get the results. The thread that the
callback comes in on is the thread that did the work asynchronously, not the
calling thread, so be careful if you make UI calls from that thread.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
Nicholas,

Thanks again for your reply. I do as you suggested below. Another thing
I
found in MSDN is to figure out if the asynch operation is done or not -
IAsyncResult

...
DownloadDelegate download = new DownloadDelegate(DownloadFile);
IAsyncResult aResult = download.BeginInvoke(null, null); // start download
asynch.
aResult.AsyncWaitHandle.WaitOne(); // wait for download completes
download.EndInvoke(aResult); // download all files are done
...

But now I am having the original problem - winform is locked (because of
the
WaitOne()).

So, how do I wait and see if an async. operation is done and not locking
UI
thread?

Thanks in advance,
-P
"Nicholas Paldino [.NET/C# MVP]" wrote:
Paul,

If you do that, you end up kicking off Source.Count asynchronous
operations. These are serviced by the thread pool, which might be used
by
other operations, and could be put in a waiting state.

You might want to consider passing the array of files to download to
a
function, and then just downloading them one by one.

Also, if the files come from the same site, you might have a limit of
2
or 4 connections for that site, depending on the protocol (HTTP 1.0 or
1.1),
I believe.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
> Nicholas,
>
> Thanks for the reply. It didn't lock the winform anymore but strange
> thing
> happened. download.BeginInvoke() sometimes called DownloadFile(),
> sometimes
> don't. So, basically, I have a list of files to download and I create
> download delegate in the for-loop for each file to download.
>
> ...
> for(int i = 0; i < Source.Count; i++)
> {
> DownloadDelegate download = new DownloadDelegate(DownloadFile);
> download.BeginInvoke(Source[i].ToString(), Destination[i].ToString()});
> }
> ...
>
> Is that because several "download.BeginInvoke()" are called and .NET
> framework can't handle them all at once? BeginInvoke() should be
> asynch
> process right? I mean... once called it just run by itself, no one is
> waiting.
>
> Any idea why?
>
> Thanks,
> -P
>
>
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> Paul,
>>
>> You are calling BeginInvoke on the form, and not the delegate
>> itself.
>> Your code should be as follows:
>>
>> DownloadDelegate download = new DownloadDelegate(DownloadFile);
>> download.BeginInvoke("http://www.a.com/file.exe", "C:\\file.exe",
>> null,
>> null);
>>
>> When you call BeginInvoke on the form, you end up passing the
>> delegate
>> to the UI thread to be called, which is why it is blocking.
>>
>> Hope this helps.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - mv*@spam.guard.caspershouse.com
>>
>> "Paul" <Pa**@discussions.microsoft.com> wrote in message
>> news:34**********************************@microsof t.com...
>> > Hi,
>> >
>> > I have the following code in a winform:
>> >
>> > ...
>> > private delegate void DownloadDelegate(string src, string dest);
>> > ...
>> > private void DownloadFile(string src, string dest)
>> > {
>> > WebClient wc = new WebClient();
>> > wc.DownloadFile(src, dest);
>> > }
>> > ...
>> > DownloadDelegate download = new DownloadDelegate(DownloadFile);
>> > BeginInvoke(download, new object[] {"http://www.a.com/file.exe",
>> > "C:\\file.exe"});
>> > ...
>> >
>> > I am using delegate so that it won't freeze my winform while I am
>> > doing
>> > other stuff. The download process is good (downloading the files),
>> > but
>> > my
>> > winform is still frozen, not until all download is done.
>> >
>> > Anyone know why? Or... any better way to do this - downloading and
>> > updating
>> > the winform in the same time?
>> >
>> > Please advice, thanks!
>> > -P
>>
>>
>>


Nov 16 '05 #8

P: n/a
You should use the asynchronous delegate to perform asynchronous
operations.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.