Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion to use My.Application.DoEvents is irresponsible. #34917

Open
Darkicorn opened this issue Apr 7, 2023 · 9 comments
Open

Suggestion to use My.Application.DoEvents is irresponsible. #34917

Darkicorn opened this issue Apr 7, 2023 · 9 comments
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-visualbasic/svc help wanted Good for community contributors to help [up-for-grabs] Pri2

Comments

@Darkicorn
Copy link

It is irresponsible to show an example with My.Application.DoEvents - at the very least the note saying "don't use it" should come before the example. Preferably the example should be re-written to eliminate it. (It's an issue that's been going on for more than twenty years, so it might as well be fixed now.)


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

@KlausLoeffelmann
Copy link
Member

Hey @Darkicorn,

thanks for addressing this.
I am not sure what exact scenario you have in mind, but we might consider adding some additional info to Application.DoEvents (@merriemcgaw, @adegeo FYI)

A somewhat problematic scenario can occur, when there is a long-running task which runs on the UI thread, probably triggered by a typical WinForms ButtonClick event handler method (or the alike):

image

Now, this of course blocks the further processing of the UI-Thread. And that means after a couple of seconds, the App becomes unresponsive, and yes, that's bad. So, what folks often do to mitigate, is this:

image

And with that they are introducing a potential and unintended recursion. Users of the app may hit the button repeatedly. And with that, they are queuing in more the button click events, which are (indirectly) called from the still executing button click event handler method, and so on, and so on.

This is certainly an improvable design.

Things to mitigate that:

  • Disabling the button in this case at the beginning of the loop, so it cannot be engaged repeatedly.
  • Use a background worker for executing the long-running task.
  • Use async/await to handle long-running tasks asynchronously.

In all the cases, the UI controls would need to be guarded against multiple clicks.
We should consider putting those suggestions in the docs.

@Darkicorn, did you have other scenarios in mind?

Thanks,

Klaus

@BillWagner
Copy link
Member

Thanks for the added clarification @KlausLoeffelmann

I'll add this to our backlog as an enhancement. I'll also add the "help wanted" label in case anyone wants to submit a PR before we update it.

@BillWagner BillWagner added doc-enhancement Improve the current content [org][type][category] help wanted Good for community contributors to help [up-for-grabs] labels Apr 11, 2023
@dotnet-bot dotnet-bot removed ⌚ Not Triaged Not triaged labels Apr 11, 2023
@Adi9235
Copy link
Contributor

Adi9235 commented May 27, 2023

Hey @BillWagner can I work on this issue ? It would be great if you could elaborate on how to fix this issue.

@BillWagner
Copy link
Member

Hi @Adi9235

Thanks for volunteering to work on this. @KlausLoeffelmann 's comment #34917 (comment) provides the background to address this. The core problem is that Application.DoEvents() processes additional messages. Unless you take some mitigating actions (like disabling the button), a user can trigger repeated events for the same command.

@adegeo
Copy link
Contributor

adegeo commented Jun 5, 2023

I don't agree with

It is irresponsible to show an example with My.Application.DoEvents

But I do agree with

Preferably the example should be re-written to eliminate it

The example in the article isn't well written. And the headings seem odd. Example 1 doesn't actuallk contain an example. Further, there are details that should be in the main portion of the article, such as where you can declare events and use RaiseEvents statement.

Another problem is that it's written in the context of Windows Forms and not just in the context of VB the language. The UI stack doesn't matter when talking about the RaiseEvents statement. The example isn't useful to someone who is using WPF and has never touched WinForms. I would opt for an example that is just a simple console app.

@Adi9235
Copy link
Contributor

Adi9235 commented Jun 6, 2023

I will try to fix this issue and will make the PR soon

@KlausLoeffelmann
Copy link
Member

Definitely +1 to what @adegeo said. We should see though, that we add this warning where it actually belongs, so in the WinForms app space. But I guess it's not too much of a high priority.

@adegeo
Copy link
Contributor

adegeo commented Jun 6, 2023

Definitely +1 to what @adegeo said. We should see though, that we add this warning where it actually belongs, so in the WinForms app space. But I guess it's not too much of a high priority.

Yes, the WinForms docs should have something about DoEvents, the caveats, dangers, and proper scenarios.

Here is a good thread about it: https://stackoverflow.com/questions/11352301/how-to-use-doevents-without-being-evil

I created this issue to track it: dotnet/docs-desktop#1647

@testpushhydra
Copy link

I use DoEvents all the time. But having been a Windows C/C++ developer, I understand how message pumps work. To say its irresponsible is to say its irresponsible for a Marine to carry a gun. Yes, reentrancy is an issue, but know what your doing and its a helpful tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-visualbasic/svc help wanted Good for community contributors to help [up-for-grabs] Pri2
Projects
None yet
Development

No branches or pull requests

7 participants