Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can a method with return type as list be called from a thread

I have a method, shown below, which calls a service.

How can I run this method through thread?

public List<AccessDetails> GetAccessListOfMirror(string mirrorId,string server)
{
    List<AccessDetails> accessOfMirror = new List<AccessDetails>();
    string loginUserId = SessionManager.Session.Current.LoggedInUserName;
    string userPassword = SessionManager.Session.Current.Password;

    using (Service1Client client = new Service1Client())
    {
        client.Open();
        accessOfMirror = client.GetMirrorList1(mirrorId, server, null);
    }

    return accessOfMirror;
}
like image 814
smv Avatar asked May 01 '13 13:05

smv


People also ask

How do you return a list from a Thread in Java?

Other scenerio is to use Callable<OBJECT YOU WANT TO RETURN> and use a Thread/ExecutorService to start your task. Then (in control in your outer object) you can use Callable . get() to get your result.

Is it possible to return something from a Thread in Java?

This is an example of how to return the value of a Thread. The steps of the example are described in short: We have implemented two classes, RetDouble and RetInt that both implement the Callable, the first using as parameter a Double and the second one using as parameter an Integer .

How does Thread run method return value?

Thread class, define the instance variable in the constructor and set it to None, then override the run() function with custom code and set the instance variable. The main thread will then access the return value.

How do you call a method in a Thread?

Java Thread run() methodThe run() method of thread class is called if the thread was constructed using a separate Runnable object otherwise this method does nothing and returns. When the run() method calls, the code specified in the run() method is executed. You can call the run() method multiple times.


2 Answers

In C# 3.5 or 4.0 you can do this.

var task = Task.Factory.StartNew<List<AccessDetails>>(() => GetAccessListOfMirror(mirrorId,server))
.ContinueWith(tsk => ProcessResult(tsk));

private void ProcessResult(Task task)
{
    var result = task.Result;
}

In C# 4.5 there's the await/async keywords which is some sugar for above

public async Task<List<AccessDetails>> GetAccessListOfMirror(string mirrorId,string server)

var myResult = await GetAccessListOfMirror(mirrorId, server)
like image 197
cgotberg Avatar answered Sep 22 '22 18:09

cgotberg


Try something like this:

public async Task<List<AccessDetails>> GetAccessListOfMirror(string mirrorId, string server)
    {
        List<AccessDetails> accessOfMirror = new List<AccessDetails>();
        string loginUserId = SessionManager.Session.Current.LoggedInUserName;
        string userPassword = SessionManager.Session.Current.Password;


        using (Service1Client client = new Service1Client())
        {
            client.Open();
            Task<List<AccessDetails>> Detail = client.GetMirrorList1(mirrorId, server, null);
            accessOfMirror = await Detail;

        }


        return accessOfMirror;
    }
like image 36
KrishnaDhungana Avatar answered Sep 21 '22 18:09

KrishnaDhungana