Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    49

    [vb.net] Form non risponde

    Ciao a tutti!!
    Non riesco a far caricare un form ... Dal form principale chiamo un altro form contentente una label, una textbox ed un pulsante.. è solo grafico...

    Quando vado a chiamare il Form2 si apre la finestra ma si intravede lo sfondo del desktop o la finestra appare completamente bianca...
    La finestra 2 insomma non risponde...

    Quale potrebbe essere il problema?
    Ho letto che per risolvere questo problema basta aggiungere dopo l'istruzione form.show la seguente istruzione:
    System.Windows.Forms.Application.DoEvents()

    Ma nn cambia assolutamente nulla...

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Il codice nel Form2 ????
    Sbagliare è umano, perseverare è diabolico.

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    49
    il thread che chiama il form2 è questo:
    codice:
     Public Sub Dati()
            While True
                Try
    
                    Dim Bytes(Client.ReceiveBufferSize) As Byte
                    'Legge Client.ReceiveBufferSize bytes a partire dal primo
                    'dallo stream e li deposita In Bytes
                    'se ci sono bytes nulli, non verranno contati
                    'di Default, Client.ReceiveBufferSize = 8129
                    NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)
    
                    Dim AppoggioSwitch As String
                    Dim mess(0) As Byte
                    'Trasforma i bytes ricevuti In stringa
                    Dim dati As String
    
                    dati = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
    
    
                    If SwitchDati = False Then
    
                        AppoggioSwitch = Mid(dati, 1, 1)
    
    
                        If SwitchDati = False Then
    
    
                            mess(0) = CByte(0)
    
                            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(mess(0))
                            NetStr.Write(sendBytes, 0, sendBytes.Length)
    
    
                        End If
                    End If
                    'Visualizza il messaggio
    
                    Me.Invoke(MethodDelegateAddText, "[Ricezione dati]" & dati & vbLf & vbLf)
    
                    Me.Invoke(MethodDelegateAddText2, dati)
    
                    DatiStampa = Text2.Text
    
                    If dati.Contains("%%EOF") Then
                        mess(0) = CByte(0)
                        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(mess(0))
                        Client.Close()
    
                        Dim PsStart As Long
                        Dim PsEOF As Long
    
                        PsStart = InStr(1, DatiStampa, "%!PS")
    
                        PsEOF = InStr(1, DatiStampa, "%%EOF")
                      
                        DatiStampa = Mid(DatiStampa, PsStart)
                        Listener.Stop()
    
                        Dim fs As FileStream
                        fs = New FileStream("c:\test2.ps", IO.FileMode.Append)
                        Dim s As New StreamWriter(fs)
                        s.WriteLine(DatiStampa)
                        s.Close()
    
                        Form2.Show()
                        Form2.Enabled = True
                        Form2.Refresh()
    
                        System.Windows.Forms.Application.DoEvents()
                         'Form2.Show()
                        gsapi.ConvertFile()
    
                        System.IO.File.Delete("c:\test2.ps")
                       
    
                       
                        System.Threading.Thread.Sleep(200)
                        DatiStampa = ""
                        DatiIntestazione = ""
                        SwitchDati = False
                        Listener.Start()
    
    
                    End If
    
                    dati = Nothing
                    If AppoggioSwitch = Chr(3) Then
                        SwitchDati = True
                    End If
                Catch ex As Exception
    
                End Try
            End While
    
        End Sub
    Il form2 per ora è solo grafico non compie nessuna operazione!!
    quale può essere il problema?

  4. #4
    Così ad occhio mi sembra che quel While sia un loop infinito, senza vie di uscita... di conseguenza è normale che la finestra risulti bloccata. Cosa devi fare di preciso con questo codice?
    Chi non cerca trova.

  5. #5
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    49
    è un'applicazione server che si mette in ascolto sulla porta 515 ed attende la ricezione di dati!! quel thread, in particolare, riceve i dati inviati, li stampa in una casella di testo e converte i dati ricevuti in un file pdf!!

    Deve essere sempre vera altrimenti la esegue solo una volta...


    posto tutto il codice:
    codice:
    Imports System.Net.Sockets
    Imports System.Text
    Imports System.IO
    Imports System.Threading
    Imports System.Net
    Imports System.Threading.SynchronizationContext
    
    Public Class Form1
    
        Dim DatiStampa As String
        Dim DatiIntestazione
        Dim SwitchDati As Boolean
        Public Listener As New TcpListener(515)
        Dim Client As TcpClient
        Dim NetStr As NetworkStream
    
        Private Delegate Sub DelegateAddText(ByVal str As String)
        Private MethodDelegateAddText As New DelegateAddText(AddressOf AddText)
    
        Private Delegate Sub DelegateAddText2(ByVal str2 As String)
        Private MethodDelegateAddText2 As New DelegateAddText2(AddressOf AddText2)
    
        Private Sub AddText(ByVal str As String)
            Text1.Text = str & Text1.Text
        End Sub
        Private Sub AddText2(ByVal str As String)
            Text2.Text = Text2.Text & str
        End Sub
       
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim porta As Integer
            porta = 515
            Try
    
                Listener.Start()
                Dim serverThread As New Thread(New ThreadStart(AddressOf StartListen))
                serverThread.Start()
    
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub StartListen()
            While (True)
                Try
                    
                    Client = Listener.AcceptTcpClient()
                    'Inizializza lo stream
                    NetStr = Client.GetStream
                    ' Socket Information
    
                    'Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString())
    
                    ' Set Thread for each Web Browser Connection
    
                    Dim clientThread As New Thread(New ThreadStart(AddressOf Dati))
                    clientThread.Start()
    
                Catch ex As Exception
                    Console.WriteLine(ex.StackTrace.ToString())
    
                    'If Listener.Start Then
                    'clientSocket.Close()
                    ' End If
    
                End Try
            End While
    
        End Sub
    
        Public Sub Dati()
            While True
                Try
    
                    Dim Bytes(Client.ReceiveBufferSize) As Byte
                    'Legge Client.ReceiveBufferSize bytes a partire dal primo
                    'dallo stream e li deposita In Bytes
                    'se ci sono bytes nulli, non verranno contati
                    'di Default, Client.ReceiveBufferSize = 8129
                    NetStr.Read(Bytes, 0, Client.ReceiveBufferSize)
    
                    Dim AppoggioSwitch As String
                    Dim mess(0) As Byte
                    'Trasforma i bytes ricevuti In stringa
                    Dim dati As String
    
                    dati = System.Text.ASCIIEncoding.ASCII.GetString(Bytes)
    
    
                    If SwitchDati = False Then
    
                        AppoggioSwitch = Mid(dati, 1, 1)
    
    
                        If SwitchDati = False Then
    
    
                            mess(0) = CByte(0)
    
                            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(mess(0))
                            NetStr.Write(sendBytes, 0, sendBytes.Length)
    
    
                        End If
                    End If
                    'Visualizza il messaggio 
    
                    Me.Invoke(MethodDelegateAddText, "[Ricezione dati]" & dati & vbLf & vbLf)
    
                    Me.Invoke(MethodDelegateAddText2, dati)
    
                    DatiStampa = Text2.Text
    
                    If dati.Contains("%%EOF") Then
                        mess(0) = CByte(0)
                        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(mess(0))
                        Client.Close()
    
                        Dim PsStart As Long
                        Dim PsEOF As Long
    
                        PsStart = InStr(1, DatiStampa, "%!PS")
    
                        PsEOF = InStr(1, DatiStampa, "%%EOF")
    
                        DatiStampa = Mid(DatiStampa, PsStart)
                        Listener.Stop()
    
                        Dim fs As FileStream
                        fs = New FileStream("c:\test2.ps", IO.FileMode.Append)
                        Dim s As New StreamWriter(fs)
                        s.WriteLine(DatiStampa)
                        s.Close()
    
                        Form2.Show()
                        Form2.Enabled = True
                        Form2.Refresh()
    
                        System.Windows.Forms.Application.DoEvents()
                        'Form2.Show()
                        gsapi.ConvertFile()
    
                        System.IO.File.Delete("c:\test2.ps")
    
                        System.Threading.Thread.Sleep(200)
                        DatiStampa = ""
                        DatiIntestazione = ""
                        SwitchDati = False
                        Listener.Start()
    
    
                    End If
    
                    dati = Nothing
                    If AppoggioSwitch = Chr(3) Then
                        SwitchDati = True
                    End If
                Catch ex As Exception
    
                End Try
    
            End While
    
        End Sub
    
       
    End Class

  6. #6
    E che codice c'è dentro Form2?
    Chi non cerca trova.

  7. #7
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    49
    la form2 per ora è solo grafica. contiene solo: una label, una textbox ed un pulsante!

  8. #8
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Ma scusa a ogni ciclo crei un nuovo Thread che a sua volta esegue un loop infinito??


    codice:
    While (True)
                Try
                    
                    Client = Listener.AcceptTcpClient()
                    'Inizializza lo stream
                    NetStr = Client.GetStream
                    ' Socket Information
    
                    'Console.WriteLine("Client: " + clientInfo.Address.ToString() + ":" + clientInfo.Port.ToString())
    
                    ' Set Thread for each Web Browser Connection
    
                    Dim clientThread As New Thread(New ThreadStart(AddressOf Dati))
                    clientThread.Start()
    
                Catch ex As Exception
                    Console.WriteLine(ex.StackTrace.ToString())
    
                    'If Listener.Start Then
                    'clientSocket.Close()
                    ' End If
    
                End Try
            End While
    Qualcosa non quadra!
    Sbagliare è umano, perseverare è diabolico.

  9. #9
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    53
    quel while..... tremendo....
    almeno mettici una condizione che possa essere soddisfatta alla chiusura del form....

  10. #10
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    49
    Ho tolto il While true del metodo StartListener e ho aggiunto
    System.Windows.Forms.Application.DoEvents()
    dopo il while true del metodo Dati() e funziona!!

    non sono pratica con i thread... anzi è la prima volta che li uso...vabbe l'avrete capito... non ho capito ancora benissimo il funzionamento!!

    Volevo alleggerire il metodo Dati() creando un altro thread che una volta chiusa la connessione crea il file pdf ed apre la form2!!
    Come potrei fare??

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.