Tag: 异步

PostSubmitter的异步CTP

我正在尝试构建一个REST客户端使用Async CTP。 我是CTP的新手,因此,通过互联网上的一些例子,我得到了一个仅用于发布(GET或POST)的clas。 到目前为止这是class级: using System; using System.Collections.Specialized; using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; namespace RESTClient.Core { /// /// Submits post data to a url. /// public class PostSubmitter { #region Backing Store private string m_url = string.Empty; private NameValueCollection m_values = new NameValueCollection(); private PostTypeEnum m_type = PostTypeEnum.Get; #endregion #region […]

等待TaskEx.Delay

我正在玩HttpListener和Async CTP class HttpServer : IDisposable { HttpListener listener; CancellationTokenSource cts; public void Start() { listener = new HttpListener(); listener.Prefixes.Add(“http://+:1288/test/”); listener.Start(); cts = new CancellationTokenSource(); Listen(); } public void Stop() { cts.Cancel(); } int counter = 0; private async void Listen() { while (!cts.IsCancellationRequested) { HttpListenerContext context = await listener.GetContextAsyncTask(); // my extension method with […]

为什么这个任务早点返回? 我做错了什么吗?

我正在尝试建立一些具有一些最小耦合的工作者,但我想使用C# async和任务。 并非所有任务都是纯粹的异步(有些将是完全同步的)。 这样做的动机是我想创建一些执行业务逻辑的简单方法,并使用System.Threading.Tasks.Task API将它们链接在一起,以保留一些排序概念。 基本上,我想创建第一个任务,注册一些延续,然后等待最终任务完成。 这是我构建的简单原型,看看我想做什么甚至可以工作: void Main() { var worker = new Worker(); var work = worker.StartWork(1, 2000); work.ConfigureAwait(false); var final = work.ContinueWith(_ => worker.StartWork(2, 0)) .ContinueWith(ant => worker.StartWork(3, 1500)); var awaiter = final.ContinueWith(_ => Tuple.Create(_.Id, _.Status)); Console.WriteLine(“\”final\” completed with result {0}”, awaiter.Result); Console.WriteLine(“Done.”); } // Define other methods and classes here class […]

紧凑框架上的线程的AsyncCallback

我需要在紧凑的框架应用程序中实现线程来改善加载时间。 我想触发一个后台线程来调用外部API,而主线程缓存一些表单。 当后台线程完成后,我需要启动另外两个线程来填充数据缓存。 我需要后台线程能够执行回调方法所以我知道它已经完成并且可以启动接下来的两个线程,但是在紧凑框架中不支持委托上的BeginInvoke方法,那么我还能怎样做呢?

如何在C#中调用和使用WSAAsyncSelect()?

我设置赏金时编辑了我的问题。 我想从WinAPI调用/ DllImport WSAAsyncSelect()并使用它,就像我在Delphi / C ++中使用它一样 例如 – delphi //Async CallBack handler Declaration procedure MessageHandler(var Msg:Tmessage);Message WM_WINSOCK_ASYNC_MSG; //Where i setup the Async dwError := WSAAsyncSelect(Sock, form1.handle, WM_WINSOCK_ASYNC_MSG, FD_CLOSE or FD_READ); //Async Callback Handler procedure Tform1.MessageHandler(var Msg:Tmessage); begin case WSAGetSelectEvent(MSG.LParam) of //LParam is FD_READ/FR_CLOSE/FD_WRITE FD_READ: OnSocketRead(MSG.WParam); //WPARAM is the Socket itself. FD_CLOSE: OnSocketClose(MSG.WParam); end; end; […]

在ContinueWith()之后,ConfigureAwait(False)不会更改上下文

我不知道我做错了什么或者我在Async库中发现了一个错误,但是在使用continueWith()回到Synchronized上下文后运行一些异步代码时我遇到了一个问题。 更新:代码现在运行 using System; using System.ComponentModel; using System.Net.Http; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { internal static class Program { [STAThread] private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } public partial class Form1 : Form { public Form1() { InitializeComponent(); MainFrameController controller = new MainFrameController(this); //First async call without continueWith controller.DoWork(); //Second […]

套接字ReceiveAsync合并数据包

我打算通过套接字接收数据包,但由于它们是从发送方以高频率发送的,因此它们中的一些被打包成单个byte数组。 然后SocketAsyncEventArgs.Buffer保存多个数据包,即使它们是单独发送的(使用wiresharkvalidation)。 我已经尝试对传入的数据包进行排队并异步处理它们,但我仍然得到相同的结果。 这种行为可能是什么原因?

检测客户端的正确方法已从TCP / IP断开连接

我使用了异步TCP / IP服务器,一切正常,但是当客户端由于错误或强制退出应用程序而断开连接时,由于IO.IOException类型的exception,它也会关闭我的服务器。 以下子发生exception: Private Sub ReadCallback(ByVal result As IAsyncResult) Try Dim client As Client = TryCast(result.AsyncState, Client) If client Is Nothing Then Return End If ‘MsgBox(client.ClientID) Dim networkStream As NetworkStream = client.NetworkStream Dim read As Integer = networkStream.EndRead(result) **’ ERRORS HERE!** If read = 0 Then Dim client As Client = TryCast(result.AsyncState, Client) SyncLock […]

如何异步这个长时间运行的方法?

我有这种方法,我想异步运行,以便我可以在运行时做其他事情。 它不依赖于任何其他Async方法(它不会调用另一个资源,下载文件或任何东西)。 我想避免使用new Task() , Task.Factory.StartTask()和Task.Run() ,如果可能的话。 是否可以异步运行此方法,使用整洁,可读的代码并且不显式使用Task? 如果不是,那么异步运行该方法的最简单方法是什么? 注意:请不要关心方法中的愚蠢逻辑 – 我把它煮成故意慢但不显示我的实际代码。 public static void main(string[] args) { RunMySlowLogic(); } private void RunMySlowLogic() { while (true) for (int i=0; i<100000000;i++) if (i == new Random().Next(999)) return true; } 目前,我认为我需要将方法包装在lambda或Task中并将其标记为异步。 等待去哪儿了?

在使用BeginInvoke和EndInvoke时,如何避免传递/存储委托?

编辑 :将实际问题移至顶部。 更新 :发现微软的一个例子,最后隐藏了一些代码。 我的问题是这些: 在同一个委托实例上调用多个BeginInvoke调用是否安全,或者我是否必须为每个正在进行的方法调用构建一个新的委托实例? 如果我必须为每个实例构造新实例,是否有某种方法可以从IAsyncResult值中获取原始委托? 是否有其他更好的方法来为我的类添加异步支持而不是使用委托? 更多信息如下。 我正在为我的一类添加异步支持,并且认为我会这么做。 上这堂课: public class Calculator { public Int32 Add(Int32 a, Int32 b) { return a + b; } } 我以为我可以这样做: public class Calculator { private delegate Int32 AddDelegate(Int32 a, Int32 b); public Int32 Add(Int32 a, Int32 b) { return a + b; } public IAsyncResult BeginAdd(Int32 […]