Hi
Well i need suggestion how to approach .. i don't know i can or cant do this but let me try .
so share your thought process ..
My excat Requirement : need to create a windows or console application when i click on generated exe file it should ask time that will take to lock screen ?? .. if i give 60 mins the screen should not get locked till 60 min of given time
My best wishes
Have a nice day

VulpesPosted Jan 30, 2014, 10:02 AM
As C# allows you to call into the Win32 API, you should be able to do anything that can be done in a Windows scripting language. However, from Vista onwards, you'll probably need a policy change to be able to programatically override system settings such as this.
The functions in the Power Management API (PowrProf.dll) are documented here:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa373163(v=vs.85).aspx
However, I can't find any C# examples of how to change this particular setting and it's not immediately apparent how to do so.
A better idea may be to allow the tester to temporarily prevent the system from going to sleep by calling the SetThreadExecutionState function with ES_DISPLAY_REQUIRED. See:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa373233(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx
SUNIL GUTTAPosted Jan 29, 2014, 11:51 PM
I guess this thing is achieved using some other scripting lang someone said me ..
So can i say C# libraries have no such strength to achieve this task but other things may do it .
VulpesPosted Jan 29, 2014, 7:34 PM
The code given above won't lengthen that time but it may shorten it, so it's not what you want.
Rather than trying to do anything programatically, I'd have thought it would be better if individual testers could change the setting on their machine to a value they're happy with.
On Windows 7, it can be set from Control Panel -> All Control Panel Options -> Power Options -> Change Plan Settings. In the case of a laptop there are two settings depending on whether the machine is running on battery or mains power.
SUNIL GUTTAPosted Jan 29, 2014, 2:23 PM
This application just need to avoid that , so tester can keep the system idle and run this created application with setting time only to specify how many minutes after the lock should be applied till then LOCK should not be applied ..
I hope i am clear . any clarity i will try to help up :)
Anyways the code given by you looks complex enough to understand but will try to crack at it .
and you said
// rest of form code
it means like a message like thing or can i design a count down timer specifying HOW many minutes to lock ?? ?? I hope this looks cool right ? and also cancel option to abort the process ..
Ty so so much
VulpesPosted Jan 28, 2014, 4:08 PM
You'll need to add a reference to your project to Microsoft.VisualBasic.dll as we're going to use VB.NET's InputBox function.
First of all make the highlighted changes to program.cs:
using VBI = Microsoft.VisualBasic.Interaction;
// ...
static class Program
{
internal static int MinsBeforeLock;
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string prompt = "How many minutes before screen is locked?";
string title = "Set Time Before Screen Locked";
string defResponse = "60";
object result = VBI.InputBox(prompt, title, defResponse);
int.TryParse(result.ToString(), out MinsBeforeLock);
Application.Run(new Form1());
}
}
Now, add a Timer to your main form (Form1 say), double click on it to create a Tick event handler, double click on the Form to create a Load event handler and add the following code:
using System.Runtime.InteropServices;
// ...
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern bool LockWorkStation();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int mins = Program.MinsBeforeLock;
if (mins < 1)
{
MessageBox.Show( "Invalid response. Default delay of 60 minutes is being used", "Set Time To Lock Screen - ERROR!");
mins = 60;
}
timer1.Interval = mins * 60 * 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
bool isLocked = LockWorkStation();
if (!isLocked)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
// rest of form code
}
Now, as the code stands, the screen will only be locked if the application is still running at the appointed time. If the user closes it before then, the screen will not be locked.
Not sure whether this is exactly what you want but it's a start :)