xsharp.eu • Async question - Page 4
Page 4 of 5

Async question

Posted: Thu Mar 02, 2023 9:01 am
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

Async question

Posted: Thu Mar 02, 2023 9:19 am
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

Async question

Posted: Thu Mar 02, 2023 9:38 am
by HansjoergP
Hi Wolfgang,

why you don't use "oThread:Wait()"?

Async question

Posted: Thu Mar 02, 2023 9:44 am
by wriedmann
Hi Hansjörg,
why you don't use "oThread:Wait()"?
I don't have that method....

Wolfgang

Async question

Posted: Thu Mar 02, 2023 9:55 am
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

Async question

Posted: Thu Mar 02, 2023 9:59 am
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

Async question

Posted: Thu Mar 02, 2023 10:05 am
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

Async question

Posted: Thu Mar 02, 2023 10:29 am
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

Async question

Posted: Thu Mar 02, 2023 10:31 am
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

Async question

Posted: Thu Mar 02, 2023 10:49 am
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