Advertisement Jump to content
Sign in to follow this  

[.net] C#: main loop as seperate thread? (how?)

This topic is 3963 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 currently trying to get some gamecoding done in C#. My idea is to run the 'engine' (main loop) in a seperate thread, thus allowing the forms to update their fields and such while the engine is working. I'm having some issues though, when I call engine.Execute() the main loop is started, but in a single thread this of course prevents the forms from being updated. I tried starting the call in a seperate thread like this: // Called inside the form's constructor engineThread = new Thread( new ThreadStart( engine.Execute ) ); engineThread.Start(); However, somehow this doesn't seem to work. For each iteration in the main loop I log a message, which should be added to a listbox on a form, but it seems my main loop immediatly breaks down, which should be related to this seperate thread call. Any idea what the problem is here? Basically I want to run my main loop, while still updating my forms, and close the forms when the engine code breaks out of the main loop. I'd much rather stay away from multithreading, because I'm a noob at it and it'll probably give me a lot of headaches in the future, but if it's the only/best way....

Share this post

Link to post
Share on other sites
if (m_tbProgress.InvokeRequired)
SetTextCallback del = new SetTextCallback(AddMsgLine);
BeginInvoke(del, new Object[] { sLine + Crlf + Crlf });
else ...//method body if called within its own thread

That's what was required in my senior project to allow a thread to make changes to a text box. Well, there is more as you need to set up a delegate to handle the invoke, but that's the way that Microsoft tells you to handle it.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!