+ Reply to Thread
Results 1 to 10 of 10

Thread: C# Tutorial: Annoy your buddies!

  1. #1
    Programming Professional Parabola will become famous soon enough Parabola will become famous soon enough Parabola's Avatar
    Join Date
    Jul 2009
    Location
    Texas
    Age
    26
    Posts
    305
    Blog Entries
    3

    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:
    Code:
    using System.Windows.Forms;
    using System.Threading;
    Now we can make our code.

    In the section that says InitializeComponent;, we can start by defining a couple variables.
    Code:
                int x = 1;
                Random RandomNumber = new Random();
    Notice that we define RandomNumber as a Random type.
    Now, we start the loop. Go ahead and add this to your code:
    Code:
    do
    {
    
    } while (x == 1);
    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:
    int delay = RandomNumber.Next(60, 360);
    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.
    Next, we make the thread wait... lurking in the darkness of the victims CPU, waiting to jump out.
    Code:
    Thread.Sleep(delay * 1000);
    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:
    MessageBox.Show(getMessage());
    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.

    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:
    Code:
    public string getMessage()
    {
    
    }
    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:
    Random RandomMessage = new Random();
     int msg = RandomMessage.Next(10);
    This will allow us to have 10 random messages. Now, we COULD go through with if / else statements, like this:
    Code:
    if (msg == 1)
         theMsg = "Message 1";
    if (msg == 2)
         theMsg = "Message 2";
    But let's not. Instead, we are going to use a nice array.
    Code:
    string[] myMsgs =  new string[10];
    Now that we have our array, we need to fill it. Fill it with whatever you want.
    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.";
    Now all that's left is for the method to return a random string.
    Code:
    return (myMsgs[msg]);
    So, your final code should look like this:
    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()
            {
            }
        }
    }
    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!

    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.
    Bijgevoegde miniaturen C# Tutorial: Annoy your buddies!-service1.jpg  
    Bijgevoegde bestanden
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: C# Tutorial: Annoy your buddies!

    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!

  3. #3
    Programming Professional Parabola will become famous soon enough Parabola will become famous soon enough Parabola's Avatar
    Join Date
    Jul 2009
    Location
    Texas
    Age
    26
    Posts
    305
    Blog Entries
    3

    Re: C# Tutorial: Annoy your buddies!

    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.

  4. #4
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    Re: C# Tutorial: Annoy your buddies!

    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

  5. #5
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: C# Tutorial: Annoy your buddies!

    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).

  6. #6
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    Re: C# Tutorial: Annoy your buddies!

    Hehe, I thought about it but I decided to skip it.

  7. #7
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: C# Tutorial: Annoy your buddies!

    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?

  8. #8
    Programming Professional Parabola will become famous soon enough Parabola will become famous soon enough Parabola's Avatar
    Join Date
    Jul 2009
    Location
    Texas
    Age
    26
    Posts
    305
    Blog Entries
    3

    Re: C# Tutorial: Annoy your buddies!

    Quote Originally Posted by ArekBulski View Post
    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?
    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:
    Code:
    psexec -u username \\hostname /i <directory svchost.exe is in>
    For example, if I wanted to install Flash10 on a pc in my network, I'd do this:
    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.

  9. #9
    Newbie rsdfs is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    1

    Re: C# Tutorial: Annoy your buddies!

    thanks

  10. #10
    Newbie sk8rjess is an unknown quantity at this point sk8rjess's Avatar
    Join Date
    Aug 2009
    Location
    Norhtern, KY
    Age
    20
    Posts
    3

    Re: C# Tutorial: Annoy your buddies!

    HAHA. this seems like it would be a fun little project to start out with, thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial - ListBox, ComboBox & Command button.
    By travy92 in forum VB Tutorials
    Replies: 16
    Last Post: 06-15-2010, 09:01 AM
  2. BlaineSch's SEO Tutorial
    By BlaineSch in forum Tutorials
    Replies: 20
    Last Post: 08-02-2009, 12:26 PM
  3. CodeCall Tutorial Contest #4
    By Jordan in forum Announcements
    Replies: 29
    Last Post: 02-25-2008, 09:25 AM
  4. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 01:05 PM