Italian thread vaffa

Discussion in 'Italian Forum' started by ivanomonti, Apr 21, 2018.

  1. ivanomonti

    ivanomonti Well-Known Member Licensed User

    perché ci litigo sempre e vincono loro,,,, cazzzz

    chi ha un esempio finito per lanciare un sub da thread separato del mainform, ringrazio anticipatamente
     
  2. Star-Dust

    Star-Dust Expert Licensed User

    B4A ? B4J?
    Che intendi esattamente?
     
  3. ivanomonti

    ivanomonti Well-Known Member Licensed User

    b4j intendo che non mi parte il thread e mi da errore di Exception : wrong number of arguments pur non passando nulla o oggetto

    Code:
    #Region Project Attributes
        
    #MainFormWidth: 600
        
    #MainFormHeight: 600
        
    #CommandLineArgs:
        
    #AdditionalJar: sqlite-jdbc-3.7.2
    #End Region

    Sub Process_Globals
        
    Private fx As JFX
        
    Private MainForm As Form
        
    Private Button_close As Button
        
    Private Button_open As Button
        
    Private TableView_list As TableView
        
    Private sqlite As class_sqlite
        
    Private ProgressBar1 As ProgressBar
        
    Private Thread1 As Thread
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
        MainForm.RootPane.LoadLayout(
    "Layout1"'Load the layout file.
        MainForm.Show
        sqlite.Initialize
        Thread1.Initialise(
    "Thread1")
        resize_column_tableview
    End Sub

    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
        
    Return True
    End Sub

    Sub Button_open_Click
        
    If Thread1.Start(Me, "AsyncSetMap"Null) = True Then
            ProgressBar1.Progress = -
    1
        
    End If
    End Sub

    Sub Thread1_Ended(endedOK As Boolean, error As String)
        
    Log(error)
        ProgressBar1.Progress = 
    0
        AsyncSetMap(
    Null)
    End Sub

    Private Sub AsyncSetMap(obj As Object)

        
    Dim FC As FileChooser
        FC.Initialize
        FileChooserUtils.AddExtensionFilters4(FC,
    Array As String("CSV","Text FIles"),Array As String("*.csv","*.txt"),True,"Output Files",True)
        FC.Title = 
    "Open file CSV"
        
    Dim FileName As String = FC.ShowOpen(MainForm)
        
    Dim DirParent As String = FC.InitialDirectory
        FileName = FileName.Replace(DirParent & 
    "\","")' C:\Users\ivanomonti\Desktop\ExportSelfAgent.csv

        
    Dim myTextReader As TextReader
        myTextReader.Initialize(
    File.OpenInput(DirParent,FileName))
        
    Dim values As String = myTextReader.ReadAll
        myTextReader.Close
            
        
    Dim list As List
        
    list.Initialize
        
    list = Regex.Split(Chr(10),values)
            
        
    Dim count, error As Int
        count = 
    0
        error = 
    0
            
        TableView_list.Items.Clear
            
        
    Dim listexport As String
            
        
    For i=1 To list.Size-1
            
    Try
                count = count + 
    1
                values = 
    list.Get(i)
                
    Dim m() As String = Regex.Split(";",values.Replace("""","").Replace("=","").Replace(Chr(10),""))
                
    Dim mp As Map
                mp.Initialize
                mp.Put(
    "Name",m(4))
                mp.Put(
    "Surname","")
                mp.Put(
    "Phone",m(0))
                mp.Put(
    "Status",m(14))
                
    If sqlite.client_add(mp) = True Then
                    
    Dim Row() As Object = Array ( m(4).ToUpperCase, m(0).ToUpperCase, m(5).ToLowerCase & " " & m(6).ToLowerCase, m(14).ToLowerCase,m(15).ToLowerCase)
                
    End If
                TableView_list.Items.Add(Row)
            
    Catch
                count = count-
    1
                error = error+
    1
                listexport = listexport & 
    Chr(10) & i & " " & values
            
    End Try
        
    Next
        
    File.WriteString(DirParent,"Report.txt",listexport)
        fx.Msgbox(MainForm,
    "The file " & FileName & " was successfully imported, we found errors (" & error & ") found in the saved report","Alert")
        fx.ShowExternalDocument(
    File.GetUri(DirParent,"Report.txt"))
    End Sub

    Sub Button_save_Click
        
    End Sub

    Sub resize_column_tableview
        TableView_list.SetColumnWidth(
    0, TableView_list.PrefWidth*25/100)
        TableView_list.SetColumnWidth(
    1, TableView_list.PrefWidth*5/100)
        TableView_list.SetColumnWidth(
    2, TableView_list.PrefWidth*15/100)
        TableView_list.SetColumnWidth(
    3, TableView_list.PrefWidth*15/100)
        TableView_list.SetColumnWidth(
    4, TableView_list.PrefWidth*40/100)
    End Sub

    Sub Button_close_Click
        
    ExitApplication
    End Sub

    Sub TableView_list_Resize (Width As Double, Height As Double)
        resize_column_tableview
    End Sub

     
  4. LucaMs

    LucaMs Expert Licensed User

    thread vaffa

    Forse M5S? Ah, no, quelli hanno rinnegato.
     
    Last edited: Apr 22, 2018
  5. LucaMs

    LucaMs Expert Licensed User

    Quale libreria hai usato?

    Di sicuro se crei un websocket server si riesce a creare più thread, anzi, vengono creati in automatico, e non ci sono problemi; ma non trovo una libreria b4j che consenta di creare thread: non è che stai usando quella per b4a in un progetto b4j?
     
  6. Filippo

    Filippo Expert Licensed User

    prova così:
    Code:
    Sub Button_open_Click
       
    dim arg(0as Object
        
    If Thread1.Start(Me, "AsyncSetMap", arg) = True Then
            ProgressBar1.Progress = -
    1
        
    End If
    End Sub
     
  7. ivanomonti

    ivanomonti Well-Known Member Licensed User

    non ha funzionato questo sistema, ma riprovo più tardi e di aggiorno
     
  8. Filippo

    Filippo Expert Licensed User

    Il problema non è come chiami il Sub "AsyncSetMap", perché così è giusto, ma quello che c'è nel Sub.
    Da un "thraed" non si possono manipolare le View di una Activity direttamente, devi usare altri Sub; guarda gli esempi nella Threading-Library: https://www.b4x.com/android/forum/threads/threading-library.6775/
     
  9. ivanomonti

    ivanomonti Well-Known Member Licensed User

    e android l'esempio, vale anche per b4j
     
  10. ivanomonti

    ivanomonti Well-Known Member Licensed User

    la verità che la videata anche se volessi fare progressbar e il suo avanzamento non riesco

    Code:
    Sub Button_open_Click
        
            
    If RadioButton_telmar.Selected = True Then
                 
    Try
                    
    Dim FC As FileChooser
                    FC.Initialize
                    FileChooserUtils.AddExtensionFilters4(FC,
    Array As String("CSV","Text FIles"),Array As String("*.csv","*.txt"),True,"Output Files",True)
                    FC.Title = 
    "Open file CSV"
                    FileName = FC.ShowOpen(MainForm)
                    DirParent = FC.InitialDirectory
                
    Catch
                    
    Log(LastException)
                    
    Return
                
    End Try
            
                FileName = FileName.Replace(DirParent & 
    "\","")

                
    Dim myTextReader As TextReader
                myTextReader.Initialize(
    File.OpenInput(DirParent,FileName))
                values = myTextReader.ReadAll
                myTextReader.Close
                
                
    list.Initialize
                
    list = Regex.Split(Chr(10),values)
                
                count = 
    0
                errorcount = 
    0
                
                TableView_list.Items.Clear
                
                
    For i=1 To list.Size-1

                    Thread1.Initialise(
    "Thread1"'<-------------------------------------------------------------------------------------
                    Thread1.RunOnGuiThread("AsyncThread"Array As Object(i)) '<-------------------------------------------------------------------------------------
                    
                
    Try
                    count = count + 
    1
                    values = 
    list.Get(i)
                    
    Dim m() As String = Regex.Split(";",values.Replace("""","").Replace("=","").Replace(Chr(10),""))
                    
    Dim mp As Map
                    mp.Initialize
                    mp.Put(
    "Name",m(4))
                    mp.Put(
    "Surname","")
                    mp.Put(
    "Phone",m(0))
                    mp.Put(
    "Status",m(14))
                    
    If sqlite.client_add(mp) = True Then
                        
    Dim rows() As Object = Array ( m(4).ToUpperCase, m(0).ToUpperCase, m(5).ToLowerCase & " " & m(6).ToLowerCase, m(14).ToLowerCase,m(15).ToLowerCase)
                        TableView_list.Items.Add(rows)
                    
    End If
                
    Catch
                    count = count-
    1
                    errorcount = errorcount+
    1
                    listexport = listexport & 
    Chr(10) & i & " " & values
                
    End Try
                
                
    Next
                
            
    Else
                fx.Msgbox(MainForm,
    "form to be completed","Alert")
                
    Return
            
    End If
            
    End Sub


    Code:
    Private Sub AsyncThread(row As Object)

        ProgressBar1.Progress = row
        
    End Sub
     
  11. OliverA

    OliverA Well-Known Member Licensed User

    I think I finally get what you are trying to do. Instead of this
    Google Translate: Penso di avere finalmente quello che stai cercando di fare. Invece di questo
    Code:
    For i=1 To list.Size-1
       Thread1.Initialise(
    "Thread1"'<-------------------------------------------------------------------------------------
       Thread1.RunOnGuiThread("AsyncThread"Array As Object(i)) '<
    Try
    Code:
    For i=1 To list.Size-1
       ProgressBar1.Progress = (
    100/list.Size) * i
       Sleep(
    0)
    The Sleep(0) will make your sub Button_open_Click resumable, allowing the GUI to process some events before returning to the statement after Sleep(0).
    Google Translate: Sleep (0) renderà il tuo Button_open_Click secondario ripristinabile, consentendo alla GUI di elaborare alcuni eventi prima di tornare all'istruzione dopo Sleep (0).
     
  12. Filippo

    Filippo Expert Licensed User

    Scusa, ma penso che tu non abbia capito il principio di come funziona la libreria Threading. :(

    Questo sarebbe il funzionamento in principio, senza provato.
    Code:
    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
        MainForm.RootPane.LoadLayout(
    "Layout1"'Load the layout file.
        MainForm.Show
        sqlite.Initialize
        Thread1.Initialise(
    "Thread1")
        resize_column_tableview
    End Sub

    Sub Button_open_Click
        
    Dim FC As FileChooser
        FC.Initialize
        FileChooserUtils.AddExtensionFilters4(FC,
    Array As String("CSV","Text FIles"),Array As String("*.csv","*.txt"),True,"Output Files",True)
        FC.Title = 
    "Open file CSV"
        
    Dim FileName As String = FC.ShowOpen(MainForm)
        
    Dim DirParent As String = FC.InitialDirectory
        FileName = FileName.Replace(DirParent & 
    "\","")' C:\Users\ivanomonti\Desktop\ExportSelfAgent.csv

        
    Dim myTextReader As TextReader
        myTextReader.Initialize(
    File.OpenInput(DirParent,FileName))
        
    Dim values As String = myTextReader.ReadAll
        myTextReader.Close
            
        
    Dim list As List
        
    list.Initialize
        
    list = Regex.Split(Chr(10),values)
            
        
    Dim count, error As Int
        count = 
    0
        error = 
    0
            
        TableView_list.Items.Clear
            
        
    Dim listexport As String
        
        ProgressBar1.Progress = -
    1

        
    Dim arg(0As Object
        Thread1.Start(Me, 
    "AsyncSetMap", arg)
    End Sub

    Private Sub AsyncSetMap(obj As Object)
        
    Dim obj(1As Object
                
        
    For i=1 To list.Size-1
            
    Try
                count = count + 
    1
                values = 
    list.Get(i)
                
    Dim m() As String = Regex.Split(";",values.Replace("""","").Replace("=","").Replace(Chr(10),""))
                
    Dim mp As Map
                mp.Initialize
                mp.Put(
    "Name",m(4))
                mp.Put(
    "Surname","")
                mp.Put(
    "Phone",m(0))
                mp.Put(
    "Status",m(14))
                
    If sqlite.client_add(mp) = True Then
                    
    Dim Row() As Object = Array ( m(4).ToUpperCase, m(0).ToUpperCase, m(5).ToLowerCase & " " & m(6).ToLowerCase, m(14).ToLowerCase,m(15).ToLowerCase)
                
    End If
                
                obj(
    0) = Row
                Thread1.RunOnGuiThread(
    "AddTableItem", obj)
            
    Catch
                count = count-
    1
                error = error+
    1
                listexport = listexport & 
    Chr(10) & i & " " & values
            
    End Try
        
    Next
        
    End Sub

    Sub AddTableItem(obj1 As Object)
        
    Dim Row() As Object = obj1
        TableView_list.Items.Add(Row)
    End Sub

    Sub Thread1_Ended(endedOK As Boolean, error As String)
        
    Log(error)
        
    File.WriteString(DirParent,"Report.txt",listexport)
        fx.Msgbox(MainForm,
    "The file " & FileName & " was successfully imported, we found errors (" & error & ") found in the saved report","Alert")
        fx.ShowExternalDocument(
    File.GetUri(DirParent,"Report.txt"))

        ProgressBar1.Progress = 
    0
    End Sub
     
  13. Star-Dust

    Star-Dust Expert Licensed User

    Qual'é la differenza?
    La usate nella stessa maniera
     
  14. Filippo

    Filippo Expert Licensed User

    Non è la stesse maniera, da un "thraed" non si possono manipolare le View di una Activity direttamente, devi usare altri Sub.
     
  15. Star-Dust

    Star-Dust Expert Licensed User

    Mi pare che ha suggerito proprio queato, almeno così ho capito
     
  16. LucaMs

    LucaMs Expert Licensed User

    Glielo ha già detto Erel.

    Io, ammettendo di non aver letto tutto, mi domando: ma non si può trovare il modo di evitare di usare i thread? Quale è il motivo per utilizzarli?
     
  17. OliverA

    OliverA Well-Known Member Licensed User

    LucaMs likes this.
  18. OliverA

    OliverA Well-Known Member Licensed User

    Another way (untested) is to create a class that processes the CSV file. The class is initialized with a callback object and method (example: Me, "Update"). Instead of
    (Un altro modo (non testato) è creare una classe che elabora il file CSV. La classe è inizializzata con un oggetto e un metodo callback (esempio: Me, "Aggiornamento"). Invece di)
    Code:
    If sqlite.client_add(mp) = True Then
       
    Dim rows() As Object = Array ( m(4).ToUpperCase, m(0).ToUpperCase, m(5).ToLowerCase & " " & m(6).ToLowerCase, m(14).ToLowerCase,m(15).ToLowerCase)
       TableView_list.Items.Add(rows)
    End If
    have (avere)
    Code:
    If sqlite.client_add(mp) = True Then
       
    Dim rows() As Object = Array ( m(4).ToUpperCase, m(0).ToUpperCase, m(5).ToLowerCase & " " & m(6).ToLowerCase, m(14).ToLowerCase,m(15).ToLowerCase)
       
    CallSub3(aSub, "_rowtoadd", rows, (100/list.Size) * i)
    End If
    Note: The above code is in the new class (Il codice sopra è nella nuova classe)

    Then in the calling module have (Quindi nel modulo di chiamata hanno)
    Code:
    Sub Update_rowtoadd(rows() As Object, progress as Int)
       TableView_list.Items.Add(rows)
       ProgressBar1.Progress = (
    100/list.Size) * i
    End Sub
    The CallSub allows for event processing and the GUI should remain responsive (CallSub consente l'elaborazione degli eventi e la GUI dovrebbe rimanere reattiva)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice