What is the best way to prevent developers from calling System.Windows.Forms.Application.DoEvents ()?

We only spent 300 man-hours fixing the buggy application in the field. It all came down to calling Application.DoEvents (re-login problem).

It didn’t fall into design reviews, code reviews. The code was inserted two years ago with the first version; the application has always been flaky, but recent changes have revealed more problems with re-entry.

This incident for the second time in our organization showed that Application.DoEvents caused crashes and multi-user debugging hours. It was discovered in this case by simply noticing a call hidden in a complex event handler for an asynchronous task.

What do you suggest preventing a recurrence of this problem:

  • Add control gate to original control?
  • Developer training?
  • Code analysis rules (why is this not a built-in rule yet?)

How to enforce coding practices?

+3
source share
8 answers

Each time an application is created centrally, run it on each assembly:

ildasm MyAssembly.exe /TEXT

Then search for:

System.Windows.Forms.Application::DoEvents

If it is found, mark the assembly as a failure, as if it were a compilation error.

+7
source

Maintain and add to development standards.

+4
source

. , - . , .

, , . FxCop .

: ? , ?

+3

- FxCop, API. FxCop , ,

+3

( BeginInvoke) .

+1

, , , , ( ):

ASSERT .

(MSQuant) DoEvents() . ASSERT .

, , , . , , (, ).

( ). . , , : , , , , .

, ASSERT ASSERT ( ) . (, ), ..

+1

" 300 -, - . Application.DoEvents( )".

, Application.DoEvents , x long = 0 long.maxvalue , .

, , , .

Private Sub foo()
    stpw.Reset() : stpw.Start()
    Do
    Loop While stpw.ElapsedMilliseconds < 1000
    stpw.Stop()
    Debug.WriteLine("foo")
End Sub
Dim stpw As New Stopwatch
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Debug.WriteLine("")
    Debug.WriteLine("but click")
    Dim t As New Threading.Thread(AddressOf foo)
    t.Start()
    Do
        Threading.Thread.Sleep(10)
        'Application.DoEvents() 'uncomment to change the behavior
    Loop While stpw.IsRunning
    Debug.WriteLine("but exit")
End Sub

So let's say that I originally wrote it without alleging a violation, but the user complained that the user interface was not responding. Therefore, I add DoEvents, but now I get odd results when users double-click on the button. Is the problem DoEvents or just a bad design. I would vote for poor design in this case.

-1
source

Will DoEvents fail to make this "good" code?

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    'simulate a long running task
    For x As Long = 1 To Long.MaxValue - 1
        'the absence of Application.DoEvents() is poor design IMHO
    Next
End Sub
-1
source

Source: https://habr.com/ru/post/1704788/


All Articles