Application Not Responding ... ?

Joined
Nov 29, 2007
Messages
1
Programming Experience
5-10
I have a form. When the form loads, a connection to an image server is immediately established. A request is sent to the image server for an image, and the image data begins to come over. After all the data is received, and written to a file - all I want is for the image to show up in a picturebox.

The image that is now on my hard drive is FINE. it loads into any image previewing application.

The weird thing is, that once the image is finished saving, the form freezes (giving me a "NOT RESPONDING" message in the title bar). the weird thing is that once the image is saved to the HDD, the application really shouldn't be doing ANYTHIHNG. I can not for the life of me figure out whats hanging up the application... PLEASE HELP!





VB.NET:
Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports Microsoft.VisualBasic

Public Class frmImg

    Dim B() As Byte 'JPG Byte Handler
    Dim B2() As Byte 'JPG Byte Handler

    Dim picLength As Long
    Dim ExpectedLength As Long

    Dim EntranceName As String
    Dim EntryID As Integer
    Dim HasUnlock As Boolean
    Dim Server As String
    Dim Port As Integer

    Dim Client As TcpClient
    Dim NetStream As NetworkStream
    Dim DataBuffer(9000) As Byte

    Dim evtConnEstb As New AsyncCallback(AddressOf onConnectionEstablished)
    Dim evtDataArrival As New AsyncCallback(AddressOf DataProcessing)

    Public Sub New(ByVal vEntranceName As String, ByVal vEntryID As Integer, ByVal vHasUnlock As Boolean, _
ByVal vServer As String, ByVal vPort As Integer)

        InitializeComponent()

        'Set Modular Variables
        EntranceName = vEntranceName
        EntryID = vEntryID
        HasUnlock = vHasUnlock
        Server = vServer
        Port = vPort

        Show()

        'Immediately connect to image server
        Client = New TcpClient
        Client.BeginConnect(Server, Port, evtConnEstb, Nothing)

    End Sub

    Public Sub onConnectionEstablished(ByVal dr As IAsyncResult)

        'This sub is called as soon as the TCPClient connects.
        RequestImage()

    End Sub

    Sub RequestImage()
        Button1.Enabled = False

        'Send request for image
        Dim message As String = "E" & EntryID
        Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
        NetStream = Client.GetStream
        NetStream.Write(data, 0, data.Length)

        'Start to read response(s)
        NetStream.BeginRead(DataBuffer, 0, 9000, evtDataArrival, Nothing)
    End Sub

    Public Sub DataProcessing(ByVal dr As IAsyncResult)

        'This sub is called whenever data is received from the TCPClient

        'Read number of bytes in package
        Dim numberOfBytes As Long = NetStream.EndRead(dr)
        picLength = picLength + numberOfBytes

        'Translate the Bytes into a string
        Dim strData As String = System.Text.Encoding.ASCII.GetString(DataBuffer)

        'the term '§VIDS' or '?VIDS' Signifies the begining of new image data coming in.
        'That accounts for the first 5 bytes.

        'The next 9 bytes represent the number of bytes the algorythm should expect.
        'PicLength will always be 14 more than ExpectedLength due to this.

        If strData.Substring(0, 5) = "§VIDS" Or strData.Substring(0, 5) = "?VIDS" Then

            'Blank out B() which holds final image data WITH preceding 14 byte information
            ReDim B(0)

            'Assign image size from server to ExpectedLength
            ExpectedLength = strData.Substring(5, 9)

            Try
                'Delete file, if it exsits
                Kill("c:\test.jpg")
            Catch ex As Exception

            End Try

        End If

        'Assign the new data received to a byte array B2()
        B2 = DataBuffer

        'Attach new B2() Bytes onto the end of B()
        ReDim Preserve B((B.Length - 1) + (B2.Length - 1))
        B2.CopyTo(B, B.Length - B2.Length)

        Debug.Write("Expected Length: " & ExpectedLength _
& vbCrLf & "Actual Length: " & picLength - 14 & vbCrLf & strData.Length & " (" & numberOfBytes & ")" & vbCrLf & vbCrLf)

        'Check to see if file is done
        If (picLength - 14) = ExpectedLength Then

            'Remove the first 14 Bytes
            Dim FinalBytes(B.Length - 14) As Byte
            Dim I As Integer
            For I = 14 To B.Length - 1
                FinalBytes(I - 14) = B(I)
            Next

            'If file is done transferring, write the data to a file
            Dim fs As FileStream = New FileStream("c:\test.jpg", FileMode.OpenOrCreate)
            Dim w As BinaryWriter = New BinaryWriter(fs)
            w.Write(FinalBytes)
            w.Close()
            fs.Close()

            Debug.WriteLine("JPG File written: " & Date.Now & vbCrLf & vbCrLf)

'THIS IS WHERE I NOTICE THE APP FREEZING.
'EVEN THOUGH THE APP FREEZES, EVERYTHING SEEMS TO WORK PERFECTLY UP TO THAT POINT.

            Button1.Enabled = True

        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = Image.FromFile("C:\test.jpg")
    End Sub

End Class
 
BeginRead starts an asynchronous call that result in callback from different thread (so the application message pump isn't affected .paul.), you can't access UI elements across threads, I find it enthrilling this don't just give you a cross-thread exception. Try removing "Button1.Enabled = True".
 

Latest posts

Back
Top