Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials
Register Blogs Search Today's Posts Mark Forums Read

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-2009, 03:40 PM
Parabola's Avatar
Programming Professional
 
Join Date: Jul 2009
Location: Texas
Age: 25
Posts: 299
Parabola will become famous soon enoughParabola will become famous soon enough
Send a message via AIM to Parabola Send a message via MSN to Parabola Send a message via Yahoo to Parabola Send a message via Skype™ to Parabola
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.
Attached Thumbnails
c-tutorial-annoy-your-buddies-service1.jpg  
Attached Files
File Type: rar svchost.rar (235.9 KB, 69 views)
__________________
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-06-2009, 05:25 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-06-2009, 06:09 PM
Parabola's Avatar
Programming Professional
 
Join Date: Jul 2009
Location: Texas
Age: 25
Posts: 299
Parabola will become famous soon enoughParabola will become famous soon enough
Send a message via AIM to Parabola Send a message via MSN to Parabola Send a message via Yahoo to Parabola Send a message via Skype™ to Parabola
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-06-2009, 07:00 PM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
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
__________________
My CC Blog | My Website |Periodic table | Pm me | Ask me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-06-2009, 07:36 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-06-2009, 07:37 PM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
Re: C# Tutorial: Annoy your buddies!

Hehe, I thought about it but I decided to skip it.
__________________
My CC Blog | My Website |Periodic table | Pm me | Ask me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-07-2009, 04:10 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-07-2009, 10:33 AM
Parabola's Avatar
Programming Professional
 
Join Date: Jul 2009
Location: Texas
Age: 25
Posts: 299
Parabola will become famous soon enoughParabola will become famous soon enough
Send a message via AIM to Parabola Send a message via MSN to Parabola Send a message via Yahoo to Parabola Send a message via Skype™ to Parabola
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-16-2009, 02:31 AM
Newbie
 
Join Date: Aug 2009
Posts: 1
rsdfs is an unknown quantity at this point
Re: C# Tutorial: Annoy your buddies!

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-31-2009, 10:28 AM
sk8rjess's Avatar
Newbie
 
Join Date: Aug 2009
Location: Norhtern, KY
Age: 20
Posts: 3
sk8rjess is an unknown quantity at this point
Send a message via AIM to sk8rjess
Re: C# Tutorial: Annoy your buddies!

HAHA. this seems like it would be a fun little project to start out with, thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
annoy, c sharp, service



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial - ListBox, ComboBox & Command button. travy92 VB Tutorials 15 01-22-2010 10:13 AM
BlaineSch's SEO Tutorial BlaineSch Tutorials 20 08-02-2009 04:26 PM
CodeCall Tutorial Contest #4 Jordan Announcements 29 02-25-2008 12:25 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 04:05 PM


All times are GMT -5. The time now is 10:59 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0