当鼠标离开Form的Boundaries时,程序会冻结 [英] Program freezes when mouse leaves the Form's Boundaries

查看:94
本文介绍了当鼠标离开Form的Boundaries时,程序会冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时冻结是暂时的,直到鼠标返回,有时是永久性的,有时根本不会冻结。看起来像是通过标题栏向上移动鼠标会更频繁地冻结。

Sometimes the freeze is temporary until the mouse returns, sometimes permanent, sometimes no freeze at all. Seems like moving the mouse up through the Title Bar freezes more often.

表单看起来像这样:


$


TextBoxDisplay - 上部文本框

BtnStopStart - 左键,测试="开始"

BtnCopy - 中键,文字="复制"

BtnTake - 右键,文字  ='取'&b> b
NUDHowmany - NumericUpDown,默认为8,最小8最大32

TextBoxResults - 下文本框

BGWGenerate - 后台工作者支持取消 - True,ReportsProgress = True

表格为480x120

TextBoxDisplay - upper Text Box
BtnStopStart - Left Button, test = "Start"
BtnCopy - Middle Button, text = "Copy"
BtnTake - Right Button, text  = "Take"
NUDHowmany - NumericUpDown, default is 8, min 8 max 32
TextBoxResults - lower Text Box
BGWGenerate - Background Worker Supports Cancellation - True, ReportsProgress = True
Form is 480x120

代码:

Option Strict On
Imports System.ComponentModel

Public Class Form1
    Private OnOff As Boolean = False
    Private Password As String = ""
    Private RNG As New Random
    Private HowMany As Integer

    Private Sub BtnStopStart_Click(sender As Object, e As EventArgs) Handles BtnStopStart.Click
        OnOff = Not OnOff
        If OnOff Then
            BtnStopStart.Text = "Stop"
            Password = ""
            BGWGenerate.RunWorkerAsync()
        Else
            BGWGenerate.CancelAsync()
            BtnStopStart.Text = "Start"
        End If
    End Sub

    Private Sub BGWGenerate_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGWGenerate.DoWork
        Do
            If BGWGenerate.CancellationPending Then Exit Sub
            If OnOff Then
                Do
                    If BGWGenerate.CancellationPending Then Exit Sub
                Loop Until Password = ""
                For Index As Integer = 1 To 64
                    If BGWGenerate.CancellationPending Then Exit Sub
                    Password &= Chr(RNG.Next(32, 127))
                Next
                BGWGenerate.ReportProgress(1)
            End If
        Loop
    End Sub

    Private Sub BGWGenerate_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGWGenerate.ProgressChanged
        TextBoxDisplay.Text = Password
        Password = ""
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        OnOff = False
        BGWGenerate.CancelAsync()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        HowMany = CInt(NUDHowmany.Value)
        OnOff = False
    End Sub

    Private Sub BtnCopy_Click(sender As System.Object, e As System.EventArgs) Handles BtnCopy.Click
        TextBoxResults.Text = TextBoxDisplay.Text
    End Sub

    Private Sub NUDHowmany_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NUDHowmany.ValueChanged
        HowMany = CInt(NUDHowmany.Value)
    End Sub

    Private Sub BtnTake_Click(sender As System.Object, e As System.EventArgs) Handles BtnTake.Click
        TextBoxResults.Text = TextBoxDisplay.Text.Substring(RNG.Next(0, TextBoxDisplay.TextLength - HowMany), HowMany)
    End Sub
End Class

我从未见过这样的事情。

I have never seen anything like this happen.

推荐答案

一个Do循环(在Do循环中)?

A Do loop (inside a Do loop)?

它没有重要的是它在另一个线程上 - 你的计算机永远被束缚,试图处理它。

It doesn't matter that it's on another thread - your computer is perpetually tied up trying to process that.

重构那个,看看这不是一个更好的解决方案。

Refactor that and see if that's not a better solution.


这篇关于当鼠标离开Form的Boundaries时,程序会冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆