I'm trying to make an auto shutdown application that will shutdown the computer when multiple processes close.
Example: The user has a checklistbox that lists all of the current running processes. The user check marks all desired processes that they wish to monitor.Once all of these processes close then the computer is supposed to shutdown. I am having trouble doing this, I don't know how to make the program check to see if the checked process items have closed. Here is some code that I have right now, I would appreciate all the help anyone can give me.
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.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter;
Process[] p = Process.GetProcesses();
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 100;
foreach (Process plist in p)
{
checkedListBox1.Items.Add(plist.ProcessName);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
counter = 0;
checkedListBox1.Items.Clear();
Process[] p = Process.GetProcesses();
foreach (Process plist in p)
{
checkedListBox1.Items.Add(plist.ProcessName);
counter = counter + 1;
}
if (counter == 0)
{
MessageBox.Show("works");
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
}
}
Thanks,
-Angel Mendez
Assuming you have a List<string> representing your checkboxes, try:
List<string> checkProcs = new List<string>(); // All monitored process names
var allProcesses = Process.GetProcesses().Select(p => p.ProcessName);
// Now use:
allProcesses.Except(checkProcs)
That should give a list of monitored processes that don't exist any more.
using System;
using System.Collections;
using System.Windows.Forms;
using System.Diagnostics;
namespace testprocessapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process[] p = Process.GetProcesses();
timer1.Interval = 10000;
checkedListBox1.Items.Clear();
foreach (Process plist in p)
{
checkedListBox1.Items.Add(plist.ProcessName);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int counter = 0;
Process[] p = Process.GetProcesses();
foreach (Process process in p)
{
foreach (var item in checkedListBox1.Items)
{
if (item.ToString() == process.ProcessName)
{
counter = counter + 1;
}
}
}
MessageBox.Show(counter == 0 ? "Your process has been terminated" : "Your process is still there");
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList arrayList = new ArrayList();
foreach (var checkedItem in checkedListBox1.CheckedItems)
{
arrayList.Add(checkedItem);
}
checkedListBox1.DataSource = arrayList;
//button1.Enabled = false;
button1.Text = "Monitoring...";
timer1.Start();
}
}
}
I have now created a replica of the application. This code works.
Related
The code that I got was from naudio record sound from microphone then save and many thanks to Corey
This is the error message that I get when I run the code for the second or subsequent times.
The first time it runs, it runs with no issues what so ever.
If I change the file name it works perfectly.
Unable to copy file "obj\Debug\Basque.exe" to "bin\Debug\Basque.exe". The process cannot access the file 'bin\Debug\Basque.exe' because it is being used by another process. Basque
Could someone gave me some guidance to where I'm making my error
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 NAudio.Wave;
namespace Basque
{
public partial class FlashCard : Form
{
public WaveIn waveSource = null;
public WaveFileWriter waveFile = null;
public FlashCard()
{
InitializeComponent();
StopBtn.Enabled = false;
StartBtn.Enabled = true;
}
private void StartBtn_Click(object sender, EventArgs e)
{
StartBtn.Enabled = false;
StopBtn.Enabled = true;
waveSource = new WaveIn();
waveSource.WaveFormat = new WaveFormat(44100, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
waveFile = new WaveFileWriter(#"C:\Temp\bas0001.wav", waveSource.WaveFormat);
waveSource.StartRecording();
}
private void StopBtn_Click(object sender, EventArgs e)
{
StopBtn.Enabled = false;
waveSource.StopRecording();
}
void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveFile != null)
{
waveFile.Write(e.Buffer, 0, e.BytesRecorded);
waveFile.Flush();
}
}
void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
{
if (waveSource != null)
{
waveSource.Dispose();
waveSource = null;
}
if (waveFile != null)
{
waveFile.Dispose();
waveFile = null;
}
StartBtn.Enabled = true;
}
private void PlayBtn_Click(object sender, EventArgs e)
{
}
private void ExitBtn_Click(object sender, EventArgs e)
{
}
}
}
It might help to put a Formclosing method with Application.Exit(); in it. If it only works on the first try, it might be because the application isn't fully closing.
You can check if this will fix it when you check task manager. Just make sure that your application isn't still there. Even if it isn't still there, the Application.Exit(); might help.
I'm working on making a program give the ability to allow the user to set a target of an active window.
I have two problems with my code, perhaps someone can let me know if the path i've chosen is wrong or there is a better path.
The Window output is only showing 16 characters of the name of the process.
I have the check box listed but don't know how to dynamically assign them to do the change where it will make the TextBox.Text Change.
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.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace Workspace
{
public partial class Form5 : Form
{
string target = File.ReadAllText("./target.txt");
public Form5()
{
InitializeComponent();
//Shows current target in textbox.
string target = File.ReadAllText("./target.txt");
textBox1.Text = target;
// Sets a starting point.
int total_processes = 0;
// Captures proccesses.
Process[] processlist = Process.GetProcesses();
//looks at all proccess to separate with titles.
foreach (Process process in processlist)
{
//calculates total proccess with titles.
if (!String.IsNullOrEmpty(process.MainWindowTitle))
{
total_processes = total_processes + 1;
}
}
// Sets up string array total by number of processes with name.
string[] stringArray = new string[total_processes];
//Names each proccess array.
int loopnum = 0;
foreach (Process process in processlist)
{
if (!String.IsNullOrEmpty(process.MainWindowTitle))
{
stringArray[loopnum] = process.MainWindowTitle;
loopnum = loopnum + 1;
}
}
// Generates # of Radio buttons per proccess with name.
System.Windows.Forms.RadioButton [] radioButtons = new System.Windows.Forms.RadioButton[total_processes];
for (int i = 0; i < total_processes; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = stringArray[i];
radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
radioButtons[i].CheckedChanged += new EventHandler(this.radioButtons_CheckChanged);
this.Controls.Add(radioButtons[i]);
}
}
private void radioButtons_CheckChanged(object sender, EventArgs e)
{
// Dynamic Check box if checked changes textBox1.Text to radioButtons[i].Text
}
private void button1_Click(object sender, EventArgs e)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("./target.txt");
file.WriteLine(textBox1.Text);
file.Close();
}
}
}
Use ((System.Windows.Forms.RadioButton)sender).Text to get the text property of your radio button:
private void radioButtons_CheckChanged(object sender, EventArgs e)
{
textBox1.Text= ((System.Windows.Forms.RadioButton)sender).Text;
}
When an event raises, the sender cotains a reference to your control that raised the event, so you can access properties of sender control.
I am trying to make a MP3 player with playlist functionality, I have googled everywhere, tried examples, but nothing works. Where the code freezes up at is when p[ListBox1.SelectedIndex + 1]; is called, and I have no idea why it locks up the program. I have tried making an array and selecting the next song through that, but that also didn't work.
I think this is the easiest way to do this. But it locks up the program for some reason.
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.Threading;
using System.Collections;
namespace MusicPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] f, p;
ArrayList playlist = new ArrayList();
private int current_index = 0;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){
f = openFileDialog1.SafeFileNames;
p = openFileDialog1.FileNames;
for (int i = 0; i < f.Length; i++)
{
listBox1.Items.Add(f[i]);
}
foreach (string d in open.FileNames)
{
listBox1.Items.Add(d);
}
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex];
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(MediaPlayer_PlayStateChange);
}
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
switch (axWindowsMediaPlayer1.playState)
{
// Locks up here
case WMPLib.WMPPlayState.wmppsReady:
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
break;
case WMPLib.WMPPlayState.wmppsStopped:
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
break;
default:
break;
}
}
}
}
EDIT:
I updated my code and this is what it looks like:
public void MediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
switch (axWindowsMediaPlayer1.playState)
{
case WMPLib.WMPPlayState.wmppsReady:
axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
break;
case WMPLib.WMPPlayState.wmppsStopped:
axWindowsMediaPlayer1.BeginInvoke(new Action(change_song));
break;
default:
break;
}
}
void change_song()
{
axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex + 1];
}
But when I run this, it gets stuck at "Media Changing" and eventually does nothing.
This question already has an answer here:
Function call only works when MessageBox.Show() is included?
(1 answer)
Closed 8 years ago.
I try to make simple audio player,
then I try it.
After end of song, the player stopped.
I want to set it automatically and select randomized, at the playlist.
I use URL and a ListBox as playlist..
This is the code snippet at autoplay part:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
Random rnd = new Random();
int nowPlayIndex = rnd.Next(listURLPlayers.Count);
axWindowsMediaPlayer1.URL = listURLPlayers[nowPlayIndex];
axWindowsMediaPlayer1.Ctlenabled = true;
axWindowsMediaPlayer1.Ctlcontrols.play();
listAudio.SelectedIndex = nowPlayIndex;
}
}
But I try it then the URL changed, but not played automatically.
What is wrong with my code?
https://github.com/mudzakkir/MP3Player.git
Please help.
Ok it is now working..
I should use Timer..
My code is now like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mp3Player
{
public partial class MediaPlayer : Form
{
string HomeDir;
string[] sPlaylists, sURLs;
List<string> listURLPlayers = new List<string>();
public MediaPlayer()
{
InitializeComponent();
HomeDir = Directory.GetCurrentDirectory();
string[] playlist = File.ReadAllLines("playlist.txt");
foreach (string fileText in playlist)
{
listAudio.Items.Add(fileText);
}
string[] playlistUrl = File.ReadAllLines("playlistURL.txt");
foreach (string fileText in playlistUrl)
{
listURLPlayers.Add(fileText);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
sPlaylists = openFileDialog1.SafeFileNames;
sURLs = openFileDialog1.FileNames;
for (int i = 0; i < sPlaylists.Length; i++)
{
listAudio.Items.Add(sPlaylists[i]);
}
for (int i = 0; i < sURLs.Length; i++)
{
listURLPlayers.Add(sURLs[i]);
}
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter("playlist.txt");
foreach (var item in listAudio.Items)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
SaveFile = new System.IO.StreamWriter("playlistURL.txt");
foreach (var item in listURLPlayers)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
}
}
private void listAudio_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = listURLPlayers[listAudio.SelectedIndex];
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
timer1.Enabled = true;
}
}
private void listAudio_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listURLPlayers[listAudio.SelectedIndex];
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
Random rnd = new Random();
int nowPlayIndex = rnd.Next(listURLPlayers.Count);
axWindowsMediaPlayer1.URL = listURLPlayers[nowPlayIndex];
listAudio.SelectedIndex = nowPlayIndex;
}
}
}
Thank You
I'm trying to build an application that goes to a certain website (from a textbox) x number of times (also from a textbox) through a proxy. What I'm having trouble with is getting the proxy list into the program and using it in the webBrowser control. Here's what I have so far. Any help is appreciated. Thanks.
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.Security.Permissions;
using Microsoft.Win32;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Net;
namespace LetsBot
{
public partial class TrafficBot : Form
{
public TrafficBot()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
this.txtURL.Clear();
this.txtProxy.Clear();
this.txtURL.Focus();
}
private void btnStart_Click(object sender, EventArgs e)
{
this.lblBrowsing.Show();
this.btnStart.Enabled = false;
int n = Convert.ToInt32(this.txtNumVisits.Text);
for (int i = 0; i < n; i++)
{
string url = this.txtURL.Text;
this.webBotBrowser.Navigate(url);
while (webBotBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
string PageText = this.webBotBrowser.DocumentText.ToString();
Thread.Sleep(5000);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
this.btnStart.Enabled = true;
this.webBotBrowser.Stop();
this.txtURL.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Exit Traffic Bot?", "Don't Let Me Go!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Application.Exit();
}
else if (dialogResult == DialogResult.No)
{
}
}
private void webBotBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (e.CurrentProgress > 0)
{
ProgressBar.Value = (int)(e.CurrentProgress / e.MaximumProgress * 100);
}
}
private List<string> getProxyListFromText(string input)
{
List<string> list = new List<string>();
StreamReader reader = new StreamReader(input);
string item = "";
while (item != null)
{
item = reader.ReadLine();
if (item != null)
{
list.Add(item);
}
}
reader.Close();
return list;
for (int i = 0; i < listBox1.Items.Count; i++)
{
object url;
WebClient wc;
url = this.txtURL.Text;
wc = new WebClient();
//This should come from the proxy list
foreach (string prox in getProxyListFromText("Proxies.txt"))
{
wc.Proxy = new WebProxy(prox);
var page = wc.DownloadString(url.ToString());
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
var pplname = doc.DocumentNode.SelectNodes("/html/body/div[3]/div/div[2]/div[2]/div/div[4]/p");
}
}
}
}
}