遇到跨线程操作困难 [英] Having difficulty with Cross-Thread operation stuff

查看:64
本文介绍了遇到跨线程操作困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的就是使用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:

> http://social .msdn.microsoft.com/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属性.
 


这篇关于遇到跨线程操作困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆