fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Lambda expression question

Lambda expression question 1 year 3 months ago #4288

  SHirsch's Avatar Topic Author SHirsch Offline Posts: 110
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

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

Last edit: by SHirsch.

Lambda expression question 1 year 3 months ago #4289

  NickFriend's Avatar NickFriend Offline Posts: 193
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.

Lambda expression question 1 year 3 months ago #4291

  Chris's Avatar Chris Offline Posts: 1179
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

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

Last edit: by Chris.

Lambda expression question 1 year 3 months ago #4294

  SHirsch's Avatar Topic Author SHirsch Offline Posts: 110
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.

  • Page:
  • 1