我有一个类负责通过调用遗留类来检索产品可用性.此遗留类本身通过进行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) 我的理解是,如果在 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:\ 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) 我有一个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块.但是,它永远不会进入那里.我在这做错了什么?
我正在使用下面的查询从数据库加载一些数据.我可以使用此方法等待此查询完成使用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) 我正在初始化一个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) 我在以下链接中使用该示例.
我的代码如下.
#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) 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# ×4
c++ ×4
async-await ×2
.net ×1
.net-4.0 ×1
boost ×1
boost-log ×1
c++14 ×1
debugging ×1
exception ×1
foreach ×1
idisposable ×1
powershell ×1
shared-ptr ×1
stdmap ×1
task ×1