Need suggestions on improving a very simple c# server restarter - c#

Recently I had an idea for a simple auto restarter for my server exe.
Basically all it does is
Checks if there is a process named WerFault running
If there is that means the server has crashed, so it closes both the server and WerFault
After that opens the server again.
The second thing it checks for is if the wServer is even running, if not it starts it up.
This all is within a timer with a delay of 10 seconds.
However I am very sure that there is a much more efficient way of doing this. I learned C# on my own (have not read a single book, all I know was acquired thru self learning + Google).
Also the reason why I want the server to be open 24/7 is because it will run on a vps.
Note: This is a windows forms application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Process[] prs = Process.GetProcesses();
foreach (Process pr in prs)
{
if (pr.ProcessName == "WerFault")
{
pr.Kill();
if (pr.ProcessName == "wServer")
{
pr.Kill();
Process.Start(#"C:\Users\Arturs\Dropbox\ROTMGServer-master\bin\Debug\wServer\wServer.exe");
}
return;
}
}
if (pr.ProcessName == "wServer")
{
return;
}
Process.Start(#"C:\Users\Arturs\Dropbox\ROTMGServer-master\bin\Debug\wServer\wServer.exe");
return;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
}
}
}
}

You've written:
if (pr.ProcessName == "WerFault")
{
pr.Kill();
{
if (pr.ProcessName == "wServer")
{
// ...
It is unlikely that there will ever be a process pr whose ProcessName is both the string "WerFault" and the string "wServer".
Perhaps you want to move the second if outside of the first one.

Related

Why can't I open a webpage in a new window using C# winforms? [duplicate]

This question already has answers here:
Process.Start to open an URL, getting an Exception?
(4 answers)
Closed 1 year ago.
I am very interested in C# winforms. I decided to make it open a browser window once I click on a link label with the default browser. I searched on the internet and found the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
try
{
VisitLink();
}
catch (Exception ex)
{
MessageBox.Show("Unable to open link that was clicked.");
}
}
private void VisitLink()
{
// Change the color of the link text by setting LinkVisited
// to true.
linkLabel1.LinkVisited = true;
//Call the Process.Start method to open the default browser
//with a URL:
System.Diagnostics.Process.Start("http://www.microsoft.com");
}
}
}
When I tried it in my code, I clicked on the link but nothing shows up. Not even an exception pops up, which made me confused. Can anyone help me? Thanks!
The link is correctly marked as visited after the click ? If not, the event is probably not even got fired. Check if you correctly add the callback on the click event

Awesomium "LoadingFrameComplete" is firing too many times

Just recently began tinkering with Awesomium, it's very cool and much better than the stock webBrowser for WinForms.
However, when I use the _LoadingFrameComplete method to determine if the page has loaded, it seems to be firing 10+ times (when used on Facebook, 2 times when navigating to google.com)
I am trying to get the comparable method of webBrowser1_DocumentCompleted (which only fires one time, after the document has completed).
Is this a 'me' problem, or am I using the wrong methods to check whether the website has finished loading completely.
I'm using Visual C# 2010 Edition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Debugging_Problems
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string searchURL = textBox1.Text;
webControl1.Source = new Uri(searchURL);
}
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
richTextBox1.AppendText("Completed.\n");
}
}
}
You need to use IsMainFrame
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
if (e.IsMainFrame)
{
richTextBox1.AppendText("Completed.\n");
}
}
Try putting if(e.IsMainFrame) { .... } inside your LoadingFrameComplete event handler and only put your code in there. – Jon
That was the answer. Thank you.

How to start another .exe in C# when the WinFormApp is going to be closed? [duplicate]

This question already has an answer here:
How to run code before program exit? [duplicate]
(1 answer)
Closed 4 years ago.
When a WinFromApp.exe written in C# is closing,
it finally should start another Prog.exe.
Important, the WinFromApp.exe shouldn't wait for a result of Prog.exe.
The WinFromApp.exe should completly close while Prog.exe runs.
[This is my 2nd using of this forum for place a Question.]
I still have programmed many with Dreamweaver MS-Office-VBA...
Visual Studio (2017) programming is new for me. Because of this, I need also to know where to place the code/code-part(s).
I have as From "Welcome.cs"
["++...++" in Code marks the done including from Anu Viswan.]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.VisualBasic;
using Microsoft.Win32;
using HardwareHelperLib;
namespace Welcome
{
public partial class Welcome : Form
{
public Welcome()
{
InitializeComponent();
+Application.ApplicationExit += Application_ApplicationExit;+
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
...;
}
private void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
...;
}
private void Welcome_Load(object sender, EventArgs e)
{
...;
}
private void button1_Click(object sender, EventArgs e)
{
...;
}
++private void Application_ApplicationExit(object sender, EventArgs e)
{
var process = new ProcessStartInfo("notepad.exe");
Process.Start(process);
}
private void Welcome_FormClosing(object sender, FormClosingEventArgs e)
{
var process = new ProcessStartInfo("notepad.exe");
Process.Start(process);
}++
}
}
And Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Welcome
{
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Welcome());
}
}
}
I have to place this:
Application.ApplicationExit += Application_ApplicationExit;
by here [+...+], and it is working = THANK YOU VERY MUCH !!!
This question already has an answer here:
How to run code before program exit? [duplicate]
I have looked at this link, but not found the answer of my Question...
Because as I has written in my Question:
Visual Studio (2017) programming is new for me. Because of this, I need also to know where to place the code/code-part(s).
Sorry, until yesterday the last years I used stackoverflow only as a Reader.
So I am also new at stackoverflow doing Questions.
Assuming your application is WinForm app, you can do this with ApplicationExit event.
First you need to subscribe for application exit and FormClosing event.
Application.ApplicationExit += Application_ApplicationExit;
and then, in the event, you can use Process.Start to invoke the second exe.
private void Application_ApplicationExit(object sender, EventArgs e)
{
var process = new ProcessStartInfo("notepad.exe");
Process.Start(process);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
var process = new ProcessStartInfo("notepad.exe");
Process.Start(process);
}

C# , pull sites from TXT, pull datafrom website, output data to lists and CSV

Good morning guys I'm new to this place and have been having a lot of issues getting any of the findings to work.
I'm currently using C# inside visual studio community and was using a you tube video as reference but I'm stuck getting it to work efficiently (i think its in a loop) and to do other actions.
Im trying to get a list of urls from a text file in desktop or by user inputting it to a field:
EG: program opens HE puts IP in one text box and hits submit program runs and iots done.
or if it pulls a list of IP's from Desktop C: then it will add it to the website while using a loop?
eg:
my current code is similar to this as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HTTPcollect_Windows_Form_App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> IPs = new List<string>();
WebClient web = new WebClient();
String html = web.DownloadString("http://www.ipvoid.com/scan/8.8.8.8/");
MatchCollection m1 = Regex.Matches(html, #"IP Address.*<strong>(.+?) <\/strong>", RegexOptions.Singleline);
foreach (Match m in m1)
{
string IP = m.Groups[1].Value;
IPs.Add(IP);
}
listBox1.DataSource = IPs;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

FileDialogue - Apparently it doesn't work?

I am currently trying to make my mp3 player for a project.
I have written this code by following a guide:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] files, path;
[STAThread]
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files = openFileDialog1.SafeFileNames;
path = openFileDialog1.FileNames;
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
}
}
}
And heres a screen of my form:
For some reason when I click the OPEN button, nothing happens. First when I used this code I got an error about "Form1_load", but since I wasn't using this, I just deleted the error line and then there was no errors found.
I am very clueless, so anyone with ANY idea what is wrong?
I followed this guide completely:
http://www.c-sharpcorner.com/UploadFile/8ea152/mp3-media-player-in-C-Sharp-4-0/
Thanks
First of all make sure that you have assigned click event for your button for that just double click on your button from design window if it redirects you to the buttonclick method then everything is fine and if it does not then assign it first. For that
public Form1()
{
InitializeComponent();
button1.Click += button1_Click;
}
And
Why do you require [STAThread] here? I mean just remove the attribute and try again because [STAThread] is used for below reson:-
The STAThreadAttribute marks a thread to use the Single-Threaded COM
Apartment if COM is needed. By default, .NET won't initialize COM at
all. It's only when COM is needed, like when a COM object or COM
Control is created or when drag 'n' drop is needed, that COM is
initialized. When that happens, .NET calls the underlying
CoInitializeEx function, which takes a flag indicating whether to join
the thread to a multi-threaded or single-threaded apartment.
For more information :-
http://blogs.msdn.com/b/jfoscoding/archive/2005/04/07/406341.aspx

Categories

Resources