wally_davis
asked on
Small vb.net app error: InvalidOperationException was unhandled
I have a VB.NET Windows form that has two buttons and a textbox.
One button will just open and read a text file and perform an Asynchronous operation and display text. (So, I've just written the routine for the buttonReadAsync_Click event and need to get the error resolved before moving onto the second button)
Second button will open and readt a text file and perform a Synchronous operation.
I'm still working on how to understand threading and don't quite have the hang of it yet. I've described what the error is below and where it happens in my code.
I would appreciate an explanation on what I missed and why it threw this error.
Thanks,
Wallace
One button will just open and read a text file and perform an Asynchronous operation and display text. (So, I've just written the routine for the buttonReadAsync_Click event and need to get the error resolved before moving onto the second button)
Second button will open and readt a text file and perform a Synchronous operation.
I'm still working on how to understand threading and don't quite have the hang of it yet. I've described what the error is below and where it happens in my code.
I would appreciate an explanation on what I missed and why it threw this error.
Thanks,
Wallace
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Text
Imports System.Threading
Public Class MainForm
Dim fs As System.IO.FileStream
Dim fileContents As Byte()
Dim callBack As AsyncCallback
Private Sub buttonReadAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonReadAsync.Click
OpenFileDialog1.ShowDialog()
callBack = New AsyncCallback(AddressOf fs_StateChanged)
fs = New System.IO.FileStream(OpenFileDialog1.FileName, _
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read, 4096, IO.FileOptions.None)
ReDim fileContents(CInt(fs.Length))
fs.BeginRead(fileContents, 0, CInt(fs.Length), callBack, Nothing)
End Sub <-- AFTER THIS LINE IS PROCESSED IT THROWS THE ERROR "Cross-thread operation not valid: Control 'textBoxResults' accessed from a thread other than the thread it was created on.
Private Sub fs_StateChanged(ByVal AsyncResult As IAsyncResult)
If AsyncResult.IsCompleted Then
textBoxResults.Text = Encoding.UTF8.GetString(fileContents)
fs.Close()
End If
End Sub
End Class
ASKER
I run into that exact example on Microsoft's website and tried to have the fileContents displayed in the textbox. It displayed the Windows Form Title Text. So, I tried to pass in the "fileContents" Array variable and it couldn't convert from Byte to String datatype. So, I'm a bit stuck and am trying to find a way to implement this in code. If you or anyone would know how to update the code, that would help me understand what I'm doing wrong.
Thanks!
Thanks!
ASKER
p.s. I pulled this code from a Book CopyRighted in 2004, so, what worked then doesn't work now. I typed this all out and stepped through the code in hopes to understand Network Programming better but this sucks. The name of the book is Network Programming in .NET: With C# and Visual Basic .NET by Fiach Reid. I'm not sure why the example would work then vs. now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx