delegate

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Paul said:
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.
 
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,

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.ToString(), Destination.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 said:
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]
- (e-mail address removed)

Paul said:
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
 
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]
- (e-mail address removed)

Paul said:
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.ToString(), Destination.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 said:
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]
- (e-mail address removed)

Paul said:
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
 
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 said:
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]
- (e-mail address removed)

Paul said:
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.ToString(), Destination.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 said:
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]
- (e-mail address removed)

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

 
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]
- (e-mail address removed)

Paul said:
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 said:
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]
- (e-mail address removed)

Paul said:
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.ToString(), Destination.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



:

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]
- (e-mail address removed)

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

 
Thanks again Nicholas!

-P


Nicholas Paldino said:
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]
- (e-mail address removed)

Paul said:
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 said:
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]
- (e-mail address removed)

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.ToString(), Destination.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



:

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]
- (e-mail address removed)

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

 
Back
Top