B4J Question b4j and thread library issue

Discussion in 'B4J Questions' started by behnam_tr, Aug 14, 2019.

  1. behnam_tr

    behnam_tr Member

    hi
    i have a b4j app and i use threading library (b4a version in b4j)
    when i use "mythread.start" method i dont have a problem\
    but when i use "mythread.RunOnGuiThread" my app is hang until the operation end.
    in this method the sub are runing that load panel and leyout and tableview
    this app is telegram bot and have many users
    perhaps in onetime many users use the program and run this sub

    actually no different between use and not use thread in RunOnGuiThread for me
    It still hangs in two case (with thread and without thread)

    anyone can help me to solve this problem ??

    Code:
    Dim args(2As Object
    args(
    0)=from.Id
    args(
    1)=counter1
                  
    mythread.Priority=mythread.MinPriority
    mythread.RunOnGuiThread(
    "snapshot",args)
    my sub :

    Code:
    Sub snapshot(uid As Int,counter As Int)
      
        
    Dim p As Pane
        p.Initialize(
    "")
        p.LoadLayout(
    "hesabdari_printley")
        
    'p.SetSize(1100dip,900dip)
        p.Visible=False
        MainForm.RootPane.AddNode(p,
    0,0,1100,900)
      
        lbl_userfullname.Text=bardasht_list.get_fullname(uid)
    lbl_datehesabdari.Text=today&
    " "&DateTime.time(DateTime.Now)
      
        TableView1.SetColumnWidth(
    0,110)
        TableView1.SetColumnWidth(
    1,610)
        TableView1.SetColumnWidth(
    2,110)
        TableView1.SetColumnWidth(
    3,100)
        TableView1.SetColumnWidth(
    4,108)
        TableView1.SetColumnWidth(
    5,50)
        TableView1.SingleCellSelection=
    False
        TableView1.Items.Clear
      
        asJO(TableView1).RunMethod(
    "setFixedCellSize",Array(40.0))
      
        TableView1.Style=
    "-fx-font-size: 14;-fx-font-family: tahoma;"
        SetColumnStyle(
    0"-fx-alignment: CENTER;")
        SetColumnStyle(
    1"-fx-alignment: CENTER_right;")
        SetColumnStyle(
    2"-fx-alignment: CENTER;")
        SetColumnStyle(
    3"-fx-alignment: CENTER;")
        SetColumnStyle(
    4"-fx-alignment: CENTER;")
        SetColumnStyle(
    5"-fx-alignment: CENTER;")
      
        
    File.MakeDir(File.DirApp, "/user_reports")
      

        
    Dim RS As ResultSet=Sql1.ExecQuery2("SELECT * FROM hesabdari OFFSET LIMIT ?,?",Array As String(counter,19))
        
    Dim ii As Int=counter+1
        
    Do While RS.NextRow
          
            
    If RS.GetString("userid")=uid Then
          
                
    Dim Row(6As Object
                Row(
    5)=ii
                Row(
    4)=RS.GetString("type")
                Row(
    3)=NumberFormat( RS.GetString("mablagh"),0,0)
                Row(
    2)=NumberFormat( RS.GetString("mandeh") ,0,0)
                Row(
    1)=RS.GetString("tozihat")
                Row(
    0)= unix_to_time(RS.GetString("date"))
          
                TableView1.Items.Add(Row)
                ii=ii+
    1
                      
            
    End If
          
        
    Loop
      
        
        
    Dim bmp As B4XBitmap = Pane1.Snapshot
        
    Dim out As OutputStream
        
    Dim filename As String=uid&Rnd(1,200)&".png"
        out = 
    File.OpenOutput(File.DirApp&"/user_reports", filename, False)
        bmp.WriteToStream(out, 
    100"PNG")
        out.Close
        
    ''''''''''''''''''''''''''''''
        jtb2.sendPhoto(jtb2.byId(uid),jtb2.MediaByFile(File.DirApp&"/user_reports",filename),"test",True,Null,Null)
        
    '''''''''''''''''''''''''''
        File.Delete(File.DirApp&"/user_reports", filename)
          
    End Sub
     
    Last edited: Aug 14, 2019
  2. Erel

    Erel Administrator Staff Member Licensed User

    Don't use the Threading library.

    The correct solution is to switch to B4XTable.

    RunOnGuiThread means that you are running the sub on the main thread. It is better to call it directly. You cannot run that sub on a different thread as only the main thread can access the UI.
     
  3. behnam_tr

    behnam_tr Member

    thanks Erel

    in b4xtable for creating snapshot it should be visible =true
    but it not needed in tableview
    i need the visible = false

    i change sub and rewrite sub with b4xtable but It still hangs again
    when running this sub the app is not useable and cant do anythings in main form or other forms until compele sub

    in this code must p.visible=true else final image is empty

    Code:
    Sub snapshot(uid As Int,counter As Int)

    Dim args(1As Object
    args(
    0)=from.Id
    mythread.Priority=mythread.MinPriority
    mythread.Start(Me,
    "send_supp_info",args)



           
    Dim p As Pane
        p.Initialize(
    "")
        p.LoadLayout(
    "hesabdari_printley")
            p.Visible=
    False
        MainForm.RootPane.AddNode(p,
    0,0,1100,900)
        
        lbl_userfullname.Text=bardasht_list.get_fullname(uid)
        lbl_datehesabdari.Text=today&
    "  "&DateTime.time(DateTime.Now)
        
        
    Dim StateColumn As B4XTableColumn=B4XTable1.AddColumn("تاریخ",B4XTable1.COLUMN_TYPE_TEXT)
        StateColumn.Width = 
    110
        
    Dim StateColumn As B4XTableColumn=B4XTable1.AddColumn("توضیحات",B4XTable1.COLUMN_TYPE_TEXT)
        StateColumn.Width = 
    600
        
    Dim StateColumn As B4XTableColumn=B4XTable1.AddColumn("مانده حساب",B4XTable1.COLUMN_TYPE_TEXT)
        StateColumn.Width = 
    110
        
    Dim StateColumn As B4XTableColumn=B4XTable1.AddColumn("مبلغ",B4XTable1.COLUMN_TYPE_TEXT)
        StateColumn.Width = 
    100
        
    Dim StateColumn As B4XTableColumn=B4XTable1.AddColumn("نوع",B4XTable1.COLUMN_TYPE_TEXT)
        StateColumn.Width = 
    108
        
    Dim StateColumn As B4XTableColumn=B4XTable1.AddColumn("ردیف",B4XTable1.COLUMN_TYPE_TEXT)
        StateColumn.Width = 
    50
        
        B4XTable1.RowHeight=
    37
        

        
        
    File.MakeDir(File.DirApp, "/user_reports")
        
        
    Dim data As List
        data.Initialize
        
    '    Dim RS As ResultSet=Sql1.ExecQuery2("SELECT * FROM hesabdari WHERE userid = ? ", Array As Int(uid))
        Dim RS As ResultSet=Sql1.ExecQuery2("SELECT * FROM hesabdari OFFSET LIMIT ?,?",Array As String(counter,19))
        
    Dim ii As Int=counter+1
        
    Do While RS.NextRow
            
            
    If RS.GetString("userid")=uid Then
            
            
    Dim Row(6As Object
            Row(
    5)=ii
            Row(
    4)=RS.GetString("type")
            Row(
    3)=NumberFormat( RS.GetString("mablagh"),0,0)
            Row(
    2)=NumberFormat( RS.GetString("mandeh") ,0,0)
            Row(
    1)=RS.GetString("tozihat")
            Row(
    0)= unix_to_time(RS.GetString("date"))
            
                
            data.Add(
    Array(Row(0), Row(1), Row(2),Row(3),Row(4),Row(5)))
                
            ii=ii+
    1
                        
            
    End If
            
        
    Loop
        B4XTable1.SetData(data)
        Sleep(
    500)
          
            
    Dim bmp As B4XBitmap = Pane1.Snapshot
            
    Dim out As OutputStream
            
    Dim filename As String=uid&Rnd(1,20)&".png"
            out = 
    File.OpenOutput(File.DirApp&"/user_reports", filename, False)
            bmp.WriteToStream(out, 
    100"PNG")
            out.Close

    End sub
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    1. I write my answer again:

    Don't use the Threading library.

    2. You should never load layouts to zero sized panels.
     
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