XNA game collected by GC after running update - c#

I'm working on writing a splash screen that returns a game mode (int) and an IP address (string). The idea is the splash screen runs, takes user input and then runs the main game with these options. I'm using a thread to achieve this - the thread polls for an exit request from the splash screen, then pulls the values out to program.cs and calls exit() on splash.
The main game runs on it's own with no issues but with the splash screen enabled the game runs just 1 frame and seems to be disposed of by garbage collection after running the update method. (returns a DisposedObjectException or something of the sort if trying to reference it) After a bit of debugging I've found the issue is with the exit command. Code is as follows:
using System;
using System.Threading;
namespace SplashScreen
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
int choice = 0;
string ip = "";
bool runSplash = true;
bool useThreading = true;
bool ignoreThreadResponse = false;
// Debug option, toggle running splash screen
if (runSplash == true)
{
bool splashrunning = true;
using (Splash1 splash = new Splash1())
{
if (useThreading)
{
// Run a thread to poll whether the splash screen has requested an exit every 0.5 seconds
Thread t = new Thread(() =>
{
while (splashrunning)
{
// If splash requests exit pull gameMode choice and IP Address before killing it, then quit this thread
if (splash.requestingExit)
{
choice = splash.choice;
ip = splash.ip;
// The offending piece of code, without this you can simply select an option, force close and second part runs fine
//splash.Exit();
splashrunning = false;
}
Thread.Sleep(500);
}
});
t.Start();
}
splash.Run();
}
}
// If splash screen is not running, assign default values
if(!useThreading || !runSplash || ignoreThreadResponse)
{
choice = 2;
ip = "127.0.0.1";
}
if (choice != 0)
{
// This game is picked up by garbage collection after running Update once
using (Game1 game = new Game1(choice, ip))
{
game.Run();
}
}
}
}
}
When splash.Exit() is called, it causes game1 to be collected after the first update. If I disable threading it works fine. If I exit using the X at the top right it works fine. Whether or not I ignore the thread response the game fails to run if threading is enabled and I call splash.Exit().
What I'm looking for is any of the following:
A reason why the second game is being collected.
An alternative way to exit a game or call the 'close window' (big red x) function.
A better way of implementing this.
I've used console input to do this in the past, but I want to move on to using a graphical interface instead of an ugly command prompt for the user.
Edit:
Turns out I was almost there. While GSM is probably the correct way of doing things, for anyone who just wants to grab the code from the question and throw caution to the wind you simply need to add a thread to run the second game.
I'm pretty sure this isn't ideal, but it's a lot less tweaking in my case.
Thread gt = new Thread(() =>
{
using (Game1 game = new Game1(choice, ip))
{
game.Run();
}
});
gt.Start();
So while I'd recommend anyone starting from scratch to use GSM, this might be a quick solution for someone else just trying to get it running.

What does your Splash class look like? Exit is a method of the Game class, and exits the game. Is your Splash class inheriting Game? If so, it shouldn't.
Edit:
After reading the bottom half of your post a bit more clearly - you should only have a single class that inherits from Game, and it should run your game. If you want to display a splash screen it should be a custom class, or look into the Game State Management sample.

Related

Constantly check if a process is open

I want to constantly check if a background process is running or not, I only have one label (to display the result)
My code that shows whether it's running or not is:
Process[] plist = Process.GetProcessesByName("chrome");
if (plist.Length > 0)
{
label1.ForeColor = Color.Green;
label1.Text = "FOUND";
}
else
{
label1.ForeColor = Color.Red;
label1.Text = "NOT FOUND";
}
And when run it, it works, but when I close the process it still shows "FOUND", how can I make it always check if plist.Lenght is 0 or >0?
p.s: I tried some duplicate questions etc and I didn't get it to work.
using System;
using System.Diagnostics;
namespace Program
{
class Program
{
static void Main(string[] args)
{
Process[] plist = Process.GetProcessesByName("msedge");
if (plist.Length > 0)
{
Console.WriteLine(plist[0].ProcessName);
}
else
{
Console.WriteLine("NotFound");
}
}
}
}
The code above seems to be working just fine. Two things I would check are:
open up task manager
ensure all chrome processes are closed, even if your chrome web browser is closed, there might be some open here. I used msedge, and it seemed to work just fine.
You may have hard looped your UI thread. This means it is so busy checking processes, that it has no time to update your label. normally you do not do tasks like this on the UI thread, but instead break them off on side threads. Alternatively you can use a timer on the main thread, check every couple of miliseconds. The last but Bad way to do this is to call:
label1.Invalidate();
Application.DoEvents();
These lines will force the UI to update and pause your process check, but is really only good for testing / development. As this will cause all of your processes to lag, not a good thing for production enviroments.

Stopping SoundPlayer from a different thread while it's playing

For the sake of making a (shuffled) playlist, I've made a separate thread in which I load and play each song in the playlist. The background stuff (wav files, file paths, playlists and shuffling) all work without a hitch.
The issue is that I have 2 windows, each of which can close and open the other. Each window has a different playlist, and when I switch to the other window, I want my static SoundPlayer to stop playing, then start playing the new playlist.
This currently isn't working: currently, the application waits until the current track is finished before displaying the next window and starting the other playlist. Yes, the entire application waits on this.
I'm new to thread coding, so I'm not really sure what to do. The two methods of stopping this I've tried so far have been SoundPlayer.Stop() and Thread.Abort(). Neither changes the situation at all.
In each window:
Thread playlistThread;
public Window()
{
InitializeComponent();
MusicPlayer.music.Stop();
playlistThread = new Thread(() => MusicPlayer.PlayPlaylist(MusicPlayer.ShufflePlaylist(MusicPlayer.PlaylistFromType("[insert track type]"), random)));
playlistThread.Start();
PlayPlaylist which I will show next takes a List of strings, so don't worry about the Thread line, it's just a few sections put into one. The properties after that simply generate that list, and again, that all works, but I can show it if anyone thinks it's necessary. Here is the PlayPlaylist method:
public static void PlayPlaylist(List<string> tracks)
{
for (int i = 0; i < tracks.Count; i++)
{
music.SoundLocation = tracks[i];
music.PlaySync();
}
}
Here's the answer I worked out:
public static void PlayTrack(List<string> tracks, int i)
{
while (true)
{
if (i == tracks.Count)
{
tracks = MusicPlayer.ShufflePlaylist(tracks, MusicPlayer.random);
i = 0;
}
music.SoundLocation = tracks[i];
int l = SoundInfo.GetSoundLength(tracks[i]);
music.Play();
while (l > 0)
{
Thread.Sleep(1000);
l -= 1000;
}
i++;
}
}
The SoundInfo class with its GetSoundLength method I found here.
The reason this method works while others do not is because of how Play() and PlaySync() work. PlaySync() plays the entire .wav file in the current thread, with nothing else running until it finishes. Thus, even SoundPlayer.Stop() and Thread.Abort() do not work, as they insert a new line after the current one.
By running this method in a new thread, you avoid PlaySync() giving you that issue. However, it will still be impossible to stop the track ahead of time using PlaySync(). This is why you use Play() instead.
Therein lies a second issue, however: Play() plays the track in its own thread, meaning the rest of the code will continue. This is a big risk if you're wanting to do anything only after the current track finishes.
The answer is to calculate the length of the track you're going to play. Then simply create a while loop, running until l (given by GetSoundLength()) reaches 0. In each pass through the loop the thread (separate from your window's main thread) sleeps for 1 second. This is fine on the CPU and means that every second extra code, such as SoundPlayer.Stop(), can be injected into the thread.

A thread for updating a RichTextBox.Text twice a second is not working

First of all - I'm very low skilled programmer. I am building the foundation of a simple music app for my bachelor degree project. My question is regarding a internal clock method that is meant to increase an int value by 1 BPM times a minute.
I've created an internalClock class:
public class internalClock
{
// THIS METHOD WILL BE CALLED WHEN THE THREAD IS STARTED
public static void clockMethod()
{
int BPM = 135;
int clockTick = 1;
Form1 clockForm = new Form1();
// infinite loop
while (true)
{
if (clockTick == 8)
{
clockTick = 1;
}
else
{
clockTick++;
}
clockForm.metrobox.Text = clockTick.ToString();
Thread.Sleep(60 * 1000 / BPM);
}
}
}
This is how I managed to get an access to the RichTextBox itself:
public RichTextBox metrobox
{
get { return metroBox; }
set { metroBox = value; }
}
In the main 'Program.cs' I've written what's meant to start a separate thread to run the clockMethod until the program is closed:
// THREADING
// Create a thread
internalClock oClock = new internalClock();
Thread oClockThread = new Thread(new ThreadStart(internalClock.clockMethod));
// Start the internalClock thread
oClockThread.Start();
It's not updating the text in the RichTextBox. Also, if I call the clockMethod() without creating a separate thread for it - the application freezes. Sorry for my amateur question, I'm just getting started with C# (yeah.. my uni is useless). What am I doing wrong in my code?
So the above code has several problems, however I would encourage you to check out the Timer control that you can add to the form you want to do processing at a certain interval or in certain "ticks". MSDN Form Timer
With the timer you can remove that class you have and invoking a new thread, etc etc. I would read up on the Timer class in the given link and think about how you can re-work your application structure to fit that. The concepts for why that thread isn't working, etc, is frankly not that important for where you're at. I think you just need to focus for now on a tool that already does what you want it to do, which I believe is the Timer.
As a general note, you usually don't need to create a raw thread in .NET. As of .NET 4.0 you can access types called Tasks to perform multi-threaded logic and processing. If you find the need to do that later on, check that out. Task Type MSDN

Displaying a form locks up mono

I've got a mono app written in c# and executed on a Mac using "mono myapp.exe"
The app itself is a "Windows Application" when viewed from the project properties, but it doesn't always show a window. In program.cs, there is a static Main:
static void Main(string[] args) {
UserClient client = new UserClient();
client.Start(args);
}
public class UserClient {
public void Start(string[] args) {
// Talk to server, listen for instructions, etc.
....
// Launch the "Stay Alive" thread
// Just a thread that Sleeps/Loops watching for an exit command; mainly used to keep the process alive
}
}
Inside the UserClient's Start method, there is a piece of code that continuously monitors a server which gives it instructions to do things. One of the things it does is optionally displays a message using a windows form.
When the server instructs the process to display a message, it instantiates a form, displays it using frm.ShowDialog() and then after 30 seconds, a timer on the form runs Close() and the frm then gets disposed. However, when this happens, on my Mac I see an application title bar saying "mono" and a new icon on my dock bar for the mono app. After about 2 minutes the mono process in Activity Monitor shows "Not Responding." This eventually will prevent the user from logging out, shutting down, etc. (because Mac OS can't kill mono gracefully).
ON THE OTHER HAND... if the server never tells the process to display that form, everything runs fine and dandy: a dock icon never shows up (which is good!), mono title bar never shows up and the mono process continues to run happily, not preventing the system from shutting down or rebooting.
Anyone experienced this or have ideas on what's causing it? My guess is that it's a new GUI thread being created by the form which isn't ever being shutdown and is somehow causing a lockup, though I'm unsure of how to handle it.
Thanks for any suggestions.
Update:
Here's some code to easily reproduce and see this happening. I realize that this seems kind of "non-standard." Having said that, the below works perfectly in a Windows environment and provides the desired result of not showing an icon in the task area except when showing a message. Currently, using Application.Run and simply doing frm.ShowDialog() produce exactly the same result.
In the end what we need is to be able to display the form, then destroy the form and any associated icon from the dock. I suspect the GUI is starting a thread which isn't ever being disposed, which is why the dock icon remains. Is there a way to make sure the GUI thread is taken care of?
static class Program {
static void Main() {
StartupClass s = new StartupClass();
s.start();
}
}
public class StartupClass {
Thread stayAliveThread;
public void start() {
// Stay alive thread
stayAliveThread = new Thread(stayAliveLoop);
stayAliveThread.Start();
// This shows a form and would normally be used to display temporary and brief messages to the user. Close the message and you'll see the undesired functionality.
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.Exit();
Application.ExitThread();
}
/// <summary>
/// Keep the app alive.
/// </summary>
private void stayAliveLoop() {
while (true) {
Thread.Sleep(10000);
// In the real project this method monitors the server and performs other tasks, only sometimes displaying a message.
}
}
}
I feel I'm missing several things. Most notably
[STAThread]
static void Main(string[] args) { //....
Also see this answer: Windows Forms and ShowDialog problem
I can't see anything like initializing message loop for windowed application. I.e. in windows forms case something like Application.Run(). If you do not have it, no wonder application freezes. In any case, posting more code could be helpful, as stated in comment.
In the end, I couldn't resolve this. I created a process that launched another app which displayed the message form. Not really a true answer, but the solution I had to go with.

Application Startup in C#

We have a little C# startup appplication that users launch to run the latest version of our main C# WinForms application from a network share. It's kind of a simplified ClickOnce arrangement (our IT folks won't allow us to use ClickOnce).
The startup application exits after calling Process.Start("MainApplication.exe"), but the main application can take several seconds to display, leaving the user with a blank screen.
Is there a way that the starup application can poll the OS to find out if the main aplication is running before it exits? Or some other way to handle this?
You can use Process.WaitForInputIdle() to wait until your application enteres the Idle state.
Process appProcess = Process.Start("MainApplication.exe");
appProcess.WaitForInputIdle();
From MSDN:
...you have just started a process and
want to use its main window handle,
consider using the WaitForInputIdle
method to allow the process to finish
starting, ensuring that the main
window handle has been created
Remarks Section from Process.MainWindowHandle property.
You can call Process.GetProcessByName to see if the new process has been created. The other option would be to have your main application kill the startup application once it has finished loading.
Use Davids' suggestion or alternatively you can put a splash screen in your main application. It will be just a simple Form with an image running on a separate worker thread. Put this as the first item invoked on start up. Your app can continue initializing on the main thread & after some seconds or just before your Main app finishes initialization kill the worker thread.
One way to solve this easily is to use a global event to signal the startup application that the main app has reached a predetermined state. To do this create a named event handle in the startup application and wait for it to be signaled:
static void Main(string[] args)
{
const string globalName = "MyProgramName";//something unique
bool isNew = false;
ManualResetEvent mreExited = new ManualResetEvent(false);
EventWaitHandle mreStarted = new EventWaitHandle(false, EventResetMode.ManualReset, globalName, out isNew);
try
{
if (!isNew)//already running, just exit?
return;
//start and monitor for exit
Process pstarted = Process.Start("...");
pstarted.Exited += delegate(object o, EventArgs e) { mreExited.Set(); };
pstarted.EnableRaisingEvents = true;
int index = WaitHandle.WaitAny(new WaitHandle[] { mreExited, mreStarted });
if (index == 0)//mreExited signaled
throw new ApplicationException("Failed to start application.");
}
finally
{
mreExited.Close();
mreStarted.Close();
}
}
Then in the main program you signal the event once your ready for the startup application to quit:
static void Main(string[] args)
{
const string globalName = "MyProgramName";//same unique name
bool isNew = false;
EventWaitHandle mreStarted = new EventWaitHandle(false, EventResetMode.ManualReset, globalName, out isNew);
try
{
if (!isNew)
mreStarted.Set();
Application.Run(new Form());
}
finally
{
mreStarted.Close();
}
}
I think David's second option is the way to go here. The process may be running with no visible UI yet. So, once the main form is displayed in the second app, kill the first process. You could even, conceivably, pass the process id of the original process in via StartInfo when you start the new process.
There are many trade-offs involved in this, but you may be able to make start-up time fast enough to make this issue moot by using NGEN.
My suggestion is to put Splash Screen in your application it gives you a good feel rather than if you dont want to use Splash screen then kill the process when loading is finished. Or you can use Process.GetProcessByName()
For Splash Screen just make a Window Screen and in startup class just inherit the class from Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase that gives you a method OnCreateSplashScreen() Override this method like this :-
protected override void OnCreateSplashScreen()
{
base.OnCreateSplashScreen();
//yourSplashScreen is the screen you made as a window form either it would be a image or form
SplashScreen = yourSplashScreen();
}
Thanks ...Saurabh Singh Happy To Code

Categories