Italian thread vaffa

ivanomonti

Expert
Licensed User
Longtime 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
 

Star-Dust

Expert
Licensed User
Longtime 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
B4A ? B4J?
Che intendi esattamente?
 

ivanomonti

Expert
Licensed User
Longtime User
B4A ? B4J?
Che intendi esattamente?

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

B4X:
#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 String) As 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


 

LucaMs

Expert
Licensed User
Longtime 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?
 
D

Deleted member 103

Guest
b4j intendo che non mi parte il thread e mi da errore di Exception : wrong number of arguments pur non passando nulla o oggetto
prova così:
B4X:
Sub Button_open_Click
   dim arg(0) as Object
    If Thread1.Start(Me, "AsyncSetMap", arg) = True Then
        ProgressBar1.Progress = -1
    End If
End Sub
 

ivanomonti

Expert
Licensed User
Longtime User
prova così:
B4X:
Sub Button_open_Click
   dim arg(0) as Object
    If Thread1.Start(Me, "AsyncSetMap", arg) = True Then
        ProgressBar1.Progress = -1
    End If
End Sub

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

ivanomonti

Expert
Licensed User
Longtime User

ivanomonti

Expert
Licensed User
Longtime 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/

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

B4X:
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



B4X:
Private Sub AsyncThread(row As Object)

    ProgressBar1.Progress = row
    
End Sub
 

OliverA

Expert
Licensed User
Longtime 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
B4X:
For i=1 To list.Size-1
   Thread1.Initialise("Thread1") '<-------------------------------------------------------------------------------------
   Thread1.RunOnGuiThread("AsyncThread", Array As Object(i)) '<
Try
B4X:
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).
 
D

Deleted member 103

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

Questo sarebbe il funzionamento in principio, senza provato.
B4X:
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(0) As Object
    Thread1.Start(Me, "AsyncSetMap", arg)
End Sub

Private Sub AsyncSetMap(obj As Object)
    Dim obj(1) As 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
 

Star-Dust

Expert
Licensed User
Longtime User
Qual'é la differenza?
La usate nella stessa maniera
 

Star-Dust

Expert
Licensed User
Longtime User
Non è la stesse maniera, da un "thraed" non si possono manipolare le View di una Activity direttamente, devi usare altri Sub.
Mi pare che ha suggerito proprio queato, almeno così ho capito
 

OliverA

Expert
Licensed User
Longtime 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)
B4X:
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)
B4X:
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)
B4X:
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)
 
Top