WebClient events do not fire

Posted by ignace.sys@kluwer.be on 24-Mar-2015 04:37

I want to download files. 

The file download works but the events  DownloadProgressChanged and DownloadFileCompleted are not fired.

I've rried the code in C#: works perfect.

What am i doing wrong ?

Thanks,

Ignace

/*************************************** Code ***************************************/

USING System.Net.*.
USING System.*.
USING System.ComponentModel.*.

CLASS DownloadFile:

DEF VAR Client AS WebClient.
DEF VAR w-File AS Uri.

METHOD PUBLIC VOID StartDownload(INPUT p-File AS CHARACTER,INPUT p-Target AS CHARACTER):
Client = NEW WebClient().
w-File = NEW Uri(p-File).

Client:DownloadProgressChanged:Subscribe(Client_DownloadProgressChanged).
Client:DownloadFileCompleted:Subscribe(Client_DownloadFileCompleted).
Client:DownloadFileAsync (w-File, p-Target).
END METHOD.

METHOD PRIVATE VOID Client_DownloadProgressChanged(sender AS System.Object, e AS DownloadProgressChangedEventArgs ) :
MESSAGE e:ProgressPercentage VIEW-AS ALERT-BOX INFO BUTTONS OK.
END METHOD.

METHOD PUBLIC VOID Client_DownloadFileCompleted(sender AS System.Object, e AS AsyncCompletedEventArgs ) :
MESSAGE "Download completed!" VIEW-AS ALERT-BOX INFO BUTTONS OK.
END METHOD.

END CLASS.


/*
private void btnDownload_Click(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
*/

All Replies

Posted by Peter Judge on 24-Mar-2015 07:36

Without trying this, it could very well be because of the async call.
 
-- peter
 
[collapse]
From: ignace.sys@kluwer.be [mailto:bounce-ignacesyskluwerbe@community.progress.com]
Sent: Tuesday, 24 March, 2015 05:38
To: TU.OE.Development@community.progress.com
Subject: [Technical Users - OE Development] WebClient events do not fire
 
Thread created by ignace.sys@kluwer.be

I want to download files. 

The file download works but the events  DownloadProgressChanged and DownloadFileCompleted are not fired.

I've rried the code in C#: works perfect.

What am i doing wrong ?

Thanks,

Ignace

/*************************************** Code ***************************************/

USING System.Net.*.
USING System.*.
USING System.ComponentModel.*.

CLASS DownloadFile:

DEF VAR Client AS WebClient.
DEF VAR w-File AS Uri.

METHOD PUBLIC VOID StartDownload(INPUT p-File AS CHARACTER,INPUT p-Target AS CHARACTER):
Client = NEW WebClient().
w-File = NEW Uri(p-File).

Client:DownloadProgressChanged:Subscribe(Client_DownloadProgressChanged).
Client:DownloadFileCompleted:Subscribe(Client_DownloadFileCompleted).
Client:DownloadFileAsync (w-File, p-Target).
END METHOD.

METHOD PRIVATE VOID Client_DownloadProgressChanged(sender AS System.Object, e AS DownloadProgressChangedEventArgs ) :
MESSAGE e:ProgressPercentage VIEW-AS ALERT-BOX INFO BUTTONS OK.
END METHOD.

METHOD PUBLIC VOID Client_DownloadFileCompleted(sender AS System.Object, e AS AsyncCompletedEventArgs ) :
MESSAGE "Download completed!" VIEW-AS ALERT-BOX INFO BUTTONS OK.
END METHOD.

END CLASS.


/*
private void btnDownload_Click(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}
*/

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by ignace.sys@kluwer.be on 24-Mar-2015 08:08

Hello Peter,

you have the Client:DownloadFileAsync and the Client:DownloadFile.

Only with Client:DownloadFileAsync one an use the events.

Meanwhile there's some progress.

i'v made a window with a button, on choose of the button i run a procedure.

Result : sometimes it works, sometimes the application chrashes.

code:

DEF VAR dl AS DownloadFile.

dl = NEW DownloadFile().

dl:StartDownload("somefile","c:\temp\2.zip").

Posted by Lieven De Foor on 24-Mar-2015 08:13

Hi Ignace,

Since OpenEdge is single-threaded, the async call won't work.

Have you tried the synchronous version (DownloadFile())?

It makes sense that only the async call has these events. When doing a synchronous call you don't need to receive an event when downloading has finished, you simply wait for the method to return.

The downside is you can't show any progress... (no pun intended).

Take care (en de groeten aan de collega's!)

Posted by ignace.sys@kluwer.be on 24-Mar-2015 08:26

Hi Lieven,

We use the async call because we want to show a progressbar...

And sometimes it works, sometimes it crashes...

Posted by Lieven De Foor on 24-Mar-2015 08:28

You'll never be able to reliably use asynchronous calls in OE when the runtime isn't supporting this.

The times it works are more a coincidence then not...

Posted by ignace.sys@kluwer.be on 24-Mar-2015 08:32

You' re right, sometimes it works is not enough.

Thanks.

Posted by Lieven De Foor on 24-Mar-2015 08:34

If you really want to use the WebClient DownloadFileAsync and show a progress bar, you could extract that code in an external c# project and call it as an executable. You'll have to do some inter process communication between your ABL application and the external exe, which won't be easy, but it can be done...

Posted by Peter Judge on 24-Mar-2015 08:34

It's explicitly not supported .
 
 
-- peter
 
[collapse]
From: ignace.sys@kluwer.be [mailto:bounce-ignacesyskluwerbe@community.progress.com]
Sent: Tuesday, 24 March, 2015 09:33
To: TU.OE.Development@community.progress.com
Subject: RE: [Technical Users - OE Development] WebClient events do not fire
 

You' re right, sometimes it works is not enough.

Thanks.

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

This thread is closed