Skip to main content
added 972 characters in body; deleted 40 characters in body
Source Link
triazotan
  • 2.2k
  • 1
  • 23
  • 32

You can create a new thread running your method, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Obviously, would not abort the method (probably no way to do that directly), but the thread -- but it seems that would accomplish what you desired. Please indicate if you need more details on

A simple threading samplecode example probably would go something like below. Note I made some assumptions about signature with parameter, all this in some class, etc. For method:

 public string DoSomethingComplex(int param) 

and then in the same class:

public delegate void Callback(string newFoo); public void ThreadedDoSomethingComplexEnded(string newFoo) { foo = newFoo; } private static void ThreadedDoSomethingComplex(ClassFooIsIn fooObject, int param, Callback callback) { var newFoo = fooObject.DoSomethingComplex(param); callback(newFoo); } 

usage aka code in some other method in ClassFooIsIn:

var thread = new Thread(() => ThreadedDoSomethingComplex(this, param, ThreadedDoSomethingComplexEnded)); thread.Start(); if (!thread.Join(20000)) { foo = ""; } 

Foo should be initialized before above shown usage, so in reality you could possibly skip foo = ""; line.

You can create a new thread running your method, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Obviously, would not abort the method (probably no way to do that directly), but the thread -- but it seems that would accomplish what you desired. Please indicate if you need more details on threading sample.

You can create a new thread running your method, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Obviously, would not abort the method (probably no way to do that directly), but the thread -- but it seems that would accomplish what you desired.

A simple threading code example probably would go something like below. Note I made some assumptions about signature with parameter, all this in some class, etc. For method:

 public string DoSomethingComplex(int param) 

and then in the same class:

public delegate void Callback(string newFoo); public void ThreadedDoSomethingComplexEnded(string newFoo) { foo = newFoo; } private static void ThreadedDoSomethingComplex(ClassFooIsIn fooObject, int param, Callback callback) { var newFoo = fooObject.DoSomethingComplex(param); callback(newFoo); } 

usage aka code in some other method in ClassFooIsIn:

var thread = new Thread(() => ThreadedDoSomethingComplex(this, param, ThreadedDoSomethingComplexEnded)); thread.Start(); if (!thread.Join(20000)) { foo = ""; } 

Foo should be initialized before above shown usage, so in reality you could possibly skip foo = ""; line.

added 157 characters in body; added 16 characters in body
Source Link
triazotan
  • 2.2k
  • 1
  • 23
  • 32

You can use create a new thread running your method, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Obviously, would not abort the method (probably no way to do that directly), but the thread -- but it seems that would accomplish what you desired. Please indicate if you need more details on threading sample.

You can use create a new thread, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Please indicate if you need more details on threading.

You can create a new thread running your method, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Obviously, would not abort the method (probably no way to do that directly), but the thread -- but it seems that would accomplish what you desired. Please indicate if you need more details on threading sample.

Source Link
triazotan
  • 2.2k
  • 1
  • 23
  • 32

You can use create a new thread, then:

thread.Start(); bool success = thread.Join(20000); 

Join returns true if thread finished successfully before designated time. Please indicate if you need more details on threading.