c# Task with arguments

like this it's working:

        public static OrderRS Method1()
        {

            OrderRS result = new OrderRS();

            return result;

        }

        static void BS()
        {

            Task<OrderRS> TheFirst = Task.Factory.StartNew<OrderRS>(Method1);

            Task TheSecond =TheFirst.ContinueWith(x=>
                {
                    OrderRS t = x.Result;
                    int r = t.OrderId;
                });
           
        }

Open in new window


But i need  to add arguments to Method1 like this:
        public static OrderRS Method1(int r1,Trs r2)
        {

            OrderRS result = new OrderRS();

            return result;

        }

        static void BS()
        {

            Task<OrderRS> TheFirst = Task.Factory.StartNew<OrderRS>(Method1(r1,Trs));

            Task TheSecond =TheFirst.ContinueWith(x=>
                {
                    OrderRS t = x.Result;
                    int r = t.OrderId;
                });

           
        }

Open in new window


How can i do it?
and how can i use the result from the second task?
center1010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Where is the type Trs defined, most likely the cause of the issue. Please post the error message you are getting.

public static OrderRS Method1(int r1,Trs r2)
0
käµfm³d 👽Commented:
The overloads of StartNew allow you to pass a state object to the method you are invoking. The only caveat is that you get one parameter, so in order to pass multiple parameters you will need to wrap them in a class/struct.

e.g.

public static OrderRS Method1(TaskParams parms)
{
    // Do something with parms.R1 and parms.R2
    
    OrderRS result = new OrderRS();

    return result;

}

static void BS()
{
    TaskParams parms = new TaskParams() { R1 = r1, R2 = Trs };
    Task<OrderRS> TheFirst = Task.Factory.StartNew<OrderRS>(Method1, parms);

    Task TheSecond =TheFirst.ContinueWith(x=>
        {
            OrderRS t = x.Result;
            int r = t.OrderId;
        });


}


...


public class TaskParams
{
    public int R1 { get; set; }
    public Trs R2 { get; set; }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.