小编Fat*_*ror的帖子

开始结束异步方法调用

这是Microsoft的代码片段.我对异步方法调用有疑问.

因为我们正在调用end.Invoke在Begin-invoke之后看起来我们正在进行同步调用.因为我们正在等待异步调用的返回值.

如果异步方法在调用end.invoke时没有完成,会发生什么.我们可以继续下一个声明,或者我们必须等待.

如果在多线程环境中发生这种情况,它们如何处理回调信号以纠正线程.

public void DemoEndInvoke()
{
    MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
    string s ;
    int iExecThread;

    // Initiate the asynchronous call.
    IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);  

    // Do some useful work here. This would be work you want to have
    // run at the same time as the asynchronous call.

    // Retrieve the results of the asynchronous call.
    s = dlgt.EndInvoke (out iExecThread, ar) ;  

    MessageBox.Show (string.Format ("The delegate call returned the string: …
Run Code Online (Sandbox Code Playgroud)

c# multithreading asynchronous begininvoke .net-3.5

1
推荐指数
1
解决办法
3849
查看次数

StringBuilder类的线程安全性

class Test {
    public string GetData() {
        StringBuilder sb = new StringBuilder();
        sb.Append("aassffss");
        sb.Append("bbhhhhh");
        return sb.ToString();
    }
}
// calling from multithreads,as below, 
// from each thread i will create new 
// instance and call method.

Test t = new Test(); 
t.GetData();
Run Code Online (Sandbox Code Playgroud)

根据MSDN.类的任何实例成员StringBuilder都不是线程安全的.所以我相信这不是线程安全的.我对么?

c# stringbuilder multithreading thread-safety

1
推荐指数
1
解决办法
2938
查看次数