我有这个型号:
public class ExchangeRate
{
[Key]
public int ExchangeRateID { get; set; }
[Required]
[Display(Name = "Currency:")]
public string Currency { get; set; }
[Required]
public decimal Rate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
"创建"视图工作正常,但当我在编辑视图中时,我只希望显示Currency属性,而不是可编辑的.我该怎么做?如果我为这个类创建另一个"仅查看"模型,那么我将省略"Currency"属性并且无法显示它.
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ExchangeRate</legend>
@Html.HiddenFor(model => model.ExchangeRateID)
<div class="editor-label">
@Html.LabelFor(model => model.Currency)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.Currency)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rate)
@Html.ValidationMessageFor(model => model.Rate)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
}
将@ Html.EditorFor(model …
我有些怀疑下面的“库”方法是否真的有效,或者最终会陷入僵局。
我有一个需要保护的“旧”对象,并且“ lock ”似乎是最好的工具,因为在整个“类”中对此资源(SomeSharedResource)有一些可重入的调用。
编辑:
1.) 对“Compute”的调用实际上会阻塞或更坏的情况是死锁吗?
var finalResult = await Compute(3).ConfigureAwait(false); // will this block?
Run Code Online (Sandbox Code Playgroud)
2.) 如果有人这样做呢?
var finalResult = Compute(3).Result; // will this block/deadlock?
Run Code Online (Sandbox Code Playgroud)
3.) 2个线程同时调用:
var finalResult = await Compute(3).ConfigureAwait(false); // will this block?
Run Code Online (Sandbox Code Playgroud)
有问题的方法:
private readonly object lockObject = new object();
private async Task<double> Compute(int input)
{
double result;
lock (lockObject) {
result = SomeSharedResource(input);
}
return await ComputeAsync(result).ConfigureAwait(false); // some other awaitable method
}
Run Code Online (Sandbox Code Playgroud) Lucian在这里讨论了一种模式(技巧3:在任务返回API中包装事件并等待它们).
我试图在一个经常调用的方法上实现它,看起来像下面的设计代码:
public Task BlackBoxAsync()
{
var tcs = new TaskCompletionSource<Object>(); // new'ed up every call
ThreadPool.QueueUserWorkItem(_ =>
{
try
{
DoSomethingStuff();
tcs.SetResult(null);
}
catch(Exception exc) { tcs.SetException(exc); }
});
return tcs.Task;
}
Run Code Online (Sandbox Code Playgroud)
我很担心性能,TaskCompletionSource每次通话都是新的(让我们说我每100毫秒调用一次这个方法).
我当时正在考虑使用BufferBlock<T>,认为每次通话都不会重新开始.所以它看起来像:
private readonly BufferBlock<object> signalDone; // dummy class-level variable, new'ed up once in CTOR
public Task BlackBoxAsync()
{
ThreadPool.QueueUserWorkItem(_ =>
{
try
{
DoSomethingStuff();
signalDone.Post(null);
}
catch(Exception exc) { }
});
return signalDone.ReceiveAsync();
}
Run Code Online (Sandbox Code Playgroud)
调用对象会将其称为:
for (var i=0; i<10000; i++) …Run Code Online (Sandbox Code Playgroud) c# task-parallel-library async-await tpl-dataflow taskcompletionsource
我遇到了MVC4的问题
@Html.AntiForgeryToken()
Run Code Online (Sandbox Code Playgroud)
html助手.在我的开发机器上,当我运行项目时,在检查标题(使用Fiddler)时,返回的标记的名称是
__RequestVerificationToken
Run Code Online (Sandbox Code Playgroud)
但是当部署到IIS 7.5版(Windows 2008 R2)时,令牌名称如下所示:
__RequestVerificationToken_L2V6b3JkZXI1
Run Code Online (Sandbox Code Playgroud)
这在哪里变了?是因为我的应用程序没有部署到IIS的"根文件夹"?例如,我的应用程序已部署到
"http://myserver/myapp" instead of "http://myserver"
Run Code Online (Sandbox Code Playgroud) 我有 2 个线程调用“Task.Factory.StartNew”。假设一个线程 (ThreadA) 比另一个线程 (ThreadB) 稍早一些。
... 在线程 A 中,稍微提前调用
Task.Run(() => {
SomeMethodInThreadA();
});
Run Code Online (Sandbox Code Playgroud)
... 在线程 B 中,稍后调用
Task.Run(() => {
SomeMethodInThreadB();
});
Run Code Online (Sandbox Code Playgroud)
TaskScheduler是否保证SomeMethodInThreadA在SomeMethodInThreadB之前先执行?
如果没有,我该怎么做?使用 Task.Factory.StartNew 并传入特殊的 TaskScheduler?
另外,除了保证先处理 SomeMethodInThreadA 之外,我还想确保SomeMethodInThreadA在执行SomeMethodInThreadB之前先完成。
我研究过使用StaTaskScheduler,但我不确定这是否是解决方案。
编辑:
在没有给出关于我继承的程序的太多细节/戏剧的情况下,我想我正在寻找的是一个自定义的 TaskScheduler,它:
当委托排队时尊重序列
串行执行它们
在同一个线程中执行,类似于我上面链接的 StaTaskScheduler 源代码
来自 TPL 文档
与 一样
ActionBlock<TInput>,TransformBlock<TInput,TOutput>默认一次处理一条消息,保持严格的 FIFO 顺序。
但是,在多线程场景中,即如果多个线程“同时”执行SendAsync,然后通过调用“等待”结果ReceiveAsync,我们如何保证将某些内容发布到 的线程TransformBlock<TInput,TOutput>实际获得它正在等待的预期结果为了?
在我的实验中,似乎“保证”我想要的结果的方法是添加 option BoundedCapacity = 1。至少线程在发送和接收时仍然不会被阻塞。
如果我不这样做,某些线程将收到用于另一个线程的结果。
在这个特定用例中,这是正确的方法吗?
下面是一些代码,说明了我的担忧:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace ConsoleTransformBlock
{
class Program
{
private readonly static TransformBlock<int, int> _pipeline;
static Program()
{
_pipeline = new TransformBlock<int, int>(async (input) =>
{
await Task.Delay(RandomGen2.Next(5, 100)).ConfigureAwait(false);
return input;
},
new ExecutionDataflowBlockOptions() { BoundedCapacity = 1 }); // this is the …Run Code Online (Sandbox Code Playgroud) c# multithreading task-parallel-library async-await tpl-dataflow
此视图适用于IE9和Chrome.但是,不适用于IE8.呈现页面时,它的外观如下:

我的HTML(MVC3 View)如下所示.
<div id="machinedisplay" data-bind="with: selectedMachine" >
<h2><span data-bind="text: MachineDesciption" /></h2>
<!-- ko with: my.vm.machineData -->
<table>
<thead><tr>
<th>Point Name</th><th>Description</th><th>Points Data</th>
</tr></thead>
<tbody data-bind="foreach: Points">
<tr>
<td data-bind="text: PointName()"></td>
<td data-bind="text: PointDesciption()"></td>
<td>
<table style="width:100%;">
<thead><tr>
<th>Name</th><th>Description</th><th>Value</th><th></th>
</tr></thead>
<tbody data-bind="foreach: Params">
<tr>
<td data-bind="text: ParameterName"></td>
<td data-bind="text: ParameterDescription"></td>
<td data-bind="text: StringValue"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<!-- /ko -->
</div>
Run Code Online (Sandbox Code Playgroud)
关于IE8的任何想法都可以解决?
编辑: 为了在一个更简单的模型上说明这个问题,请查看这个小提琴http://jsfiddle.net/ericpanorel/nzKvb/
我想我遇到了问题,因为我使用的是"with"或"if"绑定.我在某处读到这会导致IE8出现问题.
我使用IE9,如果你使用你的开发者工具从IE9切换到IE8,这个小提琴不再正常工作.这个小提琴实际上源于一个淘汰赛的样本(http://knockoutjs.com/examples/gridEditor.html)
编辑: 我更新了小提琴... http://jsfiddle.net/nzKvb/20/ 它与嵌套的无容器绑定中的标签的短手关闭有关
<!-- ko if: Allowed-->
<h2> …Run Code Online (Sandbox Code Playgroud) 我在这里提到了Jon Skeet的文章(http://csharpindepth.com/articles/general/singleton.aspx),第六个版本.
但是,我有一些私有变量,我想初始化一次,并被这个所谓的单例类中的方法使用.我在私有构造函数中初始化它们,但很快发现,在多线程场景(Task.Run)中调用方法时它们是null .
在调试时,我观察到私有构造函数在调用"实例"时没有调用两次(应该是),因此我假设我的私有变量在那个时间点不应该为空(成功的"实例"调用).
关于如何声明,初始化和使用这些变量的任何想法?
public sealed class Singleton
{
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
// my private variables
private readonly string _plantCode;
private Singleton()
{
var appSettings = ConfigurationManager.AppSettings;
string _plantCode = appSettings["PlantCode"] ?? "Not Found";
}
public SomeMethod()
{
var temp = _plantCode; // <== _plantCode becomes null here!
}
}
Run Code Online (Sandbox Code Playgroud)