title: How set timeout for my long running time function?
body: <p>I have a function with some codes that maybe takes long time to complete. I want the function stopped from continuing if it takes time more than a specific time. How can I implement this? I tried below solution but it doesn't stop the execution.</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      let task = DispatchWorkItem {
      self.myfunction()
      }
  
  
      DispatchQueue.main.asyncAfter(deadline: .now() + 5.0, execute: {
          task.cancel()
      })
      task.perform()
  }
  
  func myfunction() -&gt; Void {
      /*some codes that may take long time to complete*/
  }
  </code></pre>