Jump to content
  • Advertisement
Sign in to follow this  
zmg2b6

C# UI Threading

This topic is 3006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!