遇到跨线程操作困难

编程入门 行业动态 更新时间:2024-10-24 22:25:55
本文介绍了遇到跨线程操作困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要做的就是使用OpenFileDialog选择图片,然后通过编辑.imagelocation属性将其加载到窗体的PictureBox中. 仅此而已;一个非常简单的任务.我不是在尝试开发多线程应用程序.我 不在乎使用了多少个线程,一个很好! 有没有一种方法可以在我的应用程序中关闭多线程,以便我可以像以前一样进行开发?如果没有,那么我正在尝试执行以下帖子中建议的操作:

All I want to do is use the OpenFileDialog to select a picture and then load it into a PictureBox in a form by editing the .imagelocation property. That's all; a very simple task. I wasn't trying to develop a multi-threaded application. I don't care how many threads are used, one is fine! Is there a way to just turn off Multi-threading in my application so that I can just develop as I've always done before? If not, well then I'm trying to do what is suggested in the following post:

> social .msdn.microsoft/Forums/zh-CN/vbgeneral/thread/f41bb09a-e13d-46db-a9ec-2958d7415eb3?prof = required

但是我不知道在BackgroundWorker1_DoWork过程中该怎么做.我正在尝试仅更改.imagelocation属性并显示它. 我真的必须使用阅读器并在读取图像时不断更新其进度吗? 这 对我来说没有意义.这是我到目前为止的代码,请花点时间告诉我我哪里出错了.谢谢.

But I don't know what to do in the BackgroundWorker1_DoWork procedure. I'm attempting to change the .imagelocation property only and display it. Do I really have to use a reader and constantly update it's progress as it reads the image? This doesn't make sense to me. Here's the code I have so far, please take a peak and tell me where I've gone wrong. Thank you kindly.

Imports System.Windows.Forms Imports System.IO Imports System.ComponentModel Public Class TaskPaneControl Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click Dim fdlg As OpenFileDialog = New OpenFileDialog() fdlg.Title = "Picture Duplicator Open File Dialog" fdlg.InitialDirectory = "C:\" fdlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*" fdlg.FilterIndex = 2 fdlg.RestoreDirectory = True If fdlg.ShowDialog() = DialogResult.OK Then With Me.BackgroundWorker1 .WorkerReportsProgress = True .RunWorkerAsync(fdlg.FileName) End With End If End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 'Dim filename As String = e.Argument 'Using reader As New StreamReader(filename) ' While Not reader.EndOfStream ' Me.BackgroundWorker1.ReportProgress(0.0, reader.ReadLine()) ' End While 'End Using End Sub Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Me.pbPictureBox.ImageLocation = e.UserState End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted MsgBox("Done reading the file!") End Sub End Class

推荐答案

我建议您删除BackgroundWorker内容,然后仅设置Picturebox.ImageLocation属性.

更多推荐

遇到跨线程操作困难

本文发布于:2023-11-27 11:58:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   困难   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!