我正在使用async/await,并从我的一个视图中调用异步方法,我需要等到它完成.我已经看过很多ASP.NET MVC的例子,你可以在你的行动签名中加入"异步".但我还没有看到ASP.NET WebPages的任何示例.
我只是在Razor视图中的返回任务上调用"Wait()"吗?我见过针对Wait()的建议.
请给我一些关于如何在WebPages的Razor视图中正确调用异步方法的参考/示例.
假设我有这个列表:
Children = new List<Child>();
Run Code Online (Sandbox Code Playgroud)
如何插入/添加null n次Children?
(背景:在我的数据访问层中,我需要设置我从数据库中获取的总孩子数.现在,我的演示文稿只需要访问Children.Count.所以我打算插入null对象Children.)
我有以下代码调用2个异步方法并等待结果:
private Task FirstTaskAsync() {
...
}
private Task SecondTaskAsync() {
...
}
private async void Caller() {
await FirstTaskAsync();
await SecondTaskAsync();
}
Run Code Online (Sandbox Code Playgroud)
问题是它现在执行并顺序等待每个任务.我想将其更改为Task.WaitAll().这是我的改变:
private async void Caller() {
var first = FirstTaskAsync();
var second = SecondTaskAsync();
Task.WaitAll(first, second);
}
Run Code Online (Sandbox Code Playgroud)
但是当我测试它时,Task.WaitAll()永远不会返回.你能告诉我缺少什么吗?
好吧,就我从线程中读到的而言,这是不可能的,但在我的情况下肯定会发生.
取决于我开始执行多少后台任务,即使它们与ui线程有0关系,也肯定会影响我的gui响应
所以我的问题是,是否有人知道其他线程如何使ui变得反应迟钝?
我100%确定这些非ui线程导致其缓慢,因为它发生,即使我禁用所有gui更新事件.它肯定受我的案例中有多少个线程(抓取网址任务和处理这些抓取的页面任务)的影响
这是我的ui线程以及我如何开始后台任务:
InitializeComponent();
this.DataContext = this;
ThreadPool.SetMaxThreads(10000, 10000);
ThreadPool.SetMinThreads(10000, 10000);
PublicVariables.initPublicVariables();
PublicStaticFunctions.func_initLists();
PublicSettings.func_init_Settings_Messages();
Task.Factory.StartNew(() =>
{
CheckCrawlURLs.func_StartCrawlingWaitingUrls();
AddUrlsToDB.func_StartUrlAddProcess();
LoadCrawlingUrlsFromDatabase.func_StartLoadingUrlsFromDB();
GlobalStats.startUpdatingGlobalStatValues();
PagesProcessor.func_StartProcessingWaitingPages();
}, CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
AppDomain currentDomain = AppDomain.CurrentDomain;
Application.Current.DispatcherUnhandledException +=
new DispatcherUnhandledExceptionEventHandler(CloseCrashHandlers.AppDispatcherUnhandledException);
currentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CloseCrashHandlers.CrashCloseHandler);
Closing += new CancelEventHandler(CloseCrashHandlers.CloseHander);
set_Buttons_Status();
_timer = new Timer(updateGlobalStatistics,
null,
PublicSettings.irTimers_Delayed_Start_MiliSeconds,
PublicSettings.ir_RefreshUI_MS);
WebConnectionStats.Init();
Run Code Online (Sandbox Code Playgroud) 我正在使用遗传算法构建优化程序.我使用Parallel.For来减少时间.但它导致了一个问题,在下面的代码中是相同的:
class Program
{
static void Main(string[] args)
{
int j=0;
Parallel.For(0, 10000000, i =>
{
j++;
});
Console.WriteLine(j);
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
每次我运行上面的程序时,它会在0到10000000之间写入不同的j值.我猜它不会等待所有迭代完成.它传递到下一行.我该怎么解决这个问题?任何帮助将不胜感激.谢谢.
版本:Interlocked.Increment(ref j); 子句解决了意外的结果,但是当我与正常循环比较时,此操作会导致大约10倍的时间.
我想知道条件方法是否可以在类属性上具有条件.
例如 :
class Class1
{
public bool _doStuff;
[Conditional(_doStuff)]
public static void Stuff() {
// Do the stuff
}
}
Run Code Online (Sandbox Code Playgroud)
喜欢的[Conditonal("DEBUG")].
有谁知道这个?
List<?> temp = empObjList.stream()
.filter(nestedDo -> nestedDo.getAttrib1() == "subject")
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这里我调用方法getAttrib1().但是要调用的方法只能动态识别.我将只获得函数的名称作为String值.我想将它动态转换为函数.我知道我可以使用Reflection来进行动态方法调用,但我不能通过反射重写上面的代码.
int默认情况下,C,C++,C#,Java,Rust等已签名.大多数时候你需要无符号变量,因为你必须表示低于零的东西的情况比处理自然数的情况要少.无符号变量也不必以2的补码形式编码,并且对于额外的值范围,它们具有最高有效位.
考虑到所有这些因素,为什么语言创建者会默认签名?
我想使用Null-Conditional运算符来检查SubscriptionExpires下面的属性.
public partial class Subscription
{
[Key]
public int SubscriptionId { get; set; }
public string SubscriberId { get; set; }
public DateTime? SubscriptionExpires { get; set; }
public virtual ICollection<ApplicationUser> Users { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
订阅由返回
var subscription = _customersContext.Subscriptions.Where(s => s.SubscriptionId == user.SubscriptionId).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
但是如果Subscription为null,则Subscription?.SubscriptionExpires返回a null reference exception,所以我们仍然保留旧的
if (subscription != null)
Run Code Online (Sandbox Code Playgroud)
当父对象可以为null时,如何使用Null-Conditional运算符来读取属性?
我正在尝试将旧项目从BackgroundWorker转换为异步/等待,但我真的很难让进度条更新.我遵循了这篇文章但却无法像以下一样工作:
这是我的代码:
private async void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
pb.Show();
btnCancel.Enabled = true;
var progressIndicator = new Progress<int>(ReportProgress);
List<string> updates = Directory.GetFiles(txtInput.Text).ToList();
try
{
await ProcessUpdates(updates, progressIndicator, _cts.Token);
}
catch (OperationCanceledException ex)
{
MessageBox.Show(ex.Message, "Operation Cancelled");
}
btnStart.Enabled = true;
pb.Hide();
btnCancel.Enabled = false;
}
async Task<int> ProcessUpdates(List<string> updatePaths, IProgress<int> progress, CancellationToken ct)
{
int total = updatePaths.Count;
for (int i = 0; i < updatePaths.Count; i++)
{
ct.ThrowIfCancellationRequested();
string update = updatePaths[i];
ssFile.Text = …Run Code Online (Sandbox Code Playgroud) c# ×8
async-await ×2
asp.net ×1
asynchronous ×1
c ×1
c#-6.0 ×1
int ×1
java ×1
java-8 ×1
list ×1
parallel.for ×1
razor ×1
signed ×1
task ×1
unsigned ×1
windows-8.1 ×1
wpf ×1