|
Demo Software |
|
|
|
In our program we have kept a limit on number of days as well as time. The Logic used is as follows: When we run our application for the first time, we add a registry entry in the HKEY_CURRENT_USER | Control Panel. Next we add a key for application along with two values, one for maintaining the number of executions and another for maintaining the count on time. Next time when we open the application it checks these values and accordingly allows or disallows the program to execute. Our program should work for a maximum of 5 hrs or can be executed only 50 times. Every time we start the application a ‘staring form’ is displayed which indicates the time expired, time remaining, number of times the application was executed, and remaining number of times it can be executed. To keep a count on time, we have added a timer control in our application. We changed its name from timer1 to mytimer.
Here we have added 9 lables, 4 for displaying the static text and four for displaying the time expired, time left, Executions completed, and executions remaining. We have named them as texp, tleft, execom, and exerem respectively. The last label is used to display a message besides the start button. The start button indicates that the actual application will start, the earlier form is just a starting indication, and it does not start the real application. When we click the start button, the timer starts and number of executions increases by one. But before this we have to add in registry entries. We plan to add our key in the HKEY_CURRENT_USER | Control Panel. We can add this somewhere else also. We have added the keys in the constructor of the form. We have added the following data members to the form RegistryKey top, r, demo ; In top we will have the RegistryKey instance of HKEY_CURRENT_USER key. In r we will have instance of Control panel key and in demo we will have the key of our application. min will contain a count of minutes, numexe will contain a count of number of executions and timercounts will contain a count on milliseconds. public Form1( ) InitializeComponent ( ) ; demo = r.CreateSubKey ( "softdemo" ) ; } min = ( int ) demo.GetValue ( "Minutes" ) ; } texp.Text = min / 60 + " hours " + min % msg.Text = "Trail Version expired" ; } } First we have collected the top-most RegistryKey in top using the Registry.CurrentUser field of the Registry class. Next we have initialized r with the key of Control panel using the OpenSubKey( ) method. Then we have initialized demo with the key of our application. If we get a null in demo, it means that this is the first time the application is running. In this case we have added it as a new sub-key using the CreateSubKey( ) method. To this key we have added two values Minutes and Executions . We have set both the data fields to 0 using the SetValue( ) method. If demo does not contain a null it means that the application is not running for the first time and keys and values already exists. So we get the value using the GetValue( ) method. Next we have set all the text fields of the labels to the appropriate values by calculating the time in hours and minutes from the extracted key values. If the extracted Minutes happen to be more that 300 (5 hours) or the number of executions happens to be more that 50, we disable the start button and flash an appropriate message. Now lets see how to keep a count on time. The timer starts when we click the Start button: private void start_Click (object sender, System.EventArgs e) numexe++ ; } When we press ‘Start’ the number of executions is increased and the new value is set. We have added the Tick( ) handler with a time interval of 100 milliseconds. This means after every 100 milliseconds this event will be fired. We can change the value is the properties window. This is the place where we can write the logic of our actual application. private void mytimer_Tick ( object sender, System.EventArgs e ) if ( ++timercounts >= 600 ) min++ ; } if ( min >= 300 ) mytimer.Stop ( ) ; } }
protected override void Dispose( bool disposing ) top.Close ( ) ;
if ( disposing ) if ( components != null ) components.Dispose ( ) ; } }
MessageBox.Show ( "You can no more use this software, time elapsed!!!!","Sorry" ) ; }
Here we have checked the
min and numexe and if they have exceeded the specified limit we have displayed a dialog box with an appropriate message. |