Windows Develop Bookmark and Share   
 index > Windows Forms General > Winform app always crashing
 

Winform app always crashing

I got a problem that when i start my app it always crashes (Not Responding) here the code:
If you know whats wrong please post it.


Code Snippet

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace WindowsApplication3
{
public static class Rest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]

static void Main()
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

}


}
public class MyClass
{
public static void gogo()
{
string q = "no";
Process restart2;
DateTime processStartTime = DateTime.Now;
DirectoryInfo di = new DirectoryInfo("Stats");
string a;
string path = "Stats/stats.txt";



restart2 = Process.Start("server.exe");

//Created Folder
if (di.Exists)
{
Console.WriteLine("Ready");
}
else
{
di.Create();
}



while (q != "yes")
{

Thread.Sleep(10000);
if (restart2.HasExited)
{
a = (String.Format("{2} process exited at {0} and ran for {1}", restart2.ExitTime, restart2.ExitTime - processStartTime, (DateTime.Now - restart2.ExitTime)));
restart2 = Process.Start("server.exe");

string appendText = a + Environment.NewLine;
File.AppendAllText(path, appendText);



}
}
}

}
}



Heres the Form.cs:

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace WindowsApplication3
{


public partial class Form1 : Form
{


MyClass test = new MyClass();



public Form1()
{
InitializeComponent();

}


private void button1_Click(object sender, EventArgs e)
{
MyClass.gogo();

}

private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}


Sorry for the long post its my first Winform app.
Thx in Advance.
James0886  Monday, June 04, 2007 3:45 AM
The form is not responsive because you are running MyClass.gogo() in the User Interface(UI) thread. Do not run time intensive tasks in a UI thread. Create a separate thread for it... as for why you need to start that process in a loop like that... well whatever.

What your looking for is something like this in the form
Add
using System.Threading;

then where you call gogo do it like this instead

new Thread(new ThreadStart(MyClass.gogo)).Start();

Not sure why that method is static either... Maybe from your debugging? doesn't need to be.

If you need to know more about threading, so here
http://www.yoda.arachsys.com/csharp/threads/index.shtml

-mwalts

mwalts  Monday, June 04, 2007 8:09 PM

Hi

if u want to check the exe for the exit u dont need to loop it. u can use the

restart2.WaitForExit()

try to using this instead of infinite while loop and checking the condition every 1000 milliseconds

Nayan Paregi  Monday, June 04, 2007 5:06 AM
thx alot for the tip, but i use the loop because i need the program to get restarted infinite times. until i close the app, do you see some thing wrong on the code i posted, because the program works fine in console, but in winform it crashes (Not Responding) when it start and i cant stop it i need to use End Task or it will keep restarting.
James0886  Monday, June 04, 2007 4:50 PM
Could be it is getting lost at
restart2 = Process.Start("server.exe");

Another possibility is you have the file
Stats/stats.txt locked up.

Best to add more debugging statements (Console.Writeline in your case it seems) to figure out which line is hanging you up.
Chris Brandsma  Monday, June 04, 2007 7:04 PM
The form is not responsive because you are running MyClass.gogo() in the User Interface(UI) thread. Do not run time intensive tasks in a UI thread. Create a separate thread for it... as for why you need to start that process in a loop like that... well whatever.

What your looking for is something like this in the form
Add
using System.Threading;

then where you call gogo do it like this instead

new Thread(new ThreadStart(MyClass.gogo)).Start();

Not sure why that method is static either... Maybe from your debugging? doesn't need to be.

If you need to know more about threading, so here
http://www.yoda.arachsys.com/csharp/threads/index.shtml

-mwalts

mwalts  Monday, June 04, 2007 8:09 PM
thx alot mwalts. that fix the prob.
James0886  Tuesday, June 05, 2007 9:00 PM

You can use google to search for other answers

Custom Search

More Threads

• event forwarding
• How do I set defaults for my combo boxes?
• Invoke Hang on Application Shutdown
• how to attach custom (ObjectCollection) items to combo box
• ScrollBar event question
• How to read a rich text file into a richtextbox.
• Problem in Datagrid Combo Box in Win Forms
• textbox different color in each line
• DataGridView.Copy()
• Form TopMost problems