3

I am using CefSharp 3 in my WPF application to use Chromium based webBrowser control. In order to invoke some script in html ExecutScriptAsyncmethod is used. This is an EAP(Event-based Asynchronous Pattern) and i want to run this function synchronously as running it asynchronously is creating a huge mess in my output. As it is EAP I cannot await it. If I await it it throws an error saying Cannot await void

Please help..

Akansha
  • 933
  • 7
  • 18
  • You should be able use `TaskFactory.FromAsync` to get a `Task` you can await. – Lee Aug 04 '15 at 09:30
  • I think that can be done only for APM(Asynchronous Programming Model) – Akansha Aug 04 '15 at 09:39
  • Actually it's `Observable.FromEventPattern` in the reactive extensions library. You should be able to use `Observable.FromEventPattern(...).Take(1).ToTask()` to create a `Task` from the first value of an event. – Lee Aug 04 '15 at 11:19
  • See [A reusable pattern to convert event into task](http://stackoverflow.com/questions/22783741/a-reusable-pattern-to-convert-event-into-task). Note however if you want to do `task.Wait()`, you may end up with a classic deadlock, unless you host CefSharp on a separate thread. – noseratio Aug 04 '15 at 23:14

1 Answers1

1

The idea is to do this:

  1. Make the event handler signal an event
  2. Wait for the event

If you are using await then a TaskCompletionSource is a good "event". If you are going synchronous I'd probably still use that and simply Wait on the task that would be provided by the TCS.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Can you help me out with some sample? or just refer me the link of some nice tutorial that I can follow. – Akansha Aug 04 '15 at 09:46
  • I don't have anything handy. Any concrete problems? Is the idea clear? – usr Aug 04 '15 at 09:48
  • Okay I will try to go with the approach as suggested and get back to you if my issue continue. Thank you. :) – Akansha Aug 04 '15 at 09:53