473,408 Members | 2,027 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,408 software developers and data experts.

delegate

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
8 2439
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
3
by: Jeff S | last post by:
Please consider this sample code: It registers a delegate with an event. p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged); Now the following code removes the delegate:...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.