fbpx

Lambda expression question

More
10 months 6 days ago - 10 months 6 days ago #1 by Stefan Hirsch
Lambda expression question was created by Stefan Hirsch
Hi all,
ASYNC METHOD btnInstallServiceClick(sender AS Object , e AS EventArgs) AS VOID
    SELF:obtnInstallService:Enabled := FALSE
    AWAIT Task.Run(Action{ SELF , @InstallService() })
RETURN
PROTECT METHOD InstallService() AS VOID
    VAR helper := Helper{}
    helper:Install()
    SELF:CheckButtons()
RETURN

I would like to shorten this code to something like this:
ASYNC METHOD btnInstallServiceClick(sender AS Object , e AS EventArgs) AS VOID
    SELF:obtnInstallService:Enabled := FALSE
    AWAIT Task.Run( { () => VAR helper := Helper{}; helper:Install(); SELF:CheckButtons() } )
RETURN

Is that possible? If yes, how?

Regards,
Stefan
Last edit: 10 months 6 days ago by Stefan Hirsch.

Please Log in or Create an account to join the conversation.

More
10 months 6 days ago #2 by Nick Friend
Replied by Nick Friend on topic Lambda expression question
Hi Stefan,

I can't help you with the syntax I'm afraid, but just an observation.

I'm guessing that CheckButtons() sets the state of some buttons on your screen after the service has been installed. If so, it may be better to move it to after the await call in btnInstallServiceClick. That way CheckButtons is called on the UI thread once the async task has completed. It's generally better to avoid updating the UI thread from inside a task.

HTH

Nick

Please Log in or Create an account to join the conversation.

More
10 months 6 days ago - 10 months 6 days ago #3 by Chris Pyrgas
Replied by Chris Pyrgas on topic Lambda expression question
Hi Stefan,

Answering only about the syntax part (Nick has covered the rest!), first of all, you can also use a simpler syntax, instead of

AWAIT Task.Run(Action{ SELF , @InstallService() })

you can use

AWAIT Task.Run(Action{ InstallService })

(I know, the first one is auto created by XIDE)

and in order to put it all in all command, you can use
AWAIT Task.Run(Action{;
                        { =>  // lambda expression start
                           VAR helper := Helper{}
                           helper:Install()
                           SELF:CheckButtons()
                        };    // lambda expression end
                     })
It is a little tricky to use the correct syntax, but the rule is to not use ";" inside the lambda expression, use it to separate lines outside it only (lambda expression is the thing inside "{ => }".

Chris

XSharp Development Team
chris(at)xsharp.eu
Last edit: 10 months 6 days ago by Chris Pyrgas.

Please Log in or Create an account to join the conversation.

More
10 months 6 days ago #4 by Stefan Hirsch
Replied by Stefan Hirsch on topic Lambda expression question
Hi Chris,

thanks, it is compiling now. Syntax was the important part. Style is a matter of opinion.
But I will change this part. I will open a new thread to discuss the UI part.

Regards,
Stefan

Please Log in or Create an account to join the conversation.