Tag: C#的

Async在linq select中等待

我需要修改一个现有的程序,它包含以下代码: var inputs = events.Select(async ev => await ProcessEventAsync(ev)) .Select(t => t.Result) .Where(i => i != null) .ToList(); 但是这对我来说似乎很奇怪,首先在select中使用async和await 。 根据Stephen Cleary的这个答案 ,我应该可以放弃这些。 然后Select第二个Select结果。 这是不是意味着这个任务不是asynchronous的,并且是同步执行的(这么多努力),或者这个任务是asynchronous执行的,而当它完成时,执行其余的查询。 我应该按照斯蒂芬·克里里的另一个答案来写下面的代码: var tasks = await Task.WhenAll(events.Select(ev => ProcessEventAsync(ev))); var inputs = tasks.Where(result => result != null).ToList(); 这是完全一样的吗? var inputs = (await Task.WhenAll(events.Select(ev => ProcessEventAsync(ev)))) .Where(result => result != null).ToList(); 虽然我在这个项目上工作,但我想改变第一个代码示例,但我并不太热衷于改变(exception工作)的asynchronous代码。 […]

需要用户反馈的ASP.NET MVC长时间运行过程

我一直在试图在我的项目中创build一个控制器来实现可能是相当复杂的报告。 因此,他们可能需要相当长的时间,进度条肯定会帮助用户知道事情正在进行。 该报告将通过AJAX请求启动,周期性的JSON请求将获得状态并更新进度条。 我一直在尝试AsyncController,因为这似乎是一个很好的方式来运行长进程而不捆绑资源,但它似乎没有给我任何方式来检查进度(似乎阻止进一步的JSON请求和我还没有发现为什么)。 之后,我尝试着在控制器上的静态variables中存储进度,并从中读取状态 – 但说实话,这似乎有点哈克! 所有的build议感激地接受!

end iterator是否可移植性递减?

刚刚在我公司的源代码中遇到了end()迭代器的递减,这对我来说看起来很奇怪。 据我记忆,这是在一些平台上工作,但不为其他人。 也许我错了,但是我找不到任何有用的标准。 标准只说end()返回一个迭代器,它是过去的最终值,但是它保证是可减less的? 这样的代码如何符合标准? std::list<int>::iterator it = –l.end(); 提前致谢。

如何将一个lambdaexpression式存储为C ++ 11中的一个类的字段?

我想创build一个类,客户端可以存储一个类似于[]() -> void {}的lambdaexpression式作为类的字段,但我不知道如何去做。 一个答案build议使用decltype ,我试过没有成功。 这里是一个ideone源代码链接 。 以下是来源和结果: #include <cstdio> auto voidLambda = []()->void{}; class MyClass { public: decltype(voidLambda) t; MyClass(decltype(voidLambda) t) { this->t = t; } }; int main() { MyClass([] { printf("hi"); }); } 结果: prog.cpp: In constructor 'MyClass::MyClass(<lambda()>)': prog.cpp:3:79: error: no matching function for call to '<lambda()>::__lambda0()' prog.cpp:2:20: note: candidates are: […]

调用mmap之后,是否需要打开文件?

我有一个程序,映射不less(100)的大文件每个10-100MB。 我需要他们全部映射在同一时间。 目前我open电话,然后在节目开始时按mmap ,然后是munmap ,最后close 。 通常我必须在运行程序之前调整运行ulimit -n的打开文件限制。 问题是我真的需要保持文件打开,或者我可以open mmap close做一些大数据处理,然后munmap当我完成。 这个mmap的man页面对我来说似乎不太清楚。

在C#中访问简单的布尔标志时,是否需要locking或标记为volatile?

让我们只是说你有一个简单的操作在后台线程上运行。 您想要提供取消此操作的方法,以便您创build一个布尔标志,您可以通过取消button的单击事件处理程序将其设置为true。 private bool _cancelled; private void CancelButton_Click(Object sender ClickEventArgs e) { _cancelled = true; } 现在,您正在从GUI线程设置取消标志,但是您正在从后台线程读取它。 访问布尔之前,你需要locking吗? 你需要这样做(显然locking在button单击事件处理程序): while(operationNotComplete) { // Do complex operation lock(_lockObject) { if(_cancelled) { break; } } } 还是可以接受这样做(没有locking): while(!_cancelled & operationNotComplete) { // Do complex operation } 或者如何将_cancelledvariables标记为volatile。 这是必要的吗? [我知道有BackgroundWorker类与它内置的CancelAsync()方法,但我对这里的locking和线程variables访问的语义和使用感兴趣,而不是具体的实现,代码只是一个例子。 似乎有两个理论。 1)因为它是一个简单的内置types(并且访问内置types在.net中是primefaces的),并且因为我们只是在一个地方写信给它,只能在后台线程上读取,所以不需要locking或标记为volatile。 2)你应该把它标记为volatile,因为如果你没有编译器可以优化while循环中的读取,因为它认为没有什么能够修改值。 哪一个是正确的技术? (为什么?) [编辑:这似乎有两个明确界定和反对的思想stream派。 我正在寻找一个明确的答案,所以请尽可能张贴您的理由,并引用您的来源与您的答案。

我可以从当前正在执行的函数中程序地获取参数名称/值吗?

我想要做这样的事情: public MyFunction(int integerParameter, string stringParameter){ //Do this: LogParameters(); //Instead of this: //Log.Debug("integerParameter: " + integerParameter + // ", stringParameter: " + stringParameter); } public LogParameters(){ //Look up 1 level in the call stack (if possible), //Programmatically loop through the function's parameters/values //and log them to a file (with the function name as well). //If […]

std :: vector :: swap invalidate迭代器吗?

如果我交换两个向量,它们的迭代器是否仍然有效,现在只是指向“其他”容器,或迭代器是否失效? 也就是说,给出: using namespace std; vector<int> x(42, 42); vector<int> y; vector<int>::iterator a = x.begin(); vector<int>::iterator b = x.end(); x.swap(y); // a and b still valid? Pointing to x or y? 这似乎没有提到这一点: [n3092 – 23.3.6.2] void swap(vector<T,Allocator>& x); 效果:将* this的内容和容量()与x的内容和容量()交换。 请注意,因为我在VS 2005中,我也对迭代器debugging检查等的影响感兴趣(_SECURE_SCL)

为什么我观察多重inheritance比单一更快?

我有以下两个文件: single.cpp: – #include <iostream> #include <stdlib.h> using namespace std; unsigned long a=0; class A { public: virtual int f() __attribute__ ((noinline)) { return a; } }; class B : public A { public: virtual int f() __attribute__ ((noinline)) { return a; } void g() __attribute__ ((noinline)) { return; } }; int main() { cin>>a; […]

在调用condition_variable.notify_one()之前是否必须获取locking?

我对使用std::condition_variable有点困惑。 我知道我必须在调用condition_variable.wait()之前在mutex上创build一个unique_lock 。 我找不到在调用notify_one()或notify_all()之前是否也应该获取唯一的锁。 cppreference.com上的示例有冲突。 例如, notify_one页面给出了这个例子: #include <iostream> #include <condition_variable> #include <thread> #include <chrono> std::condition_variable cv; std::mutex cv_m; int i = 0; bool done = false; void waits() { std::unique_lock<std::mutex> lk(cv_m); std::cout << "Waiting… \n"; cv.wait(lk, []{return i == 1;}); std::cout << "…finished waiting. i == 1\n"; done = true; } void signals() { […]