For this kind of requirement you can use "System.Threading.Tasks.Task" class in which you can provide time out, check below link
Task Class[
^]
Below is sample code snippet I have tested in C# for replying your question.
Added second logic using thread, hope it will help
private void button1_Click(object sender, EventArgs e)
{
Func<object, object> func1 = new Func<object, object>(CallAction);
object obj = new object();
Task<object> task = new Task<object>(func1, obj);
task.Start();
task.Wait(30 * 1000);
object result;
if (task.Status == TaskStatus.RanToCompletion)
{
result = task.Result;
}
}
private object CallAction(object obj)
{
ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
object rtnObj = class1.fun1(obj);
return rtnObj;
}
object objReturn = null;
private void WorkerThreadMethod(object obj)
{
ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
objReturn = class1.fun1(obj);
}
private void button2_Click(object sender, EventArgs e)
{
ParameterizedThreadStart paraThreadStart = new ParameterizedThreadStart(WorkerThreadMethod);
Thread workerThread = new Thread(paraThreadStart);
workerThread.Start();
if (!workerThread.Join(new TimeSpan(0, 0, 30)))
{
workerThread.Abort();
}
else
{
object gotReturn = objReturn;
}
}