Webview2 does not complete download (VS2019, C#)

Jiri Novak 0 Reputation points

I have a hidden download dialog in my application. Webview2 sometimes (irregularly) does not complete the download of a small (40-70kb) Pdf file - its final renaming does not occur. The application seems to be in a waiting state, but it is not stopping nor not sleeping. The file remains in the Download folder in the form <<..guid..>>.tmp. If I close the app, this tmp file is automatically deleted. If I copy the tmp file and rename it to xx.pdf, this file is regular and the pdf reader opens it.

I tried turning off my anti virus software but it didn't help.

The problem appears in all plants all customers, on diferent webpages, so it is not a matter of one PC or webserver, but of the webview2 component.

Where can the problem be? Why won't the download finish?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,015 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 41,766 Reputation points

    I don't use this approach so I cannot answer to the actual problem but one thing that stands out to me is that you're responding to the event that the download has started and you're hooking up to get notified of state changes. But you are in a race condition at this point. It is quite possible the download has already completed before you even hook up the event handler. This is always a risk when trying to handle events after a process has started. In this case I don't see that you have any other choice since you need the download to start in order to get to the event you need to handle. But I'd recommend that after you hook up the state change handler that you then check the current state of the download. If it is already completed then you won't get the state change notification (because it happened before you hooked it up) so you need to do your completion logic.

    Hook up event handler for state change
    Check to see if download is already completed

    Note that you are still in a race condition here. It is possible that the download is in progress when you hook up the handler, it completes and your event handler gets called and then you check the state. You need to handle the case where you are trying to "complete" the download when you've already completed it. The inverse also applies. If you were to check the state first and then hook up the state change handler then it is possible for it to complete in between these two calls.