How to use the FRunnable?

I mean if I implement a thread class by FRunnable, how can I start up the thread? Should I call the Run() function or
do some other things? The code demo is as follows:

FSocketListenThread.h:

#pragma once

class CONNECTPC_API FSocketListenThread : public FRunnable {

    /** Singleton instance, can access the thread any time via static accessor, if it is active! */
    static  FSocketListenThread* Instance;

    /** Thread to run the worker FRunnable on */
    FRunnableThread* Thread;

    /** Stop this thread? Uses Thread Safe Counter */
    FThreadSafeCounter StopTaskCounter;

    virtual uint32 Run();
    virtual void Stop();
    // End FRunnable interface

    /** Makes sure this thread has stopped properly */
    void EnsureCompletion();


    /** Shuts down the thread. Static so it can easily be called from outside the thread context */
    static void Shutdown();

    static bool IsThreadFinished();


public:
    FSocketListenThread();
    ~FSocketListenThread();
};

Hi To run your thread you have to use

	Thread = FRunnableThread::Create(PointerToYourFSocketListenThreadInstance, TEXT("FSocketListenThreadRunnable - a name for thread dispatcher"), ThreadStackSize, TPri_AboveNormal);

This call gives you a WorkerThread handle which could be used to control your thread. You can Suspend, Kill or wait for completion of newly created thread.

Thread->WaitForCompletion();  //Waits till thread is finished

Run and Stop functions of FRunnable will be called outside. “Run” should process things you want to do in a separate thread. “Stop” called after “Run” ends or when you killing this thread. You can use single class to manage multithreading and process your data. To do this create a function in your class and run something like this:

bool StartListening()
{
    if (Thread != nullptr) return false; //Already running
    Thread = FRunnableThread::Create(this, TEXT("FSocketListenThread"), FSocketListenThread::ThreadStackSize, TPri_AboveNormal);
    return (Thread != nullptr)
}

void StopListening()
{
    if (Thread != nullptr) {
        // Ask your thread to stop somehow
        Thread->WaitForCompletion();
	    delete Thread;
        Thread = nullptr;
    }
}

Note that you dave to ‘delete’ thread handle if it was created successfully.

It works. Thank you very much!

How do you get the Thread stack size ThreadStackSize?

Hey Sys did you found out ?

How i can to call functions ang get result with runnable? i dont found about it

How to create a detached thread? I mean, the Thread can release itself when it is done.