how to run a particular part of your application at a defined time/interval in c#

KwebenaAcquah-9104 326 Reputation points
2021-07-06T23:20:20.027+00:00

please i am having a little probleme;

I am having Two windows in my application;
ONE; is the main wpf windows;
TWO; is the product key registration windows;
i would like to use date time interval to validate the runing of the window TWO;

like from the first (1st of every month in a (any) year to the end of that same month let the windows TWO show up preventing the user from accessing the main windows ONE; ) i don't realy know if this is possible if it is can someone help me out please

UPDATE OF INTENTION;
yes that is what i want; but i searched right round the internet i didn't get the one of my choice in this terms; so i made a window that i want to run every 30day; this windows runs having just two textboxes
TEXTBOX ONE; that which brings out the COMPUTER ID of the user;
TEXTBOX TWO; that which will take the PASSKEY which i will provide when a user reads to me his COMPUTER ID(this i can give the user a PASSKEY that will be valid for just 30day or 31days. this is what i am doing but i don't actually know if that is the best and strong way of protecting my software help me if you know of any other way please;
(preventing the user to run application after every 30days or 31days; however the first month of download i want it to be free to the user after which that user will start paying for the product key which i will provide him)(and when the user tries to redownload the software the software will surely request for the product key even on the same machine please some should help me accomplish this;

code behind WINDOW TWO;

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                txtproductid.Text = ComputerInfo.GetComputerId();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnreg_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                KeyManager km = new KeyManager(txtproductid.Text);
                string productkey = txtproductkey.Text;
                if (km.ValidKey(ref productkey))
                {
                    KeyValuesClass kv = new KeyValuesClass();
                    if (km.DisassembleKey(productkey, ref kv))
                    {
                        LicenseInfo lic = new LicenseInfo();
                        lic.ProductKey = productkey;
                        lic.FullName = "KSMS";
                        if (kv.Type == LicenseType.TRIAL)
                        {
                            lic.Day = kv.Expiration.Day;
                            lic.Month = kv.Expiration.Month;
                            lic.Year = kv.Expiration.Year;
                        }
                        km.SaveSuretyFile(string.Format(@"{0}\key.lic", Application.Current.StartupUri), lic);
                        MessageBox.Show("You Have Been Successfully Registered", "KSMS MESSAGE", MessageBoxButton.OK, MessageBoxImage.Information);
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("The Applied Product Key is Invalid", "KSMS MESSAGE", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

here is what i have tried; (maybe i don't understand myself here help me)

 public void LoopMonths(string startM, string endM)
        {
            startM = DateTime.Now.ToLongDateString(); //my start date in any month from any year
            endM = "2018-11";                                          // my end date in any month from any year(this may be unspecified maybe till eternity comes)

            int year = int.Parse(startM.Split('-')[0]);
            int month = int.Parse(startM.Split('-')[1]);
            int endYear = int.Parse(endM.Split('-')[0]);
            int endMonth = int.Parse(endM.Split('-')[1]);

            while (year < endYear || (year == endYear && month < endMonth))
            {
                // do something ... 
                month++;
                if (month == 06)
                {
                    year++;
                    month = 1;
                }
            }
        }
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-07-07T03:12:59.797+00:00

    Do you want to store the start time in the database when the user registers, and then verify the interval between the start time and the current time each time you open the application?

    How about like this:

    Window1(Register window):

        <Grid>  
            <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="260,90,0,0" TextWrapping="Wrap" Text="Register Window" VerticalAlignment="Top" Height="40" Width="183" FontSize="22"/>  
            <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="52" Margin="215,172,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="289"/>  
            <Button x:Name="RegisterButton" Content="Register" HorizontalAlignment="Left" Margin="220,288,0,0" VerticalAlignment="Top" Width="95" Height="46" Click="RegisterButton_Click"/>  
            <Button x:Name="CancleButton" Content="Cancle" HorizontalAlignment="Left" Margin="356,288,0,0" VerticalAlignment="Top" Width="110" Height="47" Click="CancleButton_Click"/>  
        </Grid>  
    

    C# code:

        public partial class Window1 : Window  
        {  
            public bool IsRegestered { get; set; }  
            public Window1()  
            {  
                InitializeComponent();  
            }  
      
            private void RegisterButton_Click(object sender, RoutedEventArgs e)  
            {  
                if (textBox.Text == "Pass")  
                {  
                    IsRegestered = true;  
                    this.Close();  
                }  
                else  
                {  
                    MessageBox.Show("Wrong Register Key");  
                }  
      
            }  
      
            private void CancleButton_Click(object sender, RoutedEventArgs e)  
            {  
                this.Close();  
            }  
    

    C# code in MainWindow:

            public MainWindow()  
            {  
                InitializeComponent();  
                DateTime dateTime = new DateTime(2021, 7, 7, 10, 16, 00);  
                if (DateTime.Now - dateTime > TimeSpan.FromMinutes(3))  
                {  
                    Window1 form2 = new Window1();  
                    form2.ShowDialog();  
                    if (!form2.IsRegestered)  
                    {  
                        this.Close();  
                    }  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-07-07T04:25:33.79+00:00

    Are you saying you need to determine if the user has a valid license for the software and if they do then allow them to use the application (the main window) and if they do not then do not allow them to use the application? Are you sure you must use a form (window) to do that? You can do the validation prior to showing any window and if the license is not current then you can show a window that says it is not valid and give them the opportunity to make it valid (pay or whatever).

    There are many licensing systems that exist that would be better than you are likely able to develop. There is even something in .Net; see How to: License Components and Controls. There is an article about it at Applications Licensing using the .NET Framework | Developer.com. That article is about Windows Forms but it can be used in WPF too. Also see Best 20 NuGet license Packages - NuGet Must Haves Package but I do not know anything about any of those except what that page says.

    Also see the Application.Startup Event. That would be a good place to check the license.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.