有时我需要创建构造函数需要很长时间才能执行的对象.这导致UI应用程序中的响应性问题.
所以我想知道编写一个被设计为异步调用的构造函数是否合理,方法是将回调函数传递给它,这会在对象可用时提醒我.
以下是示例代码:
class C
{
public:
// Standard ctor
C()
{
init();
}
// Designed for async ctor
C(std::function<void(void)> callback)
{
init();
callback();
}
private:
void init() // Should be replaced by delegating costructor (not yet supported by my compiler)
{
std::chrono::seconds s(2);
std::this_thread::sleep_for(s);
std::cout << "Object created" << std::endl;
}
};
int main(int argc, char* argv[])
{
auto msgQueue = std::queue<char>();
std::mutex m;
std::condition_variable cv;
auto notified = false;
// Some parallel task
auto f = []()
{ …Run Code Online (Sandbox Code Playgroud) 我有一个 DBus 服务器,它公开了一个需要大量时间才能完成的方法(大约 3 分钟)。客户端对此方法执行同步调用。
问题是,正好 25 秒后,客户端抛出错误,因为“没有收到回复”。
不幸的是,我无法更改客户端,因此我无法使调用异步,因为它应该是。
我试图在我的服务器配置中使用这一行:
<limit name = "reply_timeout">240000</limit>
Run Code Online (Sandbox Code Playgroud)
但情况没有改变。
任何的想法?
使用autotools,我需要在输入make install时创建一个空目录树,如:
/etc/myprg/
|-- foo
Run Code Online (Sandbox Code Playgroud)
现在,我通过指定空目标来完成此操作,如下所示:
myprgdir = $(sysconfdir)/myprg/
myprgfoodir = $(sysconfdir)/myprg/foo
Run Code Online (Sandbox Code Playgroud)
然后
dist_myprg_DATA =
dist_myprgfoo_DATA =
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否有更好的方法来做这样的事情!
我正在使用流畅的界面模式.
首先,我写了类似的东西:
class C
{
public:
C() { }
C* inParam1(int arg1){ param1 = arg1; return this; }
C* inParam2(int arg2){ param2 = arg2; return this; }
private:
int param1;
int param2;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用std :: unique_ptr,但后来我意识到我不知道如何沿着链"移动"指针(this).我尝试过类似的东西:
return std::move(this);
Run Code Online (Sandbox Code Playgroud)
那当然不行.
我怎样才能做到这一点?做这样的事有什么问题吗?
为了回复如下评论:"不要使用指针":没有(还)任何实际原因,因为我用指针做这个,只是我想知道是否可以这样做.
我在网上浏览了一个简化的键盘,用于在Windows Mobile上运行的项目,但我找不到任何好东西,所以我试着自己创建一个.
遇到的基本问题,与用于实现"密钥"的控件有关,是:我需要一个控件: - 可点击 - 可以包含文本 - 不要采取焦点
我做了一些尝试,我发现LinkLabel似乎正是我所需要的:它有效.
现在,问题是:使用LinkLabel实现触摸屏键盘是一个好主意,还是这是错误的做法?我想避免使用这个键盘开发所有应用程序,然后在"太晚"时发现一些麻烦.
是否有可能在单例模式后创建模态对话框?
这个想法是:
public partial class Singleton : Form
{
private static Singleton _instance = null;
private Singleton()
{
// Initialization code
}
public static Singleton Instance
{
get
{
if (_instance == null)
_instance = new Singleton();
return _instance;
}
}
private void Singleton_FormClosing(object sender, FormClosingEventArgs e)
{
_instance.Hide();
e.Cancel = true;
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
如果表单是非模态的(因此,如果使用Show()方法),此代码可以正常工作,但如果表单是模态的(因此,如果使用ShowDialog()方法)则不起作用,因为这也会隐藏父表格.
我想做点什么
Action<FileInfo> deleter = f =>
{
if (....) // delete condition here
{
System.IO.File.Delete(f.FullName);
}
};
DirectoryInfo di = new DirectoryInfo(_path);
di.GetFiles("*.pdf").Select(deleter); // <= Does not compile!
di.GetFiles("*.txt").Select(deleter); // <= Does not compile!
di.GetFiles("*.dat").Select(deleter); // <= Does not compile!
Run Code Online (Sandbox Code Playgroud)
为了从目录中删除旧文件.但我不知道如何直接将委托应用于FilInfo []而没有明确的foreach(上面列出的想法当然不起作用).
可能吗?
c# ×3
c++ ×2
c++11 ×2
asynchronous ×1
automake ×1
autotools ×1
c ×1
c-api ×1
constructor ×1
dbus ×1
glib ×1
linq ×1
unique-ptr ×1