小编Oma*_*mar的帖子

WaitForExitAsync 超时

所以我注意到有一个名为WaitForExit接受和 int 作为参数(毫秒)的方法,所以如果进程无法自行退出,我会在几秒钟后杀死它。

像这样的东西。

if (!CMD.WaitForExit(3000))
    CMD.Kill();
Run Code Online (Sandbox Code Playgroud)

问题是,同时我想保存输出,所以我注意到有一个异步方法WaitForExitAsync,但这个方法不接受这些毫秒。

// Wait for exit async...
// Meanwhile save the output till it kills itself.

while (CMD.StandardOutput.ReadLine() != null) 
    standard_output = StandardOutput.ReadLine();
Run Code Online (Sandbox Code Playgroud)

知道如何做到这一点吗?谢谢!

c# cmd process

6
推荐指数
1
解决办法
2415
查看次数

这是什么类型的显式?

我过去几乎一直使用隐式类型,但由于有人告诉我使用关键字var是一种不好的做法,我现在尝试使用显式类型。为此,我倾向于使用快捷方式Ctrl+.将任何隐式类型更改为显式类型。

尽管如此,今天我不得不在一个 Foreach 中处理两个列表。为此,我使用了.Zip()方法。我尝试将其隐式类型更改为显式类型,但我不能。我也尝试使用我提到的快捷方式,但由于某种原因没有选项。

var test = enemyTeam.Zip(imageList, (c, i) => new { Champion = c, Image = i });
Run Code Online (Sandbox Code Playgroud)

Test1(隐式类型到 Int 类型):


在此处输入图片说明

Test2(隐式类型到显式类型):我不能。


在此处输入图片说明

在此处输入图片说明

我也尝试过这种隐式类型但没有用:

IEnumerable<new {  CurrentGameParticipant Champion, PictureBox Image }> test = enemyTeam.Zip(imageList, (c, i) => new { Champion = c, Image = i });
Run Code Online (Sandbox Code Playgroud)

c# types visual-studio

2
推荐指数
1
解决办法
81
查看次数

为什么将委托传递给函数而不是在函数内调用函数?

我试图理解委托的原因,但我发现很难看出将方法的引用传递给函数而不是调用内部函数有什么用处。

例如

class Program
{
    public static void Print(int num) 
        => Console.WriteLine(num);

    public static void Sum(int a, int b)
        => Print(a + b);
    
    public static void Sum(int a, int b, DelExample del) 
        => del(a + b);
    
    public delegate void DelExample(int num);

    static void Main(string[] args)
    {
        Sum(1, 2);

        DelExample del = Print;
        Sum(1,2, del);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意 Sum 及其重载的工作方式相同。有没有什么理由可以解释为什么在这个场景中使用 delegate 会比调用内部函数更好?是否有任何场景我需要委托,因为使用函数不会以相同的方式工作?我知道委托对于事件来说是必要的,但是什么时候它对于这种场景有用呢?

如果您能给我一些有关它的示例(或其他有用的用途,如事件、回调、线程),或者一些资源来理解它,就像我五岁一样,我也非常感激,因为我觉得我学得相当慢。提前致谢。

c# delegates

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

标签 统计

c# ×3

cmd ×1

delegates ×1

process ×1

types ×1

visual-studio ×1