小编zai*_*aqi的帖子

在.Net C中释放资源#

我是C#和.NET的新手,并且一直在阅读它.

我需要知道为什么以及何时需要释放资源?垃圾收集器不能处理所有事情吗?我什么时候需要实现IDisposable,它与C++中的析构函数有什么不同?

另外,如果我的程序相当小,即屏幕保护程序,我是否需要关心释放资源?

谢谢.

.net c# resources memory-leaks

13
推荐指数
2
解决办法
8179
查看次数

仅使用Timer一次

我想在主表单初始化后的1秒钟内只使用一次计时器.我认为以下会有一个消息框说"Hello World"只有一次,但实际上一个新的消息框每隔一秒就会显示"Hello World".

为什么这样?我t.Stop()参加了嘀嗒事件.另外,我是否需要以某种方式处理计时器以避免内存泄漏?

        Timer t = new Timer();
        t.Interval = 1000;                
        t.Tick += delegate(System.Object o, System.EventArgs e)
                        { MessageBox.Show("Hello World"); t.Stop(); };

        t.Start();   
Run Code Online (Sandbox Code Playgroud)

请帮助并显示是否有更好的方法来做到这一点?谢谢.

c# timer winforms

8
推荐指数
1
解决办法
8911
查看次数

一个创建另一个程序的程序

我需要创建一个程序来创建另一个程序但不是编译器.

例如,

我编写了一个接受用户输入字符串的程序.假设用户输入"Pluto".然后,该程序应该创建一个单独的.exe,在执行时会显示"Hello Pluto".

我怎样才能做到这一点?如果您可以在C#和Windows Forms中提供示例,那就更好了.

谢谢.

.net c#

6
推荐指数
2
解决办法
981
查看次数

在事件发生时注册要调用的方法的方法

我有一个包含20个PictureBox控件的Panel.如果用户点击任何控件,我希望调用Panel中的方法.

我该怎么做呢?

public class MyPanel : Panel
{
   public MyPanel()
   {
      for(int i = 0; i < 20; i++)
      {
         Controls.Add(new PictureBox());
      }
   }

   // DOESN'T WORK.
   // function to register functions to be called if the pictureboxes are clicked.
   public void RegisterFunction( <function pointer> func )
   {
        foreach ( Control c in Controls )
        {
             c.Click += new EventHandler( func );
        }
   }
}
Run Code Online (Sandbox Code Playgroud)

我该如何实施RegisterFunction()?此外,如果有很酷的C#功能可以使代码更优雅,请分享.

c# winforms

3
推荐指数
1
解决办法
2083
查看次数

Terraform 仅运行多部分用户数据的第一部分

我正在考虑使用 Terraform 设置虚拟机基础设施,并使用 cloud-init 启动虚拟机。我正在尝试使用cloud-config内容类型作为第一部分和shellscript第二部分的多部分方法。

但是,在 上terraform apply,仅执行第一部分,而第二部分未运行(通过检查生成的 VM 是否已应用更改)。

相关配置部分如下。

data "template_cloudinit_config" "config" {
  gzip = true
  base64_encode = true
  part {
      filename = "init-cloud-config"
      content_type = "text/cloud-config"
      content = file("../modules/services/${var.service_name}/init.yaml")
  }
  part {
      filename = "init-shellscript"
      content_type = "text/x-shellscript"
      content = templatefile("../modules/services/${var.service_name}/init.sh",
        { hostname = "${var.prefix}-${var.service_name}" }
      )
  }
}
Run Code Online (Sandbox Code Playgroud)

cloud-init init.yaml 内容如下

groups:
  - dataops

users:
  - default
  - name: dataops
    gecos: Data Operations
    shell: /bin/bash
    primary_group: dataops
    sudo: ALL=(ALL) NOPASSWD:ALL …
Run Code Online (Sandbox Code Playgroud)

cloud-init terraform

3
推荐指数
1
解决办法
2124
查看次数

标签 统计

c# ×4

.net ×2

winforms ×2

cloud-init ×1

memory-leaks ×1

resources ×1

terraform ×1

timer ×1