Irij Posted December 25, 2016 Share Posted December 25, 2016 I am programming an application in C# where basically it creates a new thread to handle data; however, I'm not sure on how I can access calls from the new thread to the main thread? It errors when I try. I'm sure I must invoke something but I don't understand an easy way to do it? Link to comment Share on other sites More sharing options...
Oddly Posted December 25, 2016 Share Posted December 25, 2016 9 minutes ago, Irij said: I am programming an application in C# where basically it creates a new thread to handle data; however, I'm not sure on how I can access calls from the new thread to the main thread? It errors when I try. I'm sure I must invoke something but I don't understand an easy way to do it? Can you elaborate on what you mean by "access calls". Are you talking about passing variables from one thread to another? Link to comment Share on other sites More sharing options...
Irij Posted December 25, 2016 Author Share Posted December 25, 2016 I have created a separate thread that handles the networking part of the application. When data is sent to the server and then received in said network thread, it tries to call a method that resides in the main thread. But I cannot call that method from the network thread because the method im calling is not within the network thread. So how can I call a method that is outside of the thread? Link to comment Share on other sites More sharing options...
Irij Posted January 5, 2017 Author Share Posted January 5, 2017 Thought this was a programming forum.. No one must know. I'll look elsewhere. Good community here. Link to comment Share on other sites More sharing options...
Chronos Posted January 5, 2017 Share Posted January 5, 2017 You'll have to look into delegates and invokes I'm afraid. I'm not sure why you'd create a completely seperate thread where an Async method would likely work better. Honestly though, you have given us no information to go on. Is it a programatical main thread, is it a static winforms thread? If you want help the best thing you can do is generally provide a little example to get the ball rolling as honestly, your current case is so generic that I wouldn't even know where to begin.  But I'll begin somewhere, I'll just assume it's a winforms application, in which case something akin to this would be used. http://stackoverflow.com/a/6652100 WereAlpaca 1 Link to comment Share on other sites More sharing options...
Irij Posted January 6, 2017 Author Share Posted January 6, 2017 I'm using Lidgren. I have a network class that contains all the network code. The listening method needs to run on a loop and therefore I try to throw it on a separate thread so it doesn't halt the application because of the loop. How would I go about doing this in the same thread? I don't know how to run the loop while doing other things too. Link to comment Share on other sites More sharing options...
WereAlpaca Posted January 6, 2017 Share Posted January 6, 2017 1 hour ago, Irij said: I'm using Lidgren. I have a network class that contains all the network code. The listening method needs to run on a loop and therefore I try to throw it on a separate thread so it doesn't halt the application because of the loop. How would I go about doing this in the same thread? I don't know how to run the loop while doing other things too. "& at the end of the line backgrounds the process"  while /this/true; do   things and stuff done &  Quick google gave me this answer, might work for ya. I have basically no idea what half these words mean though. Link to comment Share on other sites More sharing options...
jcsnider Posted January 6, 2017 Share Posted January 6, 2017 @WereAlpaca that isn't valid C# code. I think that is for backgrounding a Linux process  @lrij: You cannot run a loop and do other things in the same thread. That is why threading exists. You can run the networking on one thread and the game logic on another. Communicating between threads can be done using delegates. I believe this guide may be of some help.  https://www.codeproject.com/Articles/624575/Delegate-Tutorial-for-Beginners  PS. This is a game dev site, not a programming site. This is a programming board but the number programmers here are low. (Only a couple who feel remotely confident working with multiple threads at a time). You'd likely have better luck at Stackoverflow or similar. Kibbelz and WereAlpaca 2 Link to comment Share on other sites More sharing options...
Robin Posted January 6, 2017 Share Posted January 6, 2017 9 hours ago, Irij said: I'm using Lidgren. I have a network class that contains all the network code. The listening method needs to run on a loop and therefore I try to throw it on a separate thread so it doesn't halt the application because of the loop. How would I go about doing this in the same thread? I don't know how to run the loop while doing other things too.  I think you're confused as to what the 'listening' part of your code is doing.  Lidgren is already multi-threaded under the hood. It handles all of the messy read head, piecing together, buffering etc. and leaves you with a cute little collection of packets to process at your leisure!  You do not have to do anything to allow this to happen.  What you're doing in your loop is polling the packet collection. You're asking lidgren if it has any new fully-formed packets for you to process.  You do not have to do a polling loop. This is just the de facto method due to most games already having a main loop.  If you do not already have a main loop, it might not be for you. Instead, you'd want something that compliments the winforms-style event loop instead.  You can see the different methods of consuming incoming packets in lidgren in their developer's wiki.  https://github.com/lidgren/lidgren-network-gen3/wiki/Receiving-Messages  If you do not understand when you should use threading you should not be trying to use threading. It is an advanced concept that is absolutely not required for the majority of projects, especially something like a game. Irij 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now