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.


LinkBack URL
About LinkBacks








Reply With Quote







Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum