正文

异步下载图片的方法2008-03-02 11:11:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/33107.html

分享到:

How to: Use Components That Support the Event-based Asynchronous Pattern  Many components provide you with the option of performing their work asynchronously. The SoundPlayer and PictureBox components, for example, enable you to load sounds and images "in the background" while your main thread continues running without interruption. Using asynchronous methods on a class that supports the Event-based Asynchronous Pattern Overview can be as simple as attaching an event handler to the component's MethodNameCompleted event, just as you would for any other event. When you call the MethodNameAsync method, your application will continue running without interruption until the MethodNameCompleted event is raised. In your event handler, you can examine the AsyncCompletedEventArgs parameter to determine if the asynchronous operation successfully completed or if it was canceled. For more information about using event handlers, see Event Handlers Overview (Windows Forms). The following procedure shows how to use the asynchronous image-loading capability of a PictureBox control. To enable a PictureBox control to asynchronously load an image Create an instance of the PictureBox component in your form. Assign an event handler to the LoadCompleted event. Check for any errors that may have occurred during the asynchronous download here. This is also where you check for cancellation. Visual Basic Copy CodeFriend WithEvents PictureBox1 As System.Windows.Forms.PictureBox C# Copy Codepublic Form1() { InitializeComponent(); this.pictureBox1.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.pictureBox1_LoadCompleted); } Visual Basic Copy CodePrivate Sub PictureBox1_LoadCompleted( _ ByVal sender As System.Object, _ ByVal e As System.ComponentModel.AsyncCompletedEventArgs) _ Handles PictureBox1.LoadCompleted If (e.Error IsNot Nothing) Then MessageBox.Show(e.Error.Message, "Load Error") ElseIf e.Cancelled Then MessageBox.Show("Load cancelled", "Canceled") Else MessageBox.Show("Load completed", "Completed") End If End Sub C# Copy Codeprivate void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message, "Load Error"); } else if (e.Cancelled) { MessageBox.Show("Load canceled", "Canceled"); } else { MessageBox.Show("Load completed", "Completed"); } } Add two buttons, called loadButton and cancelLoadButton, to your form. Add Click event handlers to start and cancel the download. Visual Basic Copy CodePrivate Sub loadButton_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles loadButton.Click ' Replace with a real url. PictureBox1.LoadAsync("http://www.tailspintoys.com/image.jpg") End Sub C# Copy Codeprivate void loadButton_Click(object sender, EventArgs e) { // Replace with a real url. pictureBox1.LoadAsync("http://www.tailspintoys.com/image.jpg"); } Visual Basic Copy CodePrivate Sub cancelLoadButton_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cancelLoadButton.Click PictureBox1.CancelAsync() End Sub C# Copy Codeprivate void cancelLoadButton_Click(object sender, EventArgs e) { pictureBox1.CancelAsync(); } Run your application. As the image download proceeds, you can move the form freely, minimize it, and maximize it.

阅读(2569) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册