tags:

views:

51

answers:

3

I need to induce a variable amount of lag in vb.net. System should be noticeably slower.

I can raise the CPU usage of the program with infinite loops and some variable creation/assignments, but how can I actually cause lag?

I understand this is an odd request, but I appreciate any help and would appreciate it being taken seriously. This is NOT being used maliciously.

I'd like to have an "InvokeLag" sub which will perform an action to slow down the computer, the faster the function is called the faster it lags. The function should be as mundane as possible, not modifying anything or doing anything to cause damage other than simple lag.

Thanks for your help!

EDIT: Preferably only achieving this effect with crazy math operations and making variables and such. Nothing system if possible.

+1  A: 

How about outsourcing your system's lag to another process altogether:

System.Diagnostics.Process.Start("Outlook.exe")

Seriously though, does the system need to be slowed by your process & its computations, or can you rely on other processes to generate the CPU usage? It depends on what you're trying to gauge. It sounds like increasing CPU usage to approach 100% is what you're targeting.

Perhaps call SysInfo, or Vista/Win7, call Windows Experience Index. I know you've updated your question with 'nothing system', but does it matter?

p.campbell
Cpu usage over 100% is probably best. This cannot use any other processes.
Cyclone
+1  A: 

What about creating a new thread with the highest priority? Calculating Pi will never end and with a high priority it should lag your system down. If you have several processors you could start several threads to calculate pi or have a separate program that calculates pi and start several processes of it?

Public Class Test
    Shared Sub Main()

        Dim threadLag As Thread = _
            New Thread(AddressOf CalculatePi)
        threadLag.Name = "ThreadWait"
        threadLag.Priority = ThreadPriority.Highest
        threadLag.Start()
    End Sub

    Private Sub LagRoutine()
        While True
            Thread.SpinWait(10000)
        End While
    End Sub

End Class

or

Public Class Test
    Shared Sub Main()
        For i as Integer = 1 to 10
             Dim p As New Process("C:\MyLagProgram.exe")
             p.Start()
        Next i
    End Sub

End Class

CalculatePi

MSDN

Update:

I think Eric might have a good point about SpinWait and it might be more what you are looking for:

The SpinWait method is useful for implementing locks. Classes in the .NET Framework, such as Monitor and ReaderWriterLock, use this method internally. SpinWait essentially puts the processor into a very tight loop, with the loop count specified by the iterations parameter. The duration of the wait therefore depends on the speed of the processor.

Contrast this with the Sleep method. A thread that calls Sleep yields the rest of its current slice of processor time, even if the specified interval is zero. Specifying a non-zero interval for Sleep removes the thread from consideration by the thread scheduler until the time interval has elapsed.

There is no System IO, its basically just a tight loop which does not give up its processing time. I am sure if you used my suggestion of spawning several threads with the highest priority with Eric's suggestion SpinWait you would achieve what you are looking for. This would also reduce the amount of code you would need to write.

Shiftbit
This method causes lag, definitely. I made three threads, two are on highest and the last is abovenormal. The mem usage was climbing slowly, is there any way having three threads perform an infinite loop of string variable declaration and assignment could possibly harm my computer?
Cyclone
@Cyclone: Strings are immutable so they will slowly eat up memory. However, it will not harm your computer. Your program will crash if you go over 1.5gigs on 32bit computer. This is extremely hard to reach. I was working on a large project and accidently blocked my finalizers and my program would crash, but I highly doubt you will crash with strings. Stress test it and see how long it can run before it crashes.
Shiftbit
Stress testing will now commence. Thank you for your help!
Cyclone
Been running for a few hours now. Mem usage is still well below 1.5 gb. Thanks for all of your tips!
Cyclone
If you are worried about memory consider this: http://stackoverflow.com/questions/934314/maximum-memory-a-net-process-can-allocate. The Process.MaxWorkingSet should give you the allowable memory for your process and Process.WorkingSet64 should give you your current Memory usage.
Shiftbit
+1  A: 

It might help if you defined lag. If you're just looking to spike the CPU, a couple of threads and some loops will do the trick (as you mentioned). You could also use the SpinWait() method on the System.Threading.Thread class.

However, from your post it sounds like that's not acceptable... perhaps that doesn't adequately simulate what you're going for? Are you looking to cause the hard drive to thrash a little bit? When I think of lag I generally think of the system paging back and forth between disk and memory. If that's the case, you could simulate all that IO yourself by reading and writing some large files of whatever (junk data).

Eric
Good Point. The definition of Lag has not been properly defined.
Shiftbit
By lag, I mean the computer slows way down, programs are not responding, and maybe the mouse is laggy.
Cyclone