小编kno*_*cte的帖子

bash:在while循环中反击(kill和kill -9)

所以我最近了解到kill不是同步命令,所以我在bash中使用while循环,这很棒:

while kill PID_OF_THE_PROCESS 2>/dev/null; do sleep 1; done
Run Code Online (Sandbox Code Playgroud)

但是,有些情况(非常罕见,但它们仍然会发生),其中进程被卡住,并且它不会对kill信号起作用.在这些情况下,杀死应用程序的唯一方法是使用" kill -9 ".

所以我想知道,如果循环已达到第10次迭代,如何在bash中修改上面的while循环以使用-9参数?

bash scripting kill while-loop kill-process

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

F#:只为第一个事件做一次动作,没有可变性/锁定?

我有这个代码下载文件,并在控制台告诉我文件有多大:

use webClient = new WebClient()

let lockObj = new Object()
let mutable firstProgressEvent = true
let onProgress (progressEventArgs: DownloadProgressChangedEventArgs) =
    lock lockObj (fun _->
        if (firstProgressEvent) then
            let totalSizeInMB = progressEventArgs.TotalBytesToReceive / 1000000L
            Console.WriteLine ("Starting download of {0}MB...", totalSizeInMB)
        firstProgressEvent <- false
    )

webClient.DownloadProgressChanged.Subscribe onProgress |> ignore
let task = webClient.DownloadFileTaskAsync (uri, Path.GetFileName(uri.LocalPath))
task.Wait()
Run Code Online (Sandbox Code Playgroud)

有没有办法做同样的事情,但既不使用锁定也不使用可变的变量?

f# locking webclient immutability

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

为什么F#的默认set集合排序而C#的默认集合没有排序?

从C#世界迁移到F#(最惯用的)思维方式时,我发现了这种有趣的区别。

在C#的OOP&mutable世界中,默认的set集合似乎是HashSet,它似乎不是默认排序的(因为它接受的比较器只是为了相等)。而如果要排序的话,则必须使用SortedSet

但是,在F#的世界中,基本set已经被排序,因为它需要用于实现相等比较的元素类型。有什么具体原因吗?为什么在该语言的主要集合中没有无序集合?

作为附带说明,我想知道是否有可能有一个不允许重复的set集合,但是当丢弃某些元素作为重复项时,它优先于某些元素。示例:一条记录​​,{ Name: string; Flag: Option<unit> }以便在插入时{ Name = "foo"; Flag = None }稍后{ Name = "foo"; Flag = Some() }它最终仅包含后一个元素(因为存在Flag)。

.net c# f# set c#-to-f#

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

是否可以使用Mono运行clickonce应用程序?

我正在开发clickonce应用程序,我希望在Mac平台上发布.我知道我可以使用Mono编译.Net应用程序 - 它可以在Mac上运行.但是我可以使用clickonce作为安装程序吗?

谢谢!

mono clickonce c#-4.0

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

F#`try`-`with`块内的转换和异常过滤器

我正在尝试将这段C#转换为F#:

var webClient = new WebClient();
try {
    webClient.DownloadString (url);
} catch (WebException e) {
    var response = e.Response as HttpWebResponse;
    if (response == null)
        throw;
    using (response) {
        using (Stream data = response.GetResponseStream ()) {
            string text = new StreamReader (data).ReadToEnd ();
            throw new Exception (response.StatusCode + ": " + text);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在已经想出了这个,但是它没有编译,因为我显然不能使用let或者usewith块中:

let Download(url: string) =
    use webClient = new WebClient ()
    try
        webClient.DownloadString(url)
    with
        | :? WebException as ex …
Run Code Online (Sandbox Code Playgroud)

c# f# exception-handling c#-to-f#

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

用F#语言覆盖C#类中的方法会使该方法无法内省吗?

我有以下C#类:

public class Foo {
    protected virtual void Bar (){
    }
}

public class Baz : Foo {
    protected override void Bar (){
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我反省它们,方法Bar总是在那里:

[<EntryPoint>]
let main args =
    let methodFromFoo = typedefof<Foo>.GetMethod("Bar", BindingFlags.Instance ||| BindingFlags.NonPublic)
    if (methodFromFoo <> null) then
        Console.WriteLine ("methodFromFoo is not null")
    else
        Console.WriteLine ("methodFromFoo is null")

    let methodFromBaz = typedefof<Baz>.GetMethod("Bar", BindingFlags.Instance ||| BindingFlags.NonPublic)
    if (methodFromBaz <> null) then
        Console.WriteLine ("methodFromBaz is not null")
    else
        Console.WriteLine ("methodFromBaz is null")
Run Code Online (Sandbox Code Playgroud)

结果是这is not null两种情况. …

c# reflection f# system.reflection c#-to-f#

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

通过F#代码迭代插入ASP.NET MVC4页面的ViewData中的字典?(剃刀)

叫我疯了,但我正在用F#开发一个ASP.NET MVC4站点.这是我的第一个障碍; 在控制器中我提交字典,这样:

[<HandleError>]
type FooController() =
    inherit Controller()
    member this.Index () =
        this.ViewData.Add("Foo", "Bar")
        this.ViewData.Add("Baz", dict [ (0, 0); (1, 3); (2, 2); ] )
        this.View() :> ActionResult
Run Code Online (Sandbox Code Playgroud)

现在我想迭代Razor模板中的值,该怎么做?我试过这个:

@foreach (var time in @ViewBag.Baz.Keys)
{
    <hr /><p>Time is @time, and value is @ViewBag.Baz[time]</p>
}
Run Code Online (Sandbox Code Playgroud)

但它抛出了这个错误:

Line 119:                    Total: <span class="highlight">6449</span> kW/h.
Line 120:                </p>
Line 121:                @foreach (var time in (@ViewBag.Baz).Keys)
Line 122:                {
Line 123:                    <hr /><p>Time is @time, and value is @ViewBag.Baz[time]</p>

[RuntimeBinderException: 'object' does not contain …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc f# razor asp.net-mvc-4

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

公共记录类型的构造函数?

假设我想要一个记录类型,例如:

type CounterValues = { Values: (int) list; IsCorrupt: bool }
Run Code Online (Sandbox Code Playgroud)

问题是,我想创建一个构造函数,将整数传递的列表转换为没有负值的新列表(它们将被0替换),并且只有在构造时发现负值时才有IsCorrupt = true .

这可能与F#有关吗?

现在,这就是我所做的,使用属性(但是,它,它不是非常F#-ish,它每次都在getter上调用ConvertAllNegativeValuesToZeroes(),所以效率不高):

type CounterValues
    (values: (int) list) =

    static member private AnyNegativeValues
        (values: (int) list)
        : bool =
            match values with
            | v::t -> (v < 0) || CounterValues.AnyNegativeValues(t)
            | [] -> false

    static member private ConvertAllNegativeValuesToZeroes
        (values: (int) list)
        : (int) list =
            match values with
            | [] -> []
            | v::t ->
                if (v < 0) then
                    0::CounterValues.ConvertAllNegativeValuesToZeroes(t)
                else
                    v::CounterValues.ConvertAllNegativeValuesToZeroes(t)

    member this.IsCorrupt = …
Run Code Online (Sandbox Code Playgroud)

f# constructor types record

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

在C#中将void转换为异步返回

我读到void从C#async调用返回并不好.但我有以下情况:

public async void MainFunction()
{
    await DoSomething()
    await DoSomethingMore()
}

public void DoSomething() 
{
    //some code that I want to execute (fire and forget)
}

public void DoSomethingMore()
{
    //some code that I want to execute (fire and forget)
}
Run Code Online (Sandbox Code Playgroud)

因为我只想要执行该函数而根本没有返回.我应该这样保留它,还是应该Task从DoSomething()返回?如果我将其更改为返回Task,因为我的代码根本不需要返回任何内容,我应该返回什么?

c# async-await

4
推荐指数
2
解决办法
2194
查看次数

通过从C#调用非异步Func <T>,返回F#函数中的异步<T>?

假设我想从F#调用这个C#函数:

public static class Foo {
    public Bar Baz()
    {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是这个功能是CPU密集型的,我不想阻止它.不幸的是,C#库没有Task<Bar> BazAsync()过载.

然后我想通过创建一个调用它并返回(已经启动)的F#函数来自己提供异步版本Async<Bar>.也就是说,我不想用System.Threading.Task<Bar>.

我想我正在寻找的东西相当于Task<T>.Run()F#Async的做事方式.

我已经看过以下选项:

  • Async.StartAsTask - >处理C#ish任务类型.
  • Async.StartAsync.StartImmediately- >收到Async<unit>Async<T>

Async.StartChild我在寻找什么呢?如果是,那将是:

let BazWrapper() =
    let asyncTask: Async<Bar> = async {
        return Foo.Bar()
    }
    Async.StartChild asyncTask
Run Code Online (Sandbox Code Playgroud)

但是,如果以上是解决方案:

  1. 为什么围绕F#异步工作流的大多数文档都没有提到StartChild?
  2. 为什么BazWrapper会返回Async<Async<Bar>>而不是Async<Bar>

f# asynchronous computation-expression c#-to-f#

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