locked
put a method in an object, and then run it RRS feed

  • Question

  • User-1982615125 posted

    how do i do this:

    Dim t As Action = Sub()
    MsgBox("test")
    End Sub
    Task.Run(t)

    ---

    put a method in an object, and then run it.

    Monday, January 29, 2018 7:37 PM

All replies

  • User2103319870 posted

    You can make use of Action Delegate to achieve your requirement like below

    First Create a method like below in your page

     Public Sub DisplayMessage()
            MsgBox("test")
        End Sub

    now assign this method to Delegate and then call Run method in task to execute it

      Dim t As Action = New Action(AddressOf DisplayMessage)
    
            'Execute the method
            Task.Run(t)

    Monday, January 29, 2018 8:04 PM
  • User-1982615125 posted

    riiiight i forgot the new word..

    as new action(sub()

    msgbox("test")

    end sub)

    Monday, January 29, 2018 9:00 PM
  • User-1982615125 posted

    how to run it in the background and not wait for exit?

    Monday, January 29, 2018 9:05 PM
  • User-707554951 posted

    Hi fazioliamboina

    As a2h suggested, you could use Action delegate. Action delegate didn’t return a value.

    The usage of it as below:

     protected void Page_Load(object sender, EventArgs e)
            {
                Action<string> t = ShowWindowsMessage;
                // Execute the method
                t("message");
            }
              void ShowWindowsMessage(string message)
            {
                TextBox1.Text = message;
            }

    Output:

    For more details about this, please refer to the links below:

    http://www.tutorialsteacher.com/csharp/csharp-action-delegate

    Best regards

    Cathy

    Wednesday, January 31, 2018 6:35 AM