Tag: 异步

调用基于异步任务的WCF方法是否利用I / O完成端口或线程池线程来调用延续?

我有以下WCF合同: [ServiceContract(Namespace = “http://abc/Services/AdminService”)] public interface IAdminService { [OperationContract] string GetServiceVersion(); // More methods here } GetServiceVersion是一个返回一些字符串的简单方法。 它用作ping来检查服务是否可访问。 现在我想异步调用它,认为它比使用.NET线程在后台调用它更有效。 所以,我为此提出了以下接口: [ServiceContract(Namespace = “http://abc/Services/AdminService”)] public interface IMiniAdminService { [OperationContract(Action = “http://abc/Services/AdminService/IAdminService/GetServiceVersion”, ReplyAction = “http://abc/Services/AdminService/IAdminService/GetServiceVersionResponse”)] Task GetServiceVersionAsync(); } 这使得可以异步调用GetServiceVersion API: var tmp = new ChannelFactory(“AdminServiceClientEndpoint”); var channelFactory = new ChannelFactory(tmp.Endpoint.Binding, tmp.Endpoint.Address); var miniAdminService = channelFactory.CreateChannel(); return miniAdminService.GetServiceVersionAsync().ContinueWith(t […]

在using语句中调用异步方法

我想使用异步调用发送消息,但在此过程中使用了可用资源。 我不确定如何正确处理这个问题。 using(IDisposable disposeMe = new DisposableThing()) { MethodAsync(disposeMe); } 任何想法如何做到这一点。 异步方法没有任何回调。 编辑: 基于@Servy接受的答案的建议。 实施改为: public async Task Method() { using(IDisposable disposeMe = new DisposableThing()) { await MethodAsync(disposeMe); } }

使用.net 4.5中的Async API对串口通信代码进行示例?

有人能指出我使用.net 4.5异步API(异步,等待,任务,ReadAsync等)进行串行通信的工作示例吗? 我试图调整现有的事件驱动的串行样本,并得到各种可怕的行为 – “其他应用程序正在使用的端口”错误,VS2013调试器抛出exception和锁定 – 这通常需要PC重启才能从中恢复。 编辑 我从头开始编写自己的样本。 这是一个简单的Winforms项目,它写入Output窗口。 表单上的三个按钮 – 打开端口,关闭端口和读取数据。 ReadDataAsync方法调用SerialPort.BaseStream.ReadAsync。 截至目前,它将从端口读取数据,但我遇到了问题,使其变得强大。 例如,如果我拔下串行电缆,打开端口,然后单击Read Data两次,我将得到一个System.IO.IOException(我有点期待),但我的应用程序停止响应。 更糟糕的是,当我尝试停止我的程序时,VS2013会抛出一个“正在进行停止调试”对话框,该对话框永远不会完成,我甚至无法从任务管理器中杀死VS. 每次发生这种情况都必须重启我的电脑。 不好。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private SerialPort _serialPort; public Form1() […]

如何在此上下文中使用WebClient.DownloadDataAsync()方法?

我的计划是让用户在我的程序中写下电影标题,我的程序将异步提取适当的信息,这样UI就不会冻结。 这是代码: public class IMDB { WebClient WebClientX = new WebClient(); byte[] Buffer = null; public string[] SearchForMovie(string SearchParameter) { //Format the search parameter so it forms a valid IMDB *SEARCH* url. //From within the search website we’re going to pull the actual movie //link. string sitesearchURL = FindURL(SearchParameter); //Have a method download asynchronously the […]

名为Mutex等待

因此我不能使用async线程仿射锁 – 如何在运行多个进程时保护我的资源? 例如,我有两个使用以下任务的进程: public async Task MutexWithAsync() { using (Mutex myMutex = new Mutex(false, “My mutex Name”)) { try { myMutex.WaitOne(); await DoSomething(); return true; } catch { return false; } finally { myMutex.ReleaseMutex(); } } } 如果由Mutex保护的moethod是同步的,那么上面的代码将起作用但是async我会得到: 从不同步的代码块调用对象同步方法。 那么命名Mutex对异步代码没用吗?

在后台操作过程中显示模态UI并继续

我有一个运行后台任务的WPF应用程序,它使用async/await 。 任务是在进展时更新应用程序的状态UI。 在此过程中,如果满足某个条件,我需要显示一个模态窗口,让用户知道这样的事件,然后继续处理,现在也更新该模态窗口的状态UI。 这是我想要实现的草图版本: async Task AsyncWork(int n, CancellationToken token) { // prepare the modal UI window var modalUI = new Window(); modalUI.Width = 300; modalUI.Height = 200; modalUI.Content = new TextBox(); using (var client = new HttpClient()) { // main loop for (var i = 0; i < n; i++) { token.ThrowIfCancellationRequested(); // […]

在LINQ语句中使用async / await时实际发生了什么?

以下片段编译,但我希望它等待任务结果,而不是给我一个List<Task> 。 var foo = bars.Select(async bar => await Baz(bar)).ToList() 正如这里指出的,你需要使用Task.WhenAll : var tasks = foos.Select(async foo => await DoSomethingAsync(foo)).ToList(); await Task.WhenAll(tasks); 但是评论指出不需要在Select()内部进行async和await : var tasks = foos.Select(foo => DoSomethingAsync(foo)).ToList(); 这里有一个类似的问题,有人试图在Where()使用异步方法。 因此,在LINQ语句中async和await是合法的语法,但是它什么都不做或者它是否具有某种用途?

在基于事件的异步模式上使用任务并行库

我正在写一个网络应用程序。 邮件通过传输方式发送: Network.SendMessage (new FirstMessage() ); 我可以注册一个事件处理程序,当这个消息类型到达时调用,如下所示: Network.RegisterMessageHandler (OnFirstMessageReceived); 事件被解雇了: public void OnFirstMessageReceived(EventArgs e) { } 我正在为我的网络应用程序编写自定义身份validation过程,这需要大约五条消息才能完成。 不使用任务并行库,我将被迫编写前面的事件处理程序中的每个过程的下一步,如下所示: public void OnFirstMessageReceived(EventArgs e) { Network.SendMessage( new SecondMessage() ); } public void OnSecondMessageReceived(EventArgs e) { Network.SendMessage( new ThirdMessage() ); } public void OnThirdMessageReceived(EventArgs e) { Network.SendMessage( new FourthMessage() ); } public void OnFourthMessageReceived(EventArgs e) { // Authentication […]

在调用开始之前,迭代中的值会发生变化

我在我的应用程序中有以下代码。 MyEventHandler handler = null; //Declare the handler foreach (string pname in group) { handler = getHandler(pname); //Get the handler if(handler == null) { throw new KeyNotFoundException(“No user ” + pname + ” could be found”); } //invoke the handler handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } 所以我得到了处理程序并调用了BeginInvoke方法。 但是在调用BeginInvoke之前,它会进入下一次迭代并且处理程序值会发生变化。 所以BeginInvoke正在涉及这个新的处理程序。 希望你明白我的观点。 那我怎么能消除这个问题呢? 我不想在BeginInvoke之后调用睡眠,因为我觉得这是一个时间的浪费。 有任何想法吗? Update1我很确定在调用BeginInvoke()之前处理程序对象会被更改。 我猜BeginInvoke需要一些时间来创建一个单独的线程来调用另一个函数。 […]

异步/等待.NET的WebBrowser类的实现

长期读者,这里的第一次海报。 我的目标:在使用WebBrowser类时能够利用async / await。 由于WebBrowser.Navigate(string url)是一个异步方法,因此在触发LoadComplete事件之前无法检查html文档。 到目前为止,这是我的(工作)代码: public class AsyncWebBrowser { protected WebBrowser m_WebBrowser; private ManualResetEvent m_MRE = new ManualResetEvent(false); public void SetBrowser(WebBrowser browser) { this.m_WebBrowser = browser; browser.LoadCompleted += new LoadCompletedEventHandler(WebBrowser_LoadCompleted); } public Task NavigateAsync(string url) { Navigate(url); return Task.Factory.StartNew((Action)(() => { m_MRE.WaitOne(); m_MRE.Reset(); })); } public void Navigate(string url) { m_WebBrowser.Navigate(new Uri(url)); } […]