So for some reason when I code this.Close(); into my program it won't actually close it.
I have the program open a different .exe file then close, but I open my task manager and it actually is still running in the background. I have this issue with the "Close" option on my context menu also.
Any ideas why?
EDIT: Even when I exit with the button, it's still in the background.
EDIT:
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void launch_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(Environment.ExpandEnvironmentVariables("%AppData%\\program.exe"));
this.Close();
}
Only actual two codes that are closing the program. But it's still running in the background.
Even when I exit through the actual x button, it still runs in the background.
You must close the process that you've created. Create a variable to save the process that has been created and when you close the form or press another button close process. See: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.close.aspx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private Process _process;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this._process = Process.Start("notepad.exe");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this._process != null) {
this._process.CloseMainWindow();
this._process.Close();
}
}
}
}
As menstion in above comment add using System.Diagnostics; to your code and try this code
public Form1()
{
InitializeComponent();
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
}
void Application_ApplicationExit(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
}
Related
I am new to C# and I want to create a windows forms application, which shows (it must be visible!) one window with some information and buttons and also it loads a page from internet (with selenium and phantom.js - though it is deprecated) every minute. I've written something like this:
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.IO;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
namespace WindowsFormsApplication1
{
public partial class Someclass : Form
{
private void label1_Click(object sender, EventArgs e)
{
}
private void Someclass_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Shows some text "Hello friend"
MessageBox.Show("Hello friend!");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello again", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public Someclass()
{
InitializeComponent();
while (!IsDisposed)
{
MessageBox.Show("Now the page will be downloaded");
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
using (var driver = new PhantomJSDriver(driverService))
{
driver.Navigate().GoToUrl("http://stackoverflow.com/");
MessageBox.Show("Here we are going to open StackOverflow");
var questions = driver.FindElements(By.ClassName("fs-display2"));
foreach (var question in questions)
{
// This will display some text from stackoverflow main page.
Console.WriteLine(question.Text);
question.Click();
MessageBox.Show("This is stackoverflow: " + question.Text);
}
MessageBox.Show("Here we go");
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}
}
}
My problem is that if I use "while", my window does not appear (but the page from internet loads correctly - every 1 minute), and if I use "if" instead of "while", my window appears well, but, of cource, the page loading goes only one time. What can solve my problem?
It's a quite simple solution: your while loop is preventing your UI from Update. You have to bring your loop in an extra Thread, or just use a backgroundworker
Yes, friends, you were right. I have put (using panel of instruments-> Background Worker):
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
while (!IsDisposed)
{
......................
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}
inside my public partial class someClass : Form after public Someclass()
and also added:
backgroundWorker1.RunWorkerAsync(2000);
inside my public Someclass()
So now the code looks like:
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.IO;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
namespace WindowsFormsApplication1
{
public partial class Someclass : Form
{
private void label1_Click(object sender, EventArgs e)
{
}
private void Someclass_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Shows some text "Hello friend"
MessageBox.Show("Hello friend!");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello again", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public Someclass()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync(2000);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
while (!IsDisposed)
{
MessageBox.Show("Now the page will be downloaded");
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
using (var driver = new PhantomJSDriver(driverService))
{
driver.Navigate().GoToUrl("http://stackoverflow.com/");
MessageBox.Show("Here we are going to open StackOverflow");
var questions = driver.FindElements(By.ClassName("fs-display2"));
foreach (var question in questions)
{
// This will display some text from stackoverflow main page.
Console.WriteLine(question.Text);
question.Click();
MessageBox.Show("This is stackoverflow: " + question.Text);
}
MessageBox.Show("Here we go");
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}
}
}
and works fine.
I just "moved" from C++ and a Rad Studio to C# and a Visual Studio as i can see more tutorials and help is available on the internet for VC. But.. I have a problem.
I know how to play a music when a form is created (when program starts). But how can i stop music playing using a normal TButton?
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 _01_21_2019
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
// play an intro sound when a form is shown
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "intro.mp3";
wplayer.controls.play();
}
private void button1_Click(object sender, EventArgs e)
{
wplayer.controls.stop(); // Here it is not working - "current context"
}
}
}
Compiler says
Error CS0103 The name 'wplayer' does not exist in the current context"
I tried to move wplayer.controls.stop() just below the play(); and it works. But how to stop music using a button?
Here is the code on pastebin:
https://pastebin.com/v9wDn5mJ
You should instantiate the object outside of the function so it's available for the class instance.
You might also want to look into the mvvm pattern. It's very helpfull when writing WPF and some other applications.
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
// play an intro sound when a form is shown
wplayer.URL = "intro.mp3";
wplayer.controls.play();
}
private void button1_Click(object sender, EventArgs e)
{
wplayer.controls.stop();
}
}
I am trying to make a room request program and have two windows that I need to connect to the database (which I know how to do). The two windows are called EMERGENCY and JANITOR. My issue is I don't know how to store information in a database. Like let's say someone logs in as EMERGENCY and sends a request, the information needs to be stored and once the JANITOR logs in, he can see the requests. So far I have the database but not sure about storing information in it without manually adding them. If anyone has any guides or links on how to do this, I'd appreciate it!
EMERGENCY window 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;
using System.Data.SqlClient;
namespace LOGINPAGE
{
public partial class EMERGENCY : Form
{
public EMERGENCY()
{
InitializeComponent();
}
void Fillcombo()
{
}
private void EMERGENCY_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'roomInfoDataSet6.Table' table. You can move, or remove it, as needed.
}
private void xButton1_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
private void xButton2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Search_Click(object sender, EventArgs e)
{
}
}
}
JANITOR 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 LOGINPAGE
{
public partial class JANITOR : Form
{
public JANITOR()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void JANITOR_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'roomInfoDataSet3.Table' table. You can move, or remove it, as needed.
this.tableTableAdapter.Fill(this.roomInfoDataSet3.Table);
}
private void xButton4_Click(object sender, EventArgs e)
{
}
private void xButton2_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
EMERGENCY
JANITOR
You can use Ms access db
You will find connecting to acess from c# to really easy with lots of YouTube video tutorials
I can have a table in it database that is updated by a "save emergency" button and a load emergency button by the janitor will just populate a datagridview with items from the same table filled with the first button
This is code for windows forms aplication:
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 WindowsFormsApplication6 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
How to create a file, file.txt, when you click on the button?
You have many methods you can use on File, like File.Create. This simply creates or overwrites a file in the local running folder:
private void button1_Click(object sender, EventArgs e)
{
if (!File.Exists("file.txt"))
{
File.Create("file.txt");
}
}
Use File.WriteAllText Method which creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.
private void button1_Click(object sender, EventArgs e)
{
File.WriteAllText("file.txt","your text ");
}
I've recently made this small Windows Forms Application:
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;
using System.Threading;
namespace Spammer
{
public partial class Form1 : Form
{
Thread t1;
int delay, y = 1;
public Form1()
{
t1 = new Thread(send);
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
t1.Start();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
y = 0;
}
private void send()
{
while (y == 1)
{
String textt = textBox1.Text;
Thread.Sleep(delay);
SendKeys.SendWait(textt);
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
Now, it looks like this:
It has a Delay textbox, a Text to Send textbox, and 2 buttons: Start and Stop.
Now, I've tried running it and setting the delay to 1000ms.
When I press the "Stop" button, it perfectly stops and no more messages are sent.
But when I input in the delay very small delays like 100ms, for example, pressing "Stop" doesn't do anything.
It's even kind of hard to click it, and even when I click it doesn't stop sending the messages.
Why is this? And can I solve it somehow?
OK, Thank you, everyone, for helping!
I'm now using the GUI timer and it works flawlessly.