Jump to content

VB.NET - I need help figuring out a code

Hi!

 

So basically I need help with figuring out a code that will be executed by a timer that has an interval of 7 minutes. What this code will do is simply something small that will not bother the user and not interupt things like powerpoint presentations or youtube videos etc. But it will just do something that makes sure that the computer does not start it's screensaver.

 

The reason for this is that on my school the teachers computers has some sort of lock on them from the IT dep. that basically makes it impossible (that I know of) to change the time for when the screen saver will be activated and when the screen goes to sleep. So basically what I want this program to do is to every 7 minute or so execute a code that does something small and not noticeble so it will not bother anyone but we can have powerpoints on for longer than 10 minutes without the screen going to sleep. (Mostly you go through several slides so often it's not a problem but when you stop for us students to write notes or the teacher is talking about what is on the screen then there are problems... The IT dep. on the school are morons and refuses to remove it, or they just don't know why it is there and just will not admit it? But I thought I'd do this to make life easier for the teachers on my school. As well us students.)

 

Im programming VB.Net not C# or C++ or C... I have thought about implementing a timer that will be for 1 hour that simply will stop the other timer that makes the computer "remove" the screensaver because it will eventually have to go into screensaver mode and if you forget the program on then the one picture may hurt your screen. So it can be good to have that there. I will then add a reload button to simply reload the timers for another hour.

 

Please ask me for more info if you want it. But really, Im the one who has the questions! So I really appreciate if someone helps!

 

 

Regards,

 

Ludwig Johnson

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why dont you build a simple console application that moves the mouse 1px every 6 mins?

Intel I9-9900k (5Ghz) Asus ROG Maximus XI Formula | Corsair Vengeance 16GB DDR4-4133mhz | ASUS ROG Strix 2080Ti | EVGA Supernova G2 1050w 80+Gold | Samsung 950 Pro M.2 (512GB) + (1TB) | Full EK custom water loop |IN-WIN S-Frame (No. 263/500)

Link to comment
Share on other sites

Link to post
Share on other sites

Why dont you build a simple console application that moves the mouse 1px every 6 mins?

That is basically the same advice I gave him is in last topic :P

 

@lubblig Why didn't you just respond in your previous topic?  If you are just going to add one condition (being VB) then there isn't much point creating a brand new topic.

 

Anyways, my last response still applies (it is just follow the instructions, if you can't get VB working and just want to go with C#).  As for VB, well they call it .net for a reason...almost everything in C# is also compatible with VB (just syntax differences...and very occasionally a few minor details).

 

For VB.net cursor moves are as simple as C#....and the VB code is as follows

Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50)

(Also just for fun, compare this to the C# code...btw this is all from the MSDN website but with only the part that concerns you is http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2)

Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

That is basically the same advice I gave him is in last topic :P

 

@lubblig Why didn't you just respond in your previous topic?  If you are just going to add one condition (being VB) then there isn't much point creating a brand new topic.

 

Anyways, my last response still applies (it is just follow the instructions, if you can't get VB working and just want to go with C#).  As for VB, well they call it .net for a reason...almost everything in C# is also compatible with VB (just syntax differences...and very occasionally a few minor details).

 

For VB.net cursor moves are as simple as C#....and the VB code is as follows

Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50)

(Also just for fun, compare this to the C# code...btw this is all from the MSDN website but with only the part that concerns you is http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2)

Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);

Hi, sorry for that but I just couldn't find the old topic.. :(  I'm not that good at searching around this forum.  But thank you for helping me again anyway!

 

This is somewhat what I have found when I have googled my self but I get errors on "Cursor.Position". It says that: 'Position' is not a member of 'System.Windows.Forms.Cursor'. What is wrong? I went to the link on microsoft and added the "Public Shared Property Position As Point" But that didn't help...

 

Do you know what the problem is? Remember that Im putting this in a: Private Sub Timer1_Tick.....

 

So the example that was on their website with Private Sub MoveCursor() Is not the same as mine.

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

hmm, sorry in this case I don't really know....could just be you are missing an include...if VB is anything like C#, you the Cursor.Position should be underlined due to the error and you can right click on the word and see if there is a "resolve" option...sometimes it is just that simple to fix it. (ie..Imports System.Windows.Forms   or something like that).  Sorry I can't be much more of a help...any chance at getting the specific error (copy and pasted from the error list)...I find sometimes my biggest problem when using Position is I spell it wrong and don't realize it :P

 

Also fyi http://linustechtips.com/main/topic/78754-net-mouse-move-when-timer-ticks/ just in case you want to attempt my original response in C#

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

hmm, sorry in this case I don't really know....could just be you are missing an include...if VB is anything like C#, you the Cursor.Position should be underlined due to the error and you can right click on the word and see if there is a "resolve" option...sometimes it is just that simple to fix it. (ie..Imports System.Windows.Forms   or something like that).  Sorry I can't be much more of a help...any chance at getting the specific error (copy and pasted from the error list)...I find sometimes my biggest problem when using Position is I spell it wrong and don't realize it :P

 

Also fyi http://linustechtips.com/main/topic/78754-net-mouse-move-when-timer-ticks/ just in case you want to attempt my original response in C#

haha, yes you were correct. Spelling error. Apperently you cannot spell position with two 's'. Thanks. :)

 

But, I tried it out. It sadly does not cope with the screensaver problems. Even though the screen is dimmed I can see the mouse in the background moving and it does not remove the dim and eventually it goes into sleep.

 

So this code and having the computer move the cursor on it's own does not remove the screensaver, it has to be done by hand.

 

Have any other ideas?

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well you could try this (sorry I don't really know too much about importing windows functions in VB, so the syntax and functionality might not be correct)

Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As LongPublic Enum ES As Integer    ES_CONTINUOUS = &H80000000    ES_DISPLAY_REQUIRED = &H2    ES_SYSTEM_REQUIRED = &H1End EnumPublic Function preventSS()    SetThreadExecutionState(ES.ES_SYSTEM_REQUIRED Or ES.ES_DISPLAY_REQUIRED Or ES.ES_CONTINUOUS)End FunctionPublic Function allowSS()    SetThreadExecutionState(ES.ES_CONTINUOUS)End Function

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

Well you could try this (sorry I don't really know too much about importing windows functions in VB, so the syntax and functionality might not be correct)

Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As LongPublic Enum ES As Integer    ES_CONTINUOUS = &H80000000    ES_DISPLAY_REQUIRED = &H2    ES_SYSTEM_REQUIRED = &H1End EnumPublic Function preventSS()    SetThreadExecutionState(ES.ES_SYSTEM_REQUIRED Or ES.ES_DISPLAY_REQUIRED Or ES.ES_CONTINUOUS)End FunctionPublic Function allowSS()    SetThreadExecutionState(ES.ES_CONTINUOUS)End Function

Thanks for the code. But I didn't understand it so I'm not sure of what to put in the timer.. Or is there no timer needed? Could you please explain the code? That would be very helpful!

 

Thank you again! You have been very helpful! :)

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the code. But I didn't understand it so I'm not sure of what to put in the timer.. Or is there no timer needed? Could you please explain the code? That would be very helpful!

 

Thank you again! You have been very helpful! :)

I would be glad to try explaining it.  To be fair, I have never used this code before so I could be wrong.  It just is a concept built off what I quickly researched. (As the SetThreadExecutionState isn't native to VB....which is why it was imported).

 

So my VB is really rusty so I might get a bit of details wrong but it should get you on your way.

 

First copy and paste the code into a module... (see code at bottom...I added a Module for you...if the syntax is right :P)

When you program start (...if I recall double clicking on the form in the form builder pops up an onload sub that you can use) call MyModuleClass.preventSS()

When your program ends call MyModuleClass.allowSS()  (Or if you wanted to you could just call MyModuleClass.allowSS() after 1 hour using a timer)

This should hopefully prevent the ScreenSaver from popping up (no guarantees, but it is the only "easy" solution I found)

 

To explain what the code itself is doing.

First I imported a windows function that isn't in VB and added in the states the function accepts

preventSS calls the SetThreadExecutionState, which is telling Windows that even without mouse movements the Display is required

allowSS will just tell windows that everything is completed and the monitor is allowed to sleep

 

 

Added a module, which I think you should just be able to copy and paste into a new module file....not sure the file extension :P

Module MyModuleClassPrivate Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As LongPublic Enum ES As Integer    ES_CONTINUOUS = &H80000000    ES_DISPLAY_REQUIRED = &H2    ES_SYSTEM_REQUIRED = &H1End EnumPublic Function preventSS()    SetThreadExecutionState(ES.ES_SYSTEM_REQUIRED Or ES.ES_DISPLAY_REQUIRED Or ES.ES_CONTINUOUS)End FunctionPublic Function allowSS()    SetThreadExecutionState(ES.ES_CONTINUOUS)End FunctionEnd Module

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

 

I would be glad to try explaining it.  To be fair, I have never used this code before so I could be wrong.  It just is a concept built off what I quickly researched. (As the SetThreadExecutionState isn't native to VB....which is why it was imported).

 

So my VB is really rusty so I might get a bit of details wrong but it should get you on your way.

 

First copy and paste the code into a module... (see code at bottom...I added a Module for you...if the syntax is right :P)

When you program start (...if I recall double clicking on the form in the form builder pops up an onload sub that you can use) call MyModuleClass.preventSS()

When your program ends call MyModuleClass.allowSS()  (Or if you wanted to you could just call MyModuleClass.allowSS() after 1 hour using a timer)

This should hopefully prevent the ScreenSaver from popping up (no guarantees, but it is the only "easy" solution I found)

 

To explain what the code itself is doing.

First I imported a windows function that isn't in VB and added in the states the function accepts

preventSS calls the SetThreadExecutionState, which is telling Windows that even without mouse movements the Display is required

allowSS will just tell windows that everything is completed and the monitor is allowed to sleep

 

 

Added a module, which I think you should just be able to copy and paste into a new module file....not sure the file extension :P

Module MyModuleClassPrivate Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As LongPublic Enum ES As Integer    ES_CONTINUOUS = &H80000000    ES_DISPLAY_REQUIRED = &H2    ES_SYSTEM_REQUIRED = &H1End EnumPublic Function preventSS()    SetThreadExecutionState(ES.ES_SYSTEM_REQUIRED Or ES.ES_DISPLAY_REQUIRED Or ES.ES_CONTINUOUS)End FunctionPublic Function allowSS()    SetThreadExecutionState(ES.ES_CONTINUOUS)End FunctionEnd Module

I added a new module to the project and then I added the code but then when the program runs. It highlights the "SetThreadExecutionState(ES.ES_SYSTEM_REQUIRED Or ES.ES_DISPLAY_REQUIRED Or ES.ES_CONTINUOUS)" code in green and says:

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\LudwigMain\Documents\Visual Studio 2012\Projects\prevent_SS\prevent_SS\bin\Debug\prevent_SS.vshost.exe'.

Additional Information: A call to PInvoke function 'prevent_SS!prevent_SS.Module1::SetThreadExecutionState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

 

 

It then gives me the option to continue or break, and continue does not work and break just pauses the program. So no, that didn't do it. But since you seem more "into" C#. Can you give me somewhat more detailed instructions on how to do it in C# instead? I know some C# so I will understand the basics of it and so on but I'm still not as certain on it as I am with VB. How about that? Or do you want to crack this code? :P

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

hmm, yea sorry, I don't know enough about VB to figure out why it is doing that.

 

Ironically, although this is a more complex task the instruction get easier in C#.  So since I am a chicken without a head in VB, let me just post the C# instructions.

 

1) Create a C# console application (ie. just File -> New -> Project and Select Visual C# and then the Console Application)

2) Visual Studios should have created a Program.cs file, just open that file up

3) Copy and paste the code at the bottom (Paste it overtop all the code in there)

4) Compile and Enjoy (Lot easier than the last solution)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Runtime.InteropServices;namespace TestConsoleApp{    class Program    {        //Just creating the options        [FlagsAttribute]        public enum EXECUTION_STATE : uint        {            ES_AWAYMODE_REQUIRED = 0x00000040,            ES_CONTINUOUS = 0x80000000,            ES_DISPLAY_REQUIRED = 0x00000002,            ES_SYSTEM_REQUIRED = 0x00000001        }        //Importing the function from the Kernel32.dll file...personally this is why I like C# more...more descriptive        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); //Defining the function        private const double HoldForXMinutes = 60;        static void Main(string[] args)        {            preventScreenSaver();            Thread.Sleep(Convert.ToInt32(1000 * 60 * HoldForXMinutes)); //This just pauses the program for X amount of Minutes...handy feature            allowScreenSaver();        }        private static void preventScreenSaver()        {            //Requesting the display            SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);        }        private static void allowScreenSaver()        {            //Releasing any state information            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);        }    }}

I tried to test out the program, but I can't make my screen saver come up (perhaps because I have programs that are preventing it running)...so sorry that this is still untested (but at least the program will run)

 

As an fyi you can change the minutes it waits before reenabling the screen saver and you can close the program by ctrl-c

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

hmm, yea sorry, I don't know enough about VB to figure out why it is doing that.

 

Ironically, although this is a more complex task the instruction get easier in C#.  So since I am a chicken without a head in VB, let me just post the C# instructions.

 

1) Create a C# console application (ie. just File -> New -> Project and Select Visual C# and then the Console Application)

2) Visual Studios should have created a Program.cs file, just open that file up

3) Copy and paste the code at the bottom (Paste it overtop all the code in there)

4) Compile and Enjoy (Lot easier than the last solution)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Runtime.InteropServices;namespace TestConsoleApp{    class Program    {        //Just creating the options        [FlagsAttribute]        public enum EXECUTION_STATE : uint        {            ES_AWAYMODE_REQUIRED = 0x00000040,            ES_CONTINUOUS = 0x80000000,            ES_DISPLAY_REQUIRED = 0x00000002,            ES_SYSTEM_REQUIRED = 0x00000001        }        //Importing the function from the Kernel32.dll file...personally this is why I like C# more...more descriptive        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); //Defining the function        private const double HoldForXMinutes = 60;        static void Main(string[] args)        {            preventScreenSaver();            Thread.Sleep(Convert.ToInt32(1000 * 60 * HoldForXMinutes)); //This just pauses the program for X amount of Minutes...handy feature            allowScreenSaver();        }        private static void preventScreenSaver()        {            //Requesting the display            SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);        }        private static void allowScreenSaver()        {            //Releasing any state information            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);        }    }}

I tried to test out the program, but I can't make my screen saver come up (perhaps because I have programs that are preventing it running)...so sorry that this is still untested (but at least the program will run)

 

As an fyi you can change the minutes it waits before reenabling the screen saver and you can close the program by ctrl-c

Awesome! It works! :) But for how long does it run? How can I add something that reloads it and gives me another 60 minutes? (Because that is for how long I want the computer to be running with out a screensaver at a time... Then you need to reload the program.)

 

EDIT: Also how do I add a visual interface? I have added a windows form to the project but I don't know how to make it run..

Spoiler

System:

i5 3570k @ 4.4 GHz, MSI Z77A-G43, Dominator Platinum 1600MHz 16GB (2x8GB), EVGA GTX 980ti 6GB, CM HAF XM, Samsung 850 Pro 256GB + Some WD Red HDD, Corsair RM850 80+ Gold, Asus Xonar Essence STX, Windows 10 Pro 64bit

PCPP:

http://pcpartpicker.com/p/znZqcf

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well at the moment it only is a run once type of program.  The program closes itself after 60 minutes, but that can be changed by modifying this line.  The program literally closes after 60 minutes...so you would have to reopen it if you wanted to extend the time :P

private const double HoldForXMinutes = 60;

If you want a visual interface then you have to create a new project and choose the winform option.  This will change the code a bit...you won't be able to copy and paste anymore...well you could, but you would have to only copy the functions (excluding the main) and build it into the winform.  If this is too confusing please let me know...it is morning and I might not be coherent

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×