Lol, very funny! I remember back when I made some programs that would ask a question like:
"Are you an idiot?" <Yes/No buttons>
Whenever you hovered over No it swapped places with the Yes button.
+rep!
Thread: C# Tutorial: Annoy your buddies! |
Ok, this tutorial is more for fun, not to actually spread through the office.
Tasks demonstrated:
Using the random function
Creating a windows service
Creating a method that will return a string
Putting a thread to sleep
Defining an array
ANNOYING PEOPLE
__________________________________________________ ________
Let's start. Create a new project. This time, we are going to create a windows service. Let's name it svchost (lol).
(see service1.jpg in the attached file if you need to know where it is)
Go ahead and click the view code link displayed. First off, we need to add a reference. In your object explorer, right click references, and click add reference. Under the .NET tab, we want to add System.Windows.Forms
Now in your code, we need to use that reference, and another for the thread, so add this to the top:
Now we can make our code.Code:using System.Windows.Forms; using System.Threading;
In the section that says InitializeComponent;, we can start by defining a couple variables.
Notice that we define RandomNumber as a Random type.Code:int x = 1; Random RandomNumber = new Random();
Now, we start the loop. Go ahead and add this to your code:
Of course, our loops needs to do something. So in our loop, we need to first create our random delay time. This way, the service might take a minute to pop a message, it might take 5, luring them into thinking it's gone.Code:do { } while (x == 1);
So now, we have an integer, delay, that is a random number between 60 and 360. This is how many seconds the delay will take.Code:int delay = RandomNumber.Next(60, 360);
Next, we make the thread wait... lurking in the darkness of the victims CPU, waiting to jump out.
This causes our thread to "sleep" for however many seconds the delay is. This is where the System.Threading comes in. Next we get our random message, as we don't want the same thing over and over do we? And then we want to display that message.Code:Thread.Sleep(delay * 1000);
But what is getMessage()? We will define that method in a little bit. MessageBox.Show is allowed only because we added the forms reference, and used it.Code:MessageBox.Show(getMessage());
Now then, our loop is done. So now we need to define our method, getMessage().
After the public svchost() section, add a new section, like this:
So, this method, as you may have guessed, will return a string. A random string, at that. So once again, we need to define a random variable.Code:public string getMessage() { }
This will allow us to have 10 random messages. Now, we COULD go through with if / else statements, like this:Code:Random RandomMessage = new Random(); int msg = RandomMessage.Next(10);
But let's not. Instead, we are going to use a nice array.Code:if (msg == 1) theMsg = "Message 1"; if (msg == 2) theMsg = "Message 2";
Now that we have our array, we need to fill it. Fill it with whatever you want.Code:string[] myMsgs = new string[10];
Now all that's left is for the method to return a random string.Code:myMsgs[0] = "I don't like you"; myMsgs[1] = "Windows has encountered an error in your chair."; myMsgs[2] = "Is this annoying?"; myMsgs[3] = "Go ahead, call your IT guy."; myMsgs[4] = "Just pull the plug, then I might shutup."; myMsgs[5] = "Free gay pics! Click OK!"; myMsgs[6] = "Your boss is watching you."; myMsgs[7] = "Windows has encountered an error and will shut down."; myMsgs[8] = "codecall.net is awesome!"; myMsgs[9] = "Plug your phone into me... I want to do dirty things to it.";
So, your final code should look like this:Code:return (myMsgs[msg]);
Now, build it, but don't publish it. In windows, go to the folder the project is in, go into the debug folder, and grab the .exe from in there. Stick this into your friend's startup, and watch the fun. You could also (on a network of course) sneak it into their computer, then use psexec (google it) to remotely run the program. Have fun!Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Windows.Forms; using System.Threading; namespace svchost { public partial class svchost : ServiceBase { public svchost() { InitializeComponent(); int x = 1; string myMsg = ""; Random RandomNumber = new Random(); do { int delay = RandomNumber.Next(60, 360); Thread.Sleep(delay * 1000); myMsg = getMessage(); MessageBox.Show(myMsg); } while (x == 1); } public string getMessage() { Random RandomMessage = new Random(); int msg = RandomMessage.Next(10); string[] myMsgs = new string[10]; myMsgs[0] = "I don't like you"; myMsgs[1] = "Windows has encountered an error in your chair."; myMsgs[2] = "Is this annoying?"; myMsgs[3] = "Go ahead, call your IT guy."; myMsgs[4] = "Just pull the plug, then I might shutup."; myMsgs[5] = "Free gay pics! Click OK!"; myMsgs[6] = "Your boss is watching you."; myMsgs[7] = "Windows has encountered an error and will shut down."; myMsgs[8] = "codecall.net is awesome!"; myMsgs[9] = "Plug your phone into me... I want to do dirty things to it."; return (myMsgs[msg]); } protected override void OnStart(string[] args) { } protected override void OnStop() { } } }
Side note: I ran this on my own computer (of course), and it worked as it should. However, when I went into the device manager and ended the process.... it didn't work. RUN THIS AT YOUR OWN CAUTION! lol It does no damage, but... does get annoying... I need to log off now lol.
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.
Lol, very funny! I remember back when I made some programs that would ask a question like:
"Are you an idiot?" <Yes/No buttons>
Whenever you hovered over No it swapped places with the Yes button.
+rep!
Lol didn't even think of doing that. But that's a whole different tutorial.
By the way, (this is for everyone), these tutorials I write- I'm actually writing them as I learn them. I literally sit down and think, "what do I want to make today?" I decide, then as I go along, I make my tutorial out of it. So seriously- I'm figuring all these things out as I write it, with no foreknowledge of how any of this works. SO.... If you see something that maybe I could shorten, something to make the code better, anything- say so.
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.
Nice tutorial, +rep.
@ Jordan: You mean like this(not in C# though)?
Code:Sub CreateForm() Dim frm As New Form frm.Size = New Size(250, 90) frm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow frm.MaximizeBox = False frm.Text = "Question?" frm.Show() Dim lblQuestion As New Label lblQuestion.Parent = frm lblQuestion.Size = New Size(300, 30) lblQuestion.Location = New Point(15, 5) lblQuestion.Font = New Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Point) lblQuestion.Text = "Are you an Idiot?" Dim btnYes As New Button btnYes.Parent = frm btnYes.Size = New Size(75, 20) btnYes.Location = New Point(40, 40) btnYes.Text = "Yes" AddHandler btnYes.Click, AddressOf btn_Click Dim btnNo As New Button btnNo.Parent = frm btnNo.Size = New Size(75, 20) btnNo.Location = New Point(130, 40) btnNo.Text = "No" AddHandler btnNo.MouseEnter, AddressOf btnNo_Enter_Leave AddHandler btnNo.MouseLeave, AddressOf btnNo_Enter_Leave AddHandler btnNo.Click, AddressOf btn_Click End Sub Sub btnNo_Enter_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Dim strNew As String If sender.Text = "No" Then strNew = "Yes" Else strNew = "No" End If For Each Item In sender.parent.Controls If Item.GetType.ToString = "System.Windows.Forms.Button" Then If Item.Text = strNew Then Item.Text = sender.Text End If End If Next sender.Text = strNew End Sub Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show("You're an Idiot", "Idiot", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) sender.parent.close() End Sub
Sort of but my program actually moved the buttons so the user could see them moving (but changing the text probably works just as well). Also, you have to take care that they can't exit the programing using traditional methods (X button, right-click and close, etc).![]()
Hehe, I thought about it but I decided to skip it.
This service program is great! I would like to see it being capable of being stopped, but that does not change my review. This tutorial is as funny as well-coded.
What do you say for a homework assignment? Would you add a stop capability to it?
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Lol I found out actually that while killing the process didn't seem to work, after I exited VS, it stopped. Apparently VS, even though I was no longer debugging or even telling it to run in any way, was still spawning that process... odd, but that's what my issue was.
Also- reason I say don't publish, as we all know, when you run a prog published from VS, you get that nice loading popup, etc. BUT, if you go into the bin/debug dir, that little exe doesn't even ask for permission to run... it just runs quietly.
If you get psexec, here's an example on how to use it:
For example, if I wanted to install Flash10 on a pc in my network, I'd do this:Code:psexec -u username \\hostname /i <directory svchost.exe is in>
Code:psexec -u markspp\<myusername> \\hostname MsiExec.exe /i \\<root server dir>\Public\IT\Deploy\install_flash_player_active_x.msi /q REBOOT=ReallySuppress
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.
thanks
HAHA. this seems like it would be a fun little project to start out with, thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)