Trouble with await and async in two seperate classes

Hello, I am evaluating Oxygene at the moment, so forgive me if this kind of a newbie type question. I’ve tried searching the Elements Wiki and it keeps changing for the “await” keyword, so I am not sure if this could be related.

Here is my problem :-
I have two classes that both require connection to the internet to download some data, so I use WebRequest to do this. Now one class has to do multiple WebRequests so I’ve built a loop around it. As I do not want to block the UI thread I place the method that includes the WebRequest into an async mode ie:-

var x := async MyWebRequestMethodThatReturnsAString;
data := Convert.ToString(await x);
//Do something with the data here

Now I have something in another class that does something similar but gets called afterwards. Unfortunately when I run this .NET crashes on the second one (somewhere on the async but it wont show me where). If I remove the second asnyc then it works fine. It also works fine I destroy the first class before I call the second class, but that can’t be right surely ?

So my question is, am I using the async / await keywords wrong here ?
To solve it I’ve called DownloadStringAsync on the first WebRequest and left the second with the async method.

I am so used to using Threads in Delphi (ie make a new class and descend from TThread object) but it seems that is not the best way to do things in .NET

I am looking into Tasks and reading up on them, but even the new C# CTP has the await / async calls in there, so I am sure that is the best way to go.

Any help or guidance on this matter would be greatly appreciated.

Regards
Anthoni

It would probably help if you posted some code

I am going to make an example showing the problem but do not know where to post it.
Do I just post the code here, or upload a project somewhere ?

You should be able to attach a zip of the project here

AsyncTest.zip (37.8 KB)

I think that is right (let me know if it isn’t

However, this seems to work perfectly in my test application, but not in my main application.
As I am currently learning Oxygene I am thinking that in my main application as I’ve been learning I’ve messed up some section of the code and not done that in my test.

Anyway, can you please have a look and let me know if I am doing things correctly ?

I think that’s ok. You might want to switch to httpclient rather than use webclient since its newer.

I tried using HTTPClient but for some reason it keeps giving me not recognized when I try and create one. So not sure what is going on there. I am compiling against .NET 4.5

Hello Anthoni,

What do you mean by not recognized HttpClient? Did you add the System.Net.Http reference to your project first?

Best regards.

Hi Victor,

What I meant was that I can not create an instance of HTTPClient like I thought I could.
For example I tried hc:HTTTPClient = new HTTPClient(url);

but that tells me that “Can not cast from string to HTTPWebRequest” so I tried this

but that says "Error    3    (E62) Type mismatch, cannot assign “System.Net.WebRequest” to “HttpWebRequest”

The only way I’ve found to create it was this

As you can probably tell, I am just learning all this so struggling a bit. I am guessing this is OK because it builds and runs OK.

Just found out about BackgroundWorker, so going to look into that and only use Async / Await when really, really requires it.

I don’t think there is a .create on httpclient

You also don’t need to use the type when you declare something

var client := new HttpClient();
            var getContentTask := client.GetAsync(Address)

Hi John,

That’s the one that I can not get.

Error    1    (E46) Unknown identifier "System.Web.HTTP"
Error    2    (E46) Unknown identifier "HttpClient"

Regards
Anthoni

I think your missing the namespace or assembly reference.

  1. If you right click on the project in solution explorer and select “manage nuget packages”. Select online in the dialog and search for httpclient.
  2. Install Microsoft Http Client Libraries.
  3. Paste the code and run a build
  4. Click on the error and it will ask if you want to add the System.Net.Http namespace. Do that

The code should now build

Yep, that works :smile:
It now recognises HttpClient

Do not know what but this is taking some wrapping around my brain to get sorted, but am getting there.
I’ve built a program in it already and it will not win any awards for design, but it works.

Couple of sideline questions if I may:
1] Is it best to use WPF (that’s what I am using at moment) rather than WinForms. WinForms reminds me a lot of the Delphi designer and is very friendly to use BUT I’ve read that WPF is the way to go really. Unfortunately XAML is taking some getting used to :expressionless:

2] When I distribute my Delphi apps I use Inno Setup, but now with .NET you need to make sure their framework is up to date etc. Is there a “common” way to distrubute desktop applications and if so what is it called so that I can go and google it

Regards
Anthoni

Hi Anthoni,

Yes I would go for WPF, It also has a designer but you can use the XAML editor directly to make changes, you will notice after a while that is is really great!
For example wrapping a couple of existing components in a container, just write the container around the components in XAML and you done, in Winforms you need to drag them and if you would choose to cut and paste them you loose all the event bindings.
Then lastly WPF components do not have a name unless you give them one, so less noise…
Also the winforms designer has it own set of problems (on big forms) like forgetting property values ect.

Concerning InnoSetup: We use it to distribute our .NET software, it’s great!
It can install the .net framework if needed.

If you need a build script that can build InnoSetup take a look at Train:

Best Regards,

Jeroen

Thanks will check it out.

I am pretty familiar with InnoSetup as that is what I have been using to distribute my Delphi apps, so if I can work out how to get it to also install the required .NET framework I will more than likely stick with that.

WPF is definately a strange beast to learn, that’s for sure. I can see that it will be very flexible once I’ve mastered it, but it has a very steep learning curve. I’ve just purchased
"Pro WPF 4.5 in C#" from Amazon. Even though it says C# according the reviews there is not much C# code to do and does take you right from the beginning so.

It might even prompt me to learn C# itself lol, as I’ve purchase the RO:Suite I also have RO:C# as well :smile:

Regards
Anthoni

FWIW, I was a Pascal user since “Turbo Pascal” all the way up to Delphi. I’ve switched to .Net and Oxygene and like it MUCH better. You can use all the .Net stuff and not have to worry. Sometimes Oxygene has features allowed by .Net even before C#. And RemObjects fixes bugs VERY quickly.

XAML is GREAT once you get the hang of it. I think once you realize that it isn’t a restricted tag language, but just an object creator, that can go a long way in not being confused about how to do things. Then it is just a matter of learning the properties and how to do binding. Also, I highly recommend using the MVVM pattern.

1 Like