Return Type Flow

I have a method that returns a value bool. I want to execute this method with Thread.

Thread t1 = new Thread(new ThreadStart(doThis));

Could you suggest a way to get this return value?

+1
source share
7 answers

Ideally, use a parallel task library and Task<T>instead ... but otherwise, you need to set up some kind of shared variable to represent the result; when the thread is finished, read the result from any thread that you need.

, bool BeginInvoke , , IAsyncResult, .

+8

. , . , (), . . , , ( ).

public class ThreadedMethod<T>
{

    private T result;
    public T Result 
    {
        get { return result; }
        private set { result = value; }
    }

    public ThreadedMethod()
    {
    }

    //If supporting .net 3.5
    public void ExecuteMethod(Func<T> func)
    {
        Result = func.Invoke();
    }

    //If supporting only 2.0 use this and 
    //comment out the other overload
    public void ExecuteMethod(Delegate d)
    {
        Result = (T)d.DynamicInvoke();
    }
}

, ( ). lambdas:

ThreadedMethod<bool> threadedMethod = new ThreadedMethod<bool>();
Thread workerThread = new Thread((unused) => 
                            threadedMethod.ExecuteMethod(() => 
                                SomeMethod()));
workerThread.Start();
workerThread.Join();
if (threadedMethod.Result == false) 
{
    //do something about it...
}
+6

, ?

bool result = doThis();

:

Func<bool> handle = doThis;
handle.BeginInvoke(Callback, handle); // asynchronous invocation
// can do more work...

:

void Callback(IAsyncResult ar) {
  bool result = ((Func<bool>)ar.AsyncState).EndInvoke(ar);
  // ...
}
+5
 static void Main(string[] args)
    {
       bool returnValue = false;
       new Thread(
          () =>
          {
              returnValue =test() ; 
          }).Start();
        Console.WriteLine(returnValue);
        Console.ReadKey();
    }

    public static bool test()
    {
        return true;
    }
+1

-, , , . , bool , .

0

I would not suggest you have a delegate that returns some value. There is a better approach to getting some value from a method when it is executed with its execution, that is, using the "out" parameters.

In your case, you may have something like the code below:

public delegate void DoThisWithReturn(out bool returnValue);

public static void DoThisMethod(out bool returnValue)
{
    returnValue = true;
}

public static void Start()
{

    var delegateInstance = new DoThisWithReturn(DoThisMethod);
    bool returnValue;
    var asyncResult = delegateInstance.BeginInvoke(out returnValue, null, null);
    //Do Some Work.. 
    delegateInstance.EndInvoke(out returnValue, asyncResult);
    var valueRecievedWhenMethodDone = returnValue;
 }
0
source
public class ThreadExecuter<t> where T : class
{
    public delegate void CallBack (T returnValue);
    public delegate T Method();
    private CallBack callBack;
    private Method method;

    private Thread t;

    public ThreadExecuter(Method _method, CallBack _callBack)
    {
        this.method = _method;
        this.callBack = _callBack;

        t = new Thread(this.Process);
    }

    public void Start() 
    {
        t.Start();
    }

    public void Abort()
    {
        t.Abort();
        callBack(null);
    }

    public void Join()
    {
        t.Join();
    }

    private void Process()
    {
        callBack(method());
    }
}

using:

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            #region [ Paket Data]
             ...
            #endregion

            for (int i = 0; i < 20; i++)
            {
                Packet packet = new Packet() { Data = data, Host = "www.google.com.tr", Port = 80, Id = i };
                SocketManager sm = new SocketManager() { packet = packet };

               <b> ThreadExecuter<packet> te = new ThreadExecuter<packet>(sm.Send, writeScreen);
                te.Start();</packet></packet></b>
            }

            Console.WriteLine("bitti.");
            Console.ReadKey();
        }

        private static void writeScreen(Packet p)
        {
            Console.WriteLine(p.Id + " - " + p.Status.ToString());
        }
    }
}

Source: http://onerkaya.blogspot.com/2013/04/returning-value-from-thread-net.html

0
source

All Articles