0

Here is the below code.In this code i want dispatcher to get timeout after 2 second but it is not working.Please help me out.

   private void Button_Click(object sender, RoutedEventArgs e)
    {
        CancellationTokenSource tokenn = new CancellationTokenSource();
          Application.Current.Dispatcher.Invoke(DispatcherPriority.Send,TimeSpan.FromSeconds(1), (Action)MyMethod2);
        MessageBox.Show("Hello");
    }


    private void MyMethod2()
    {
        Uri uri = new Uri(@"D:\1.docx");
        webBrowse.Navigate(uri);
    }
  • please clarify what are you trying to achieve and what is going wrong. – Bizhan Jul 17 '18 at 16:47
  • I have some operation which must run on UI thread,but sometime it taken more than 10-15 second to complete,so i am trying to implement so kind of timeout. – user7190448 Jul 17 '18 at 16:49
  • I think you misunderstood the purpose of Dispatcher timeout. Your method2 needs to address cancellation which it naturally can't. please read https://binary-studio.com/2015/10/23/task-cancellation-in-c-and-things-you-should-know-about-it/ – Bizhan Jul 17 '18 at 17:08
  • Thanks @Bijan.But what should i do in this case, webBrowse.Navigate() crashes my application sometime, i cannot move it on other thread because i get cross thread exception.So it must be on the UI thread.Please suggest something – user7190448 Jul 17 '18 at 17:16
  • I'm not an expert on this issue. But I'm sure there are plenty of tips and tricks you'll find. https://stackoverflow.com/q/4269800/366064 – Bizhan Jul 17 '18 at 17:25
  • 1
    "I have some operation which must run on UI thread" This is literally never a true statement. There may be manipulation that occur in your operation that must run on the UI thread, which can be handled. – Paul Swetz Jul 17 '18 at 17:59
  • @Paul- webbrowser.navigate is taking only string and this function only crashing my application.So I can't do anything as I can't modify the code written inside webbrowser.navigate.Do you have any solution – user7190448 Jul 17 '18 at 18:12

0 Answers0