小编BKS*_*BKS的帖子

将阻塞调用包装为异步以获得更好的线程重用和响应式UI

我有一个类负责通过调用遗留类来检索产品可用性.此遗留类本身通过进行BLOCKING网络调用来内部收集产品数据.请注意,我无法修改旧版API的代码.由于所有产品彼此独立,我想并行收集信息而不创建任何不必要的线程,也不阻止在调用此遗留API时被阻止的线程.有了这个背景,这里是我的基本课程.

class Product
    {
        public int ID { get; set; }
        public int  VendorID { get; set; }
        public string Name { get; set; }
    }

    class ProductSearchResult
    {
        public int ID { get; set; }
        public int AvailableQuantity { get; set; }
        public DateTime ShipDate { get; set; }
        public bool Success { get; set; }
        public string Error { get; set; }
    }

class ProductProcessor
    {
        List<Product> products;
        private static readonly SemaphoreSlim mutex = new SemaphoreSlim(2);
        CancellationTokenSource cts …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading task-parallel-library async-await

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

为什么这些对象没有被处置

我的理解是,如果在 foreach 循环中调用实现模式的对象IDisposable,它会被自动处理,而不需要在使用或调用Dispose方法中显式使用它。我有以下代码

class Program
{
    static void Main(string[] args)
    {
        using (Employee e = new Employee() { Name = "John" }) ;
        {

        }

        foreach (var e in GetEmployees())
            Console.WriteLine(e.Name);
    }

    static List<Employee> GetEmployees()
    {
        Employee emp = new Employee() { Name = "Peter" };
        Employee emp2 = new Employee() { Name = "Smith" };
        List<Employee> emps = new List<Employee>();
        emps.Add(emp);
        emps.Add(emp2);
        return emps;
    }
}

class Employee : IDisposable
{
    public string Name { …
Run Code Online (Sandbox Code Playgroud)

c# foreach idisposable

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

在ac#项目中使用Pseudovariables

我试图在ac#项目中使用$ tid 伪变量,但不断收到错误

错误CS0103:当前上下文中不存在名称'$ tid'

在此输入图像描述

我在这做错了什么?

c# debugging visual-studio-2015 visual-studio-2017

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

无法从网址下载文件

我需要从网址下载文件。我写了下面的代码。请注意,目标路径C:\ test已经存在

$url = "https://www.bergeyselectric.com/content/wp-content/uploads/2011/11/google.jpg"
$downloadpath = "C:\test"
$filename = $url.Substring($url.LastIndexOf("/") + 1)

$downladfileWithPath   = Join-Path $downloadPath $filename

Try
{
   (New-Object System.Net.WebClient).DownloadFile($url, $downladfileWithPath)
}
Catch
{
    Write-Host $_.Exception | format-list -force
}
Run Code Online (Sandbox Code Playgroud)

我得到以下例外。

System.Management.Automation.MethodInvocationException: Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error 
occurred on a send." ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to rea
d data from the transport connection: An existing connection …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-5.0

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

为什么catch中的代码不会被执行

我有一个Foo类,如下所示

Foo.h

#pragma once
class Foo
{
public:
    Foo() = default;
    ~Foo() = default;

    void DoSomething();
};
Run Code Online (Sandbox Code Playgroud)

Foo.cpp

#include "Foo.h"

void Foo::DoSomething()
{
    throw "something happened";
}
Run Code Online (Sandbox Code Playgroud)

我使用的类如下:

#include <iostream>
#include "Foo.h"

int main()
{
    try
    {
        Foo foo;
        foo.DoSomething();
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望代码进入catch块.但是,它永远不会进入那里.我在这做错了什么?

c++ exception

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

将方法转换为等到查询完成

我正在使用下面的查询从数据库加载一些数据.我可以使用此方法等待此查询完成使用await-async吗?

public static void LoadData()
        {
            using (MyEntities entities = new MyEntities())
            {
                List<Employee> Employees = (from d in entities.Employees
                                            where d.Id > 100
                                            select new Employee
                                            {
                                                Name = d.LastName + ", " + d.FirstName
                                                DoB = d.dob
                                            }).ToList();
            }
}
Run Code Online (Sandbox Code Playgroud)

c# .net-4.0 task async-await entity-framework-6.1

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

在方法中填充的地图的shared_ptr在调用者中返回空

我正在初始化一个shared_ptr以映射到一个名为GetData的单独函数中.此映射作为参数传递给GetData函数.但是,在main中,map在调用GetData函数后返回空.

#include <memory>
#include <map>

void  GetData(std::shared_ptr<std::map<int, int>> data);

int main()
{
    std::shared_ptr<std::map<int, int>> data2 = std::make_shared<std::map<int, int>>();
    GetData(data2);
    return 0;
}

void GetData(std::shared_ptr<std::map<int, int>> data)
{

    data = std::make_shared<std::map<int, int>>
        (std::initializer_list<std::map<int, int>::value_type>{
            { 1, 2 },
            { 5, 6 },
            { 4, 5 }
    });
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

更新: 如果在参数仍未通过引用传递的情况下重写方法如下,我确实在调用GetData方法后在main中填充了map.

void GetData(std::shared_ptr<std::map<int, int>> data)
{
    data->insert(std::pair<int, int>(1,2));
    data->insert(std::pair<int, int>(45, 2));
}
Run Code Online (Sandbox Code Playgroud)

c++ stdmap shared-ptr c++14

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

没有创建日志文件

我在以下链接中使用该示例.

https://www.boost.org/doc/libs/1_57_0/libs/log/doc/html/log/detailed/utilities.html#log.detailed.utilities.setup.settings_file

我的代码如下.

#include "stdafx.h"
#include <iostream>
#include <fstream>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/regex.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;
#define BOOST_LOG_DYN_LINK 1


int main(int, char*[])
{
    //init_logging();
    std::ifstream file("settings.ini");
    logging::init_from_stream(file);

    BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
    BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
    BOOST_LOG_TRIVIAL(error) << …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-logging boost-log

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

'transform': is not a member of 'std'

Here is my code

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
    // su is the string which is converted to lowercase
    std::wstring su = L"HeLLo WoRld";

    // using transform() function and ::toupper in STL
    std::transform(su.begin(), su.end(), su.begin(), ::tolower);
    std::cout << su << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

It generates the following compilation error.

1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(14): error C2039: 'transform': is not a member of 'std'
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(14): error C3861: 'transform': identifier not found
1>c:\users\root\source\repos\consoleapplication7\consoleapplication7\consoleapplication7.cpp(15): error C2679: binary '<<': no operator found …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors visual-studio

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