This is a quite simple program in C#/VS2017/.Net Framework 4.7.1. There is one PictureBox and two Buttons in my Form.
The PictureBox displays an animated gif. The TextButton is bound to MyText property and the PlusButton can change MyText property. TextButton should update everytime when I pressed PlusButton.But in some condition ,it can't update.
When having All following conditions,the TextButton can't update by itself (sometimes it even becomes a white block):
- The program is compiled into
x64(x86 is always safe) - The gif displaying area is large enough (in my case,1872x1422 is safe and 2234x1416 isn't)
- The gif is animated fast enought (a static gif is always safe,a gif with 8ms per frame isn't)
Code is here:
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp2 { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } public class MyClass:INotifyPropertyChanged { public String MyText { get { return mytext; } set {mytext=value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyText")); } } public String mytext = "2"; public MyClass() {} public event PropertyChangedEventHandler PropertyChanged; } public class Form1 : Form { public MyClass my_class = new MyClass(); private Button TextButton = new Button(); private Button PlusButton = new Button(); private PictureBox PictureBox = new PictureBox(); public Form1() { SuspendLayout(); AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ClientSize = new System.Drawing.Size(3840, 2196); TextButton.Location = new System.Drawing.Point(10, 10); TextButton.Size = new System.Drawing.Size(100, 100); PlusButton.Location = new System.Drawing.Point(10, 110); PlusButton.Size = new System.Drawing.Size(100, 100); PictureBox.Location = new System.Drawing.Point(110, 10); PictureBox.Size = new System.Drawing.Size(3000, 2000); this.PictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.PictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; Controls.Add(this.PictureBox); Controls.Add(this.TextButton); Controls.Add(this.PlusButton); ResumeLayout(false); this.WindowState = FormWindowState.Maximized; PictureBox.Image = Image.FromFile("G:/1.gif"); TextButton.DataBindings.Add(new Binding("Text", my_class, "MyText")); PlusButton.Click += (object sender, EventArgs e) => { my_class.MyText += "1"; }; } } } Maybe it's because the UI is too busy to update the controls. Then I tried to modify the last line to:
PlusButton.Click += (object sender, EventArgs e) => { my_class.MyText += "1"; TextButton.Update(); }; Then everything works just fine.
Technically , I can solve this problem by adding a bunch of xxx.Update() everywhere the property changed. But the controls are usually not visible to codes which modify properties. If I do that anyway, my code would become a mass.
Also, I can't change my program to x86 because I need far more than 4GB memory.
Is there any good way to make the databindings or controls work normally in this situation?
https://stackoverflow.com/questions/66717950/all-controls-stuck-when-displaying-an-animated-gif-in-c-sharp March 20, 2021 at 11:28AM
没有评论:
发表评论