I'm a C# beginner and I have made a little launcher for a game and to start it, I'm using this button event:
private void button5_MouseClick(object sender, MouseEventArgs e)
{
Process.Start("engine.exe", "/load /config debug");
Application.Exit();
}
As you can see this also uses Application.Exit() to close the launcher when the game starts, because they are separate.
What I want to know is if my method is good to start the game + close launcher and also,
I would like to know how to make a pop up message saying "Engine.exe not found" when I'm clicking the button and engine.exe is missing.
Thanks!
try
{
Process.Start("engine.exe", "/load /config debug");
Application.Exit();
}
catch(FileNotFoundException e)
{
MessageBox.Show(e.Message);
}
Related
I'm using axWindowsMediaPlayer playlist, and have some problem - after one video end's - next playing without any pause. I cant pause or stop it even with
private void axWindowsMediaPlayer1_MediaChange(object sender, AxWMPLib._WMPOCXEvents_MediaChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{axWindowsMediaPlayer1.Ctlcontrols.pause(); }}
It doesn't do anything, but must stop the player. What can I do with this? Or even disable AUTOplaying next playlist file, that's would be even better.
Thx a lot for help!
I believe you should be working with the object passed into the method and additionally the PlayStateChange event like so:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
e.Ctlcontrols.pause();
}
}
}
UPDATE: Also I would suggest that you use the axWindowsMediaPlayer1_PlayStateChanged() event rather than Media change as this may be where the issue is occurring.
The '8' is to signify "Media Ended" which if you know how to access it another way then you of course can. If you want to use WMPLib.WMPPlayState.wmppsMediaEnded I'm sure it would yeild the same result.
"simple" question to the "streamer guy's there". My program starts SQL backup processes in single tasks, for any backup per DB.
All the tasks start one by one after the first task finished. But, when the SQL backup command goes to the SQL server, than the process to write the backupfile takes more time then task takes to finish. At this moment, the user can close the program and skip the backupprocess to the big DB files.
Is there a way to block my programm from closing, until every single backupfile is completly written to the HDD?
You can handle the Closing of the Window like this:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (BackupStillRunning)
e.Cancel = true;
else
e.Cancel = false;
}
What you should do though is ask the user if he really wants to quit, something like this:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (BackupStillRunning)
{
if (MessageBox.Show("The backup is still running, if you close the application, the backup will be cancelled. Do you want to proceed?", "Caption", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
e.Cancel = true;
}
else
{
e.Cancel = false;
}
}
Simply not letting the user close the application will quickly get frustrating. If you really dont want the user to close the application, you should at least show a message explaining why.
As Damien_The_Unbeliever pointed out, there is no way to completely prevent the application from shutting down.
I hope that you can help me with this one, my C# is very rusty.
I'm running an executable when the form loads.
private void Form1_Load(object sender, EventArgs e)
{
ProcessStartInfo exe = new ProcessStartInfo();
exe.Arguments = "arguments";
exe.FileName = "file.exe";
Process.Start(exe);
}
And I would like to kill that process using a button, but I don't know how to achieve that.
private void button1_Click(object sender, EventArgs e)
{
}
Thanks.
Process.Start returns an object of type Process. You can save it into variable, then use the method Kill, which Immediately stops the associated process (msdn)
For example declare a field at Form1 level:
class Form1
{
private Process process;
private void Form1_Load(object sender, EventArgs e)
{
//running notepad as an example
process = Process.Start("notepad");
}
//and then at button handler kill that process
private void button1_Click(object sender, EventArgs e)
{
//consider adding check for null
process.Kill();
}
}
You should call Process.CloseMainWindow which performs an orderly termination of the process and closes all windows. As opposed to Process.Kill which causes abnormal termination. CloseMainWindow is preferable for applications with a user interface.
process = Process.Start(exe);//in form load set field
private void button1_Click(object sender, EventArgs e)
{
process.CloseMainWindow();
}
The remarks on MSDN reveals important information regarding the asynchronous behavior and other relevant details.
Kill forces a termination of the process, while CloseMainWindow only
requests a termination. When a process with a graphical interface is
executing, its message loop is in a wait state. The message loop
executes every time a Windows message is sent to the process by the
operating system. Calling CloseMainWindow sends a request to close to
the main window, which, in a well-formed application, closes child
windows and revokes all running message loops for the application. The
request to exit the process by calling CloseMainWindow does not force
the application to quit. The application can ask for user verification
before quitting, or it can refuse to quit. To force the application to
quit, use the Kill method. The behavior of CloseMainWindow is
identical to that of a user closing an application's main window using
the system menu. Therefore, the request to exit the process by closing
the main window does not force the application to quit immediately.
etc...
try this
try
{
Process [] proc Process.GetProcessesByName("notepad");
proc[0].Kill();
}
In a C# Console app, pressing the Pause key freezes the display output. Can I disable that?
I was hoping for a handler like the Console.CancelKeyPress event that handles Ctrl+C input.
Every once in a while a request comes up for hooking keys from a console program. The standard events like CTRL_C_EVENT and CTRL_CLOSE_EVENT do not include the Pause-event. I've tried doing so using a background thread, but I don't seem to manage. However, there's a not-so-hard workaround: use an extra process.
Download this easy-to-use global keyboard hook for C#. Then, when you open that project, take the following code and put it in the Form1.cs:
public partial class Form1 : Form {
globalKeyboardHook globalKeyboardHook = new globalKeyboardHook();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
globalKeyboardHook.HookedKeys.Add(Keys.Pause);
globalKeyboardHook.KeyDown += new KeyEventHandler(globalKeyboardHook_KeyDown);
globalKeyboardHook.KeyUp += new KeyEventHandler(globalKeyboardHook_KeyUp);
}
void globalKeyboardHook_KeyUp(object sender, KeyEventArgs e)
{
// remove this when you want to run invisible
lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
// this prevents the key from bubbling up in other programs
e.Handled = true;
}
void globalKeyboardHook_KeyDown(object sender, KeyEventArgs e)
{
// remove this when you want to run without visible window
lstLog.Items.Add("Down\t" + e.KeyCode.ToString());
// this prevents the key from bubbling up in other programs
e.Handled = true;
}
}
Then, the rest becomes trivial:
Change your program to start the above program and than run normally
Try to type the pause key
It'll be caught by the other program
Your program will NOT be paused.
I tried the above myself and it works.
PS: I don't mean that there isn't a possible way straight from a Console program. There may very well be, I just didn't find it, and the above global keyhook library didn't work from within a Console application.
It's not necessary to attach an hook for this. In your case #PeteVasi, you can modify the console mode to capture Ctrl+C, Ctrl+S, etc... events that are not normally possible to capture.
See my answer to a similar question here.
I have a console app, and I want to capture Control-C and shutdown gracefully.
I have the following code:
Console.CancelKeyPress += new ConsoleCancelEventHandler((o, e) =>
{
Logger.Log("Control+C hit. Shutting down.");
resetEvent.Set();
});
And the output windows shows:
6/16/2010 3:24:34 PM: Control+C hit. Shutting down.
^C
Is there a way to prevent the control-c character ^C from appearing? It's not a huge deal, but for some reason Ill be fixated on it because I'm anal like that.
Being a console application I would use Environment.Exit to signal that the application is exiting and this way stopping the ^C from being print.
You could even provide a specific error code to symbolize that the user pressed CTRL+C.
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("Control+C hit. Shutting down.");
Environment.Exit(-1);
}
This should work...
Console.CancelKeyPress += new ConsoleCancelEventHandler((o, e) =>
{
Console.WriteLine("Control+C hit. Shutting down.");
Environment.Exit(0);
});
Console.ReadLine();
While this is not necessarily a solution you want to implement, it does prevent the ^C from being displayed:
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("Control+C hit. Shutting down.");
Process.GetCurrentProcess().Kill();
}