出于某种原因,我收到一个 HttpRequestException 消息“响应过早结束。我正在创建大约 500 个任务,这些任务使用我的 RateLimitedHttpClient 向网站发出请求,以便它可以抓取它。
异常是从行中抛出的return await response.Content.ReadAsStringAsync();。
有 500 个任务,每个任务有大约 20 个页面要下载和解析(总共大约 11000 个),我是否可能超出了 .Net 的 HttpClient 的能力?
public class SECScraper
{
public event EventHandler<ProgressChangedEventArgs> ProgressChangedEvent;
public SECScraper(EPSDownloader downloader, FinanceContext financeContext)
{
_downloader = downloader;
_financeContext = financeContext;
}
public void Download()
{
_numDownloaded = 0;
var companies = _financeContext.Companies.OrderBy(c => c.Name);
_interval = companies.Count() / 100;
var tasks = companies.Select(c => ScrapeSEC(c.CIK) ).ToList();
Task.WhenAll(tasks);
}
}
public class RateLimitedHttpClient : IHttpClient
{
public RateLimitedHttpClient(System.Net.Http.HttpClient …Run Code Online (Sandbox Code Playgroud) 这不是'如何抓住所有例外',而是'你应该抓住所有例外'吗?在C#.NET中,我注意到了大量的异常.是否可以计划捕获每个例外?
例如,DirectoryInfo()构造函数抛出4个异常.我应该计划捕捉这些还是只抓住我能处理的那些?也许让其他人冒泡到Main()我所有的地方,然后告诉用户有一个未捕获的异常.在所有这些可能的例外情况下,您的代码可能会变得比实际代码更多的异常处理.
创建一个登录表单,如果凭据正确,将继续进入主表单.这是基本的伪代码:
ShowLoginForm()
if (DialogResult == OK)
CheckCredentials();
if (credentials == VALID)
ShowMainForm();
else
LoopBackAndShowLoginFormAgain(); //repeat process...
else
CloseLoginForm();
Run Code Online (Sandbox Code Playgroud)
但是,我对WinForms相对较新,并且还在搞清楚它.我正在尝试决定将循环放在哪里(Main()或LoginForm中的某个地方).我应该在哪里检查凭证?这是我到目前为止:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm loginForm = new LoginForm();
if (loginForm.ShowDialog() == DialogResult.OK)
{
Application.Run(new AutoSignerForm());
}
}
Run Code Online (Sandbox Code Playgroud)
我不想反复退出并打开一个登录表单(我在这里寻找一些效率,所以我想使用相同的登录对话框).任何指针,提示或想法?
我很难找到解决必须维护两个列表的问题的方法.
我正在使用MVVM,但不希望我的模型使用ObservableCollection.我觉得最好封装并允许我使用不同的视图/模式(例如控制台).而不是像这样设置我的结构:
public class MainWindow {
// handled in XAML file, no code in the .cs file
}
public abstract class ViewModelBase : INotifyPropertyChanged {
// handles typical functions of a viewmodel base class
}
public class MainWindowViewModel : ViewModelBaseClass {
public ObservableCollection<Account> accounts { get; private set; }
}
public class Administrator {
public List<Account> accounts { get; set; }
public void AddAccount(string username, string password) {
// blah blah
}
}
Run Code Online (Sandbox Code Playgroud)
我想避免在上面的案例中有两个不同的集合/列表.我只希望模型处理数据,并ViewModel负责其渲染方式的逻辑.
将 ReSharper 与 C++17 结合使用,我启用了许多警告,只是为了看看我的项目警告了我什么。我明白了:
Declaring a parameter with a default argument is disallowed[fuchsia-default-arguments]
Run Code Online (Sandbox Code Playgroud)
有问题的代码是构造函数:
class Point2D
{
public:
explicit Point2D(double x = 0.0, double y = 0.0);
};
Run Code Online (Sandbox Code Playgroud)
我想知道为什么默认参数会被认为是坏/差/值得警告?有人有任何代码示例证明这是一个可行的警告吗?
相对较新的WinForms,但我正在开发一个小型企业应用程序.无论如何,我应该在哪里存储表单的字符串和其他资源?我会有名称,地址等标签.我应该在设计师中对它们进行硬编码吗?或者将它们存储在.resx文件中?如果是这样,哪个.resx文件(表格或属性)?
我只想弄清楚一个"经验丰富"的C#/ .NET开发人员认为好的做法是什么?我的第一选择是Form的.resx文件.
到目前为止,这就是我所拥有的...即使我实例化一个Window(WindowBase的子类),我也会收到一个试图调用纯虚函数的错误.基本上,我的程序试图调用WindowBase :: WndProc而不是Window :: WndProc.
#ifndef WINDOWBASE_H_
#define WINDOWBASE_H_
#include <Windows.h>
class WindowBase {
public:
WindowBase(HINSTANCE hInstance, int nCmdShow);
~WindowBase();
void Show();
protected:
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam) = 0;
private:
static LRESULT CALLBACK WndRouter(HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam);
HWND hWnd;
int nCmdShow;
};
#endif /* WINDOWBASE_H_ */
Run Code Online (Sandbox Code Playgroud)
#include <Windows.h>
#include "WindowBase.h"
#include <tchar.h>
WindowBase::WindowBase(HINSTANCE hInstance, int nCmdShow) {
this->nCmdShow = nCmdShow;
WNDCLASS wcex;
//wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = …Run Code Online (Sandbox Code Playgroud) 在使用C#长时间工作之后,我正在使用C++ 17,并通过一些Project Euler问题来完成我的工作.无论如何,任何人都可以解释为什么createRandomVector(const int n)不"移动"创建的向量?我输出内存地址,它们只有在通过引用传递时才会保持不变(显然).以下是代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
using namespace std;
auto createRandomVector(const int n)
{
vector<int> v(n);
iota(begin(v), end(v), 1);
shuffle(begin(v), end(v), std::mt19937());
cout << &v << endl;
return v;
}
void printVector(const vector<int>& v, ostream& os);
int main()
{
auto v = createRandomVector(100);
printVector(v, cout);
cout << endl;
cout << &v << endl;
return 0;
}
void printVector(const vector<int>& v, ostream& os)
{
cout << &v << endl;
for_each(begin(v), …Run Code Online (Sandbox Code Playgroud) 我想在一个名为X的变量中存储一个整数和一个String,然后显示它.
int X;
printf("enter a number or a name")
scanf("%d", &X);
printf("%d", X);
Run Code Online (Sandbox Code Playgroud)
显然我需要用String类型和int类型声明变量X,我该怎么做呢?谢谢