小编Emi*_*ile的帖子

将CallerMemberName与params结合使用

现在(C#4.0)我们的日志记录方法看起来像

public void Log(string methodName, string messageFormat, params object[] messageParameters)
Run Code Online (Sandbox Code Playgroud)

记录器执行字符串格式化的位置,以便调用者没有放置String.Format来创建一个很好的日志消息(如果没有附加logviewer,则允许记录器跳过字符串格式).

使用c#5.0我想通过使用新的CallerMemberName属性来摆脱methodName参数,但我不知道如何将它与'params'关键字结合起来.有没有办法做到这一点?

c# string-formatting optional-parameters c#-5.0

26
推荐指数
3
解决办法
4557
查看次数

F#:如何优雅地选择和区分受歧视的工会?

说我有一个形状列表:

type shape = 
| Circle of float
| Rectangle of float * float

let a = [ Circle 5.0; Rectangle (4.0, 6.0)]
Run Code Online (Sandbox Code Playgroud)

我怎样才能测试一个圆圈是否存在?我可以为每个形状创建一个函数

let isCircle s = 
    match s with
    | Circle -> true
    | _ -> false
List.exists isCircle a
Run Code Online (Sandbox Code Playgroud)

但我觉得必须有F#更优雅的方式,不必定义这样的功能对于每个形状类型等.在那儿?

相关问题是如何根据形状类型对形状列表进行分组:

a |> seq.groupBy( <shapetype? >)
Run Code Online (Sandbox Code Playgroud)

f#

16
推荐指数
3
解决办法
2128
查看次数

在Bokeh应用程序中限制

我有一个带有Slider小部件的Bokeh应用程序,它使用Slider.on_change回调来更新我的图形.但是,滑块更新比我的回调函数可以处理得快得多,因此我需要一种方法来限制传入的更改请求.问题非常突出,因为滑块在滑动期间调用回调,而只有最后一个滑块值(当用户释放鼠标时)是有意义的.

我怎么能解决这个问题?

python throttling bokeh

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

为什么在全局变量的析构函数中调用 thread.join 会失败

我将有问题的代码简化为以下内容。我有一个 C 类,它在自己的线程上运行一个成员函数。在 CI 的析构函数中想要干净地退出这个线程。只要 c 在 main (1) 中定义,就可以正常工作,但当它是全局变量 (2) 时就不行了。在后一种情况下,我看到线程函数返回但 t.join() 挂起。

#include <mutex>
#include <condition_variable>
#include <thread>
#include <iostream>

using namespace std;

class C
{
public:
    C()
    {
        stop = false;
        t = thread(&C::ThreadFunc, this);
    }
    ~C()
    {
        stop = true;
        cv.notify_all();
        if (t.joinable())
        {
            cout << "joining" << endl;
            t.join();
            cout << "joined" << endl;
        }
    }

private:
    void ThreadFunc()
    {
        while (true)
        {
            unique_lock<mutex> lock(m);
            cv.wait(lock, [&]{return stop;});
            cout << "returning" << endl;
            return;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading destructor c++11

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

为什么boost :: scoped_ptr在继承场景中不起作用?

使用boost :: scoped_ptr来保存引用时,不会调用派生对象的析构函数.它在使用boost :: shared_ptr时会发生.

#include "stdafx.h"
#include <iostream>
#include "boost/scoped_ptr.hpp"
#include "boost/shared_ptr.hpp"

using namespace std;

class Base
{
public:
    Base() { cout << "Base constructor" << endl ; }
    ~Base() { cout << "Base destructor" << endl; }
};

class Derived : public Base
{
public:
    Derived() : Base() { cout << "Derived constructor" << endl; }
    ~Derived() { cout << "Derived destructor" << endl; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    boost::scoped_ptr<Base> pb;  // replacing by shared_ptr …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism inheritance boost

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