我最近遇到了IAsyncResult并且玩了很长时间.我真正想知道的是当我们有更好的替代ThreadPool时,为什么要使用IAsyncResult?根据我目前对它们的理解,我会选择在几乎所有情况下都使用ThreadPool.所以我的问题是,是否有任何上下文IAsyncResult优先于另一个?
为什么我不喜欢IAsyncResult:
把它放在代码中:
线程池
public void ThreadPoolApproach()
{
ThreadPool.QueueUserWorkItem( ( a ) =>
{
WebClient wc = new WebClient();
var response = wc.DownloadString( "http://www.test.com" );
Console.WriteLine( response );
} );
}
Run Code Online (Sandbox Code Playgroud)
IAsyncResult的
public void IAsyncResultApproach()
{
var a = BeginReadFromWeb( ( result ) =>
{
var response = EndReadFromWeb( result );
Console.WriteLine( response );
}, "http://www.test.com" );
}
public IAsyncResult BeginReadFromWeb( AsyncCallback a, string url )
{
var result = new AsyncResult<string>( a, null, this, "ReadFromFile" …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
Dictionary<string, string> list = new Dictionary<string, string>();
object lockObj = new object();
public void MyMethod(string a) {
if (list.Contains(a))
return;
lock (lockObj) {
list.Add(a,"someothervalue");
}
}
Run Code Online (Sandbox Code Playgroud)
假设我MyMethod("mystring")同时从不同的线程调用.
是否可能有多个线程(我们只需将其作为两个)同时输入if (!list.Contains(a))语句(具有几个CPU周期差异),两个线程都被评估为false,一个线程进入关键区域而另一个线程进入关键区域被锁定在外面,所以第二个线程进入并"mystring"在第一个线程退出后再次添加到列表中,导致字典尝试添加重复键?
我有以下代码,我正在尝试使用HttpClient以下命令向远程端点发出请求:
using (var client = new HttpClient())
{
client.BaseAddress = _serviceBaseAddress;
Task<HttpResponseMessage> readResponseTask = client.GetAsync(relativeUri);
readResponseTask.Wait();
using (var response = readResponseTask.Result)
{
if (response.StatusCode == HttpStatusCode.NotFound || !response.IsSuccessStatusCode)
{
return default(TResult);
}
Task<TResult> readContentTask = response.Content.ReadAsAsync<TResult>();
readContentTask.Wait();
TResult value = readContentTask.Result;
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
..和偶尔我会得到ThreadAbortException的readResponseTask.Result,像这样:
System.Threading.ThreadAbortException:线程正在中止.at System.Threading.Monitor.ObjWait(Boolean exitContext,Int32 millisecondsTimeout,Object obj)at System.Threading.ManualResetEventSlim.Wait(Int32 millisecondsTimeout,CancellationToken cancellationToken)at System.Threading.Tasks.Task.SpinThenBlockingWait(Int32 millisecondsTimeout,CancellationToken cancellationToken)在System.Threading.Tasks.Task.InternalWait(Int32 millisecondsTimeout,CancellationToken cancellationToken)处于System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout,CancellationToken cancellationToken)
在什么情况下.Result抛出这样的例外?我已经尝试模拟远程端点上的超时但我得到了异常.Wait()而不是.Result.由于异常发生之后.Wait(),我假设结果已经从远程站点返回但是在尝试访问结果时出现了某些问题.
有线索吗?它可能与线程并发有关吗?
我正在使用MEF,我有两个具有相同合同类型但具有不同合同名称的出口
例如:
[Export("TypeA", typeof(MyPlugin))]
[Export("TypeB", typeof(MyPlugin))]
Run Code Online (Sandbox Code Playgroud)
我可以使用各自的合同名称检索每个导出:
ServiceLocator.GetExportedValues<MyPlugin>("TypeA");
Run Code Online (Sandbox Code Playgroud)
但现在我希望检索所有实现的实例MyPlugin.有什么办法可以吗?
我尝试使用以下代码:
ServiceLocator.GetExportedValues<MyPlugin>();
Run Code Online (Sandbox Code Playgroud)
但它没有用.显然,它仅用于检索没有特定合同名称的实现.
任何意见?
我正在编写一个应用程序,旨在以下列格式处理数千篇包含大量spintax的文章/条目:
{Hello|Hi} {World|There!}, how are you?
Run Code Online (Sandbox Code Playgroud)
然而,当我使用分析器运行应用程序时,我注意到处理Regex的部分占用了大量资源,我的应用程序最终因内存不足问题而崩溃.任何人都可以建议一种方法来改进我的代码或更好的解析spintax的方法吗?
public static String Spin(String text)
{
Regex reg = new Regex(@"\{[^\{\}]*\}");
Random rand = new Random((int)DateTime.Now.Ticks);
while (true)
{
Match m = reg.Match(text);
if (!m.Success) break;
String[] parts = m.Value.TrimStart('{').TrimEnd('}').Split('|');
int i = rand.Next(parts.Length);
text = text.Substring(0, m.Index) + parts[i] + text.Substring(m.Index + m.Length);
}
return text;
}
Run Code Online (Sandbox Code Playgroud) 我一直听到有关在终结器和Dispose()方法中放置代码来处理非托管资源的建议.我不明白的是,因为在GC发生时调用终结器所以我们可以在技术上假设它一直被调用.在那种情况下,为什么还要处理一个物体呢?我错过了什么吗?
我想知道的是,当我们可以直接将值分配给私有对象时,为什么我们需要私有的setter?请考虑以下代码:
private int counter = 0;
public int Counter {
get {
return counter;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的上下文中,我没有看到私有setter(Counter = 1)与直接将值分配给私有对象(counter = 1)之间的任何区别.
我可以考虑使用私有设置器的唯一原因是需要触发更改通知/事件.除此之外,我们甚至需要一个私人安装者?
我目前正在开发一个WPF项目,我的大多数属性都有两个选项可以在内部分配一个值:
private int counter = 0;
public int Counter {
get {
return counter;
}
private set {
counter = value;
}
}
Run Code Online (Sandbox Code Playgroud)
通过私人设定者分配Counter = 1;
将值直接分配给私有对象counter = 1;
哪个是在内部分配值的首选方法(在类本身中)?使用一个在另一个上有什么好处吗?
c# ×8
.net ×1
finalizer ×1
iasyncresult ×1
mef ×1
properties ×1
regex ×1
setter ×1
spintax ×1
threadpool ×1
wpf ×1