Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

C# UI Threading

Started by zmg2b6 Jul 1, 2010 at 8:59 PM 3 replies 1.9k views
Original Post
zmg2b6
zmg2b6
I'm having some issues trying to implement a worker thread in the background of a C# Windows Forms app. The worker thread - I'm trying to just have it update a Panel control every couple milliseconds (depending on what i set for Thread.Sleep). The problem for me has been that when I keep the Thread.Sleep high (like 500 milliseconds) the UI is responsive and normal, but when I set the Thread.Sleep low (like at 10) to get more frames per second. then the UI gets totally unresponsive and freezes. So I must be doing something wrong!

So I'm looking for any tips, I'm not sure that I'm handling the UI correctly. The app is basically a Panel control that is rendered using OpenGL and is refreshed constantly by a background thread. So I have a panel (this is the OpenGL panel that is refreshed constantly) on the left side of my form, and then just a bunch of standard controls on the right side (TextBox, Label, etc)

Thanks!

public partial class Form1 : Form    {        private ulong frameCount = 0;        private delegate void VoidDelegate();        MyOpenGLPanel MyPanel = new MyOpenGLPanel();        public Form1()        {            InitializeComponent();            MyPanel.Dock = Dockstyle.Fill;            panel1.Controls.Add(MyPanel);            Thread t1 = new Thread(new ThreadStart(GoThread));            t1.IsBackground = true;            t1.Start();        }        private void GoThread()        {            while (true)            {                Thread.Sleep(500);                RefreshOpenGLPanel();            }        }        private void RefreshOpenGLPanel()        {            if (InvokeRequired)            {                BeginInvoke(new VoidDelegate(RefreshOpenGLPanel));            }            else            {                MyPanel.Refresh();                frameCount += 1;                TextBox_FrameCount.Text = frameCount.ToString();            }        }    }


[Edited by - zmg2b6 on July 2, 2010 6:17:55 AM]
jpetrie
jpetrie
When you sleep for less time, you're more likely to invoke to the UI thread more often; this probably contributes to the UI thread responsiveness issues you're having. Why are you updating the panel like this? It's not the most ideal way.
zmg2b6
zmg2b6
Well it's basically a terrain/map creator application and so the Panel that is refreshed constantly, I'm just using it to show a 3D representation of the map vertexes.

I guess I was envisioning something like Blender where you have a 3D view of your current mesh, and then you have just the regular Win Forms controls in the main app that are used to modify the mesh, something like that. Maybe I'm just refreshing the 3D view too much?
zmg2b6
zmg2b6
Now that I think about it you are correct, I don't really need to update it constantly like this. I changed it to just update on events like resize, or whatever other commands I issue that will alter the appearance, I think that will work good. Thanks!
jpetrie
jpetrie
That's a better idea. If you need real-time continuous update, the general approach is to P/Invoke PeekMessage and loop in it during an Application.Idle callback, updating until a message is available.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.