Async question

This forum is meant for questions and discussions about the X# language and tools
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Async question

Post by wriedmann »

Hi Nick,
thank you very much, I will try it.

But in the meantime I have tried something other and it seems to work:

Code: Select all

oThread := System.Threading.Thread{ { => self:_ConnectA( cHostName, cUserName, cPassword, nPort, oConfig ) } }
oThread:Start()
while oThread:IsAlive
	System.Threading.Thread.Sleep( 10 )
end
Using this code my debug output shows the right sequence:

Code: Select all

Creating AsyncFtpClient
Creating Task Connect()
Awaiting Task Connect()
Task Connect() finished
Waited 00:00:00.2349512
Connected is True
Is there anything that makes this code dangerous?

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Async question

Post by NickFriend »

Hi Wolfgang,

To be honest I'm not entirely sure if there's a problem with the way you're doing it.... the only thing that occurs to me is that your code has no real means of error trapping, which is covered by the AsyncHelper class. Also it seems a bit ugly to use Sleep!

I've been using the helper class for years now without issue (though of course by definition it's use is very limited in our app).

Nick
HansjoergP
Posts: 104
Joined: Mon Jul 25, 2016 3:58 pm
Location: Italy

Async question

Post by HansjoergP »

Hi Wolfgang,

why you don't use "oThread:Wait()"?
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Async question

Post by wriedmann »

Hi Hansjörg,
why you don't use "oThread:Wait()"?
I don't have that method....

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Async question

Post by wriedmann »

Hi Nick,
I'm now trying to translate that class to X#.
@Robert: I remember there was a discussion about how to translate the underscore to X#:

Code: Select all

synch.Post(async _ =>
            {
                try
                {
                    await task();
                }
                catch (Exception e)
                {
                    synch.InnerException = e;
                    throw;
                }
                finally
                {
                    synch.EndMessageLoop();
                }
            }, null);
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
HansjoergP
Posts: 104
Joined: Mon Jul 25, 2016 3:58 pm
Location: Italy

Async question

Post by HansjoergP »

Okay. I have seen now you use Thread instead of Task. Try using the Task.Run
https://learn.microsoft.com/de-de/dotne ... work-4.7.2
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Async question

Post by NickFriend »

Sorry, can't help you there... can't you just leave it in a class library to avoid the pain of translation?

Nick
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Async question

Post by wriedmann »

Hi Nick,
as temporary solution it works as separate library, but to deliver it I need to translate it to X#.
But the good thing: it seems to work very well and simplifies my code a lot.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Async question

Post by wriedmann »

Hi Hansjörg,
with Task.Run it does not works - the method does not permits that.
It works only with a Thread, with a Task it is not executed.
With Nicks class it works more elegantly.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3655
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Async question

Post by wriedmann »

Hi Robert or Chris,
how do I translate this code from C# to X#?

Code: Select all

Post(_ => done = true, null);
It is the method from this class:
https://learn.microsoft.com/en-us/dotne ... work-4.8.1
When I write

Code: Select all

self:Post( { => self:done := true }, null )
the compiler says

Code: Select all

error XS1593: Delegate 'System.Threading.SendOrPostCallback' does not take 0 arguments
Thank you very much for any hint!
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply