简体   繁体   English

如何在C#Windows应用程序中5分钟后自动创建密钥

[英]how to create secret key automatically after 5 minute in C# windows application

I create one function in Class file: 我在Class文件中创建一个函数:

public string createprivacykey(string s3)
{
     prky = Convert.ToString(s3);
     return prky;
}

and function call in Windows form on button click event: 按钮单击事件时,以Windows形式调用和函数:

private void button1_Click(object sender, EventArgs e)
{
    string clearText = textBox2.Text.Trim();
    string ciphertext=Class1.Encrypt(clearText,true);
    // txtDecryptedText.Visible = false;
    //label3.Visible = false;
    textBox2.Text = ciphertext;

    label3.Visible = true;
    label5.Visible = true;
    label4.Visible = true;

    Random val = new Random();
    int randomnumber1 = val.Next(1001, 50000);
    ra = Convert.ToString(randomnumber1);

    str = cs.createprivacykey(ra);
    label5.Text = str.ToString();
}

but i want to key regenerate automatically after 5 minute how to generate key 但是我想在5分钟后自动重新生成密钥

Add a timer to your form using the designer: 使用设计器将计时器添加到表单中:

  • Toolbox - Components - Timer 工具箱-组件-计时器
  • Drag the time to your form 将时间拖到表格上
  • It will appear on the bottom of the designer 它将出现在设计师的底部
  • Right Click on it and select properties 右键单击它并选择属性
  • Timer interval: the number of milliseconds in 5 minutes 计时器间隔:5分钟内的毫秒数
  • Enabled: true if you want it to be enabled as soon as the from is created 已启用:如果您希望在创建from之后立即启用它,则为true
  • Properties - Events - Tick: the procedure that will be called about every 5 minutes. 属性-事件-勾号:大约每5分钟调用一次。

Some improvements: 一些改进:

  • Fill the time interval in the constructor: TimeSpan.FromSeconds(5).ToMilliSeconds; 在构造函数中填充时间间隔:TimeSpan.FromSeconds(5).ToMilliSeconds;
  • Enable the timer when the form is shown. 显示表单时启用计时器。 This case you're guaranteed that your form is initialized the first time your timer ticks. 在这种情况下,可以确保您的计时器在第一次计时时被初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM