none
How to make code below is faster and prevent hangs when timer works ? RRS feed

  • Question

  • problem

    How to make code below is faster and more performance good ?

    I have windows form have 4 controls picturebox and 4 labels and timer control work every second

    every picture box represent one gate as GatePic1,GatePic2,GatePic3,GatePic4 and 4 labels represent lblwelcome1,2,3,4

    after i press start button screen is heavy and some times make black screen for 2 seconds then after that every

    thing will hangs but not always hangs so that

    i try to write code more performance i need to solve this hangs and not repeated

    my code as below

    SELECT        ZKAccess.dbo.acc_monitor_log.id, ZKAccess.dbo.acc_monitor_log.device_id, ZKAccess.dbo.USERINFO.Badgenumber, ZKAccess.dbo.USERINFO.CardNo
    FROM            ZKAccess.dbo.acc_monitor_log LEFT OUTER JOIN
                             ZKAccess.dbo.USERINFO ON ZKAccess.dbo.acc_monitor_log.pin = ZKAccess.dbo.USERINFO.Badgenumber AND 
                             ZKAccess.dbo.acc_monitor_log.card_no = ZKAccess.dbo.USERINFO.CardNo
    WHERE        (ZKAccess.dbo.acc_monitor_log.id =
                                 (SELECT        TOP (1) id
                                    FROM            ZKAccess.dbo.acc_monitor_log AS acc_monitor_log_1
                                    ORDER BY id DESC)) AND (ZKAccess.dbo.acc_monitor_log.device_id =
                                 (SELECT        TOP (1) MachineId
                                    FROM            dbo.MachineSettingsData
                                    ORDER BY MachineId)) AND (ZKAccess.dbo.USERINFO.CardNo IS NOT NULL OR
                             ZKAccess.dbo.USERINFO.CardNo <> '') AND (ZKAccess.dbo.USERINFO.Badgenumber IS NOT NULL OR
                             ZKAccess.dbo.USERINFO.Badgenumber <> '')
    UNION ALL
    SELECT        ZKAccess.dbo.acc_monitor_log.id, ZKAccess.dbo.acc_monitor_log.device_id, ZKAccess.dbo.USERINFO.Badgenumber, ZKAccess.dbo.USERINFO.CardNo
    FROM            ZKAccess.dbo.acc_monitor_log LEFT OUTER JOIN
                             ZKAccess.dbo.USERINFO ON ZKAccess.dbo.acc_monitor_log.pin = ZKAccess.dbo.USERINFO.Badgenumber AND 
                             ZKAccess.dbo.acc_monitor_log.card_no = ZKAccess.dbo.USERINFO.CardNo
    WHERE        (ZKAccess.dbo.acc_monitor_log.id =
                                 (SELECT        TOP (1) id
                                    FROM            ZKAccess.dbo.acc_monitor_log AS acc_monitor_log_1
                                    WHERE        (device_id =
                                                                 (SELECT        MachineId
                                                                    FROM            dbo.MachineSettingsData AS a
                                                                    WHERE        (2 =
                                                                                                 (SELECT        COUNT(MachineId) AS Expr1
                                                                                                    FROM            dbo.MachineSettingsData AS b
                                                                                                    WHERE        (a.MachineId >= MachineId))))) AND (ZKAccess.dbo.USERINFO.CardNo IS NOT NULL OR
                                                             ZKAccess.dbo.USERINFO.CardNo <> '') AND (ZKAccess.dbo.USERINFO.Badgenumber IS NOT NULL OR
                                                             ZKAccess.dbo.USERINFO.Badgenumber <> '')
                                    ORDER BY id DESC))
    UNION ALL
    SELECT        ZKAccess.dbo.acc_monitor_log.id, ZKAccess.dbo.acc_monitor_log.device_id, ZKAccess.dbo.USERINFO.Badgenumber, ZKAccess.dbo.USERINFO.CardNo
    FROM            ZKAccess.dbo.acc_monitor_log LEFT OUTER JOIN
                             ZKAccess.dbo.USERINFO ON ZKAccess.dbo.acc_monitor_log.pin = ZKAccess.dbo.USERINFO.Badgenumber AND 
                             ZKAccess.dbo.acc_monitor_log.card_no = ZKAccess.dbo.USERINFO.CardNo
    WHERE        (ZKAccess.dbo.acc_monitor_log.id =
                                 (SELECT        TOP (1) id
                                    FROM            ZKAccess.dbo.acc_monitor_log AS acc_monitor_log_1
                                    WHERE        (device_id =
                                                                 (SELECT        MachineId
                                                                    FROM            dbo.MachineSettingsData AS a
                                                                    WHERE        (3 =
                                                                                                 (SELECT        COUNT(MachineId) AS Expr1
                                                                                                    FROM            dbo.MachineSettingsData AS b
                                                                                                    WHERE        (a.MachineId >= MachineId))))) AND (ZKAccess.dbo.USERINFO.CardNo IS NOT NULL OR
                                                             ZKAccess.dbo.USERINFO.CardNo <> '') AND (ZKAccess.dbo.USERINFO.Badgenumber IS NOT NULL OR
                                                             ZKAccess.dbo.USERINFO.Badgenumber <> '')
                                    ORDER BY id DESC))
    UNION ALL
    SELECT        ZKAccess.dbo.acc_monitor_log.id, ZKAccess.dbo.acc_monitor_log.device_id, ZKAccess.dbo.USERINFO.Badgenumber, ZKAccess.dbo.USERINFO.CardNo
    FROM            ZKAccess.dbo.acc_monitor_log LEFT OUTER JOIN
                             ZKAccess.dbo.USERINFO ON ZKAccess.dbo.acc_monitor_log.pin = ZKAccess.dbo.USERINFO.Badgenumber AND 
                             ZKAccess.dbo.acc_monitor_log.card_no = ZKAccess.dbo.USERINFO.CardNo
    WHERE        (ZKAccess.dbo.acc_monitor_log.id =
                                 (SELECT        TOP (1) id
                                    FROM            ZKAccess.dbo.acc_monitor_log AS acc_monitor_log_1
                                    WHERE        (device_id =
                                                                 (SELECT        MachineId
                                                                    FROM            dbo.MachineSettingsData AS a
                                                                    WHERE        (4 =
                                                                                                 (SELECT        COUNT(MachineId) AS Expr1
                                                                                                    FROM            dbo.MachineSettingsData AS b
                                                                                                    WHERE        (a.MachineId >= MachineId))))) AND (ZKAccess.dbo.USERINFO.CardNo IS NOT NULL OR
                                                             ZKAccess.dbo.USERINFO.CardNo <> '') AND (ZKAccess.dbo.USERINFO.Badgenumber IS NOT NULL OR
                                                             ZKAccess.dbo.USERINFO.Badgenumber <> '')
                                    ORDER BY id DESC))


    when run query above it run quickly may be less 1 second 

    i need to avoid any thing make hangs on my code csharp below

                                     

     public DataTable GetAllMachinesData()
            {
                string sql = "";

               sql = "select * from ViewAllMachinesData ";



                DataTable dtgetpunch = DataAccess.ExecuteDataTable(sql);
                return dtgetpunch;
            }

    private void TimerGate1_Tick(object sender, EventArgs e) { //2- Get Last Record On Database dtgetlastpunch = annaul.GetAllMachinesData(); if (dtgetlastpunch.Rows.Count > 0) { foreach (DataRow row in dtgetlastpunch.Rows) { CurrentId = Utilities.ObjectConverter.ConvertToLongInteger(row["id"]); MemberCodeMachine1 = Utilities.ObjectConverter.ConvertToString(row["Badgenumber"]); MachineId = Utilities.ObjectConverter.ConvertToInteger(row["device_id"]); //For Gate Number 1 if (MachineId == GetMachine1) { if (CurrentId == PreviousIdForMachine1) { CountGate1++; GetMembersData(MemberCodeMachine1, GetMachine1); } else { CountGate1 = 0; PreviousIdForMachine1 = CurrentId; } if (CountGate1 >= 30) { GatePic1.Image = null; lblwelcome1.Visible = false; } else { lblwelcome1.Visible = true; } } //For Gate Number 2 if (MachineId == GetMachine2) { if (CurrentId == PreviousIdForMachine2) { CountGate2++; GetMembersData(MemberCodeMachine1, GetMachine2); } else { CountGate2 = 0; PreviousIdForMachine2 = CurrentId; } if (CountGate2 >= 30) { GatePic2.Image = null; lblwelcome2.Visible = false; } else { lblwelcome2.Visible = true; } } //For Gate Number 3 if (MachineId == GetMachine3) { if (CurrentId == PreviousIdForMachine3) { CountGate3++; GetMembersData(MemberCodeMachine1, GetMachine3); } else { CountGate3 = 0; PreviousIdForMachine3 = CurrentId; } if (CountGate3 >= 30) { GatePic3.Image = null; lblwelcome3.Visible = false; } else { lblwelcome3.Visible = true; } } //For Gate Number 4 if (MachineId == GetMachine4) { if (CurrentId == PreviousIdForMachine4) { CountGate4++; GetMembersData(MemberCodeMachine1, GetMachine4); } else { CountGate4 = 0; PreviousIdForMachine4 = CurrentId; } if (CountGate4 >= 30) { GatePic4.Image = null; lblwelcome4.Visible = false; } else { lblwelcome4.Visible = true; } } } } //after 30 second //============== } private void BtnStart_Click(object sender, EventArgs e) { TimerGate1.Enabled = true; TimerGate1.Start(); }

    timer above work every second and enabled after press startbutton 

    if there are any new record then display his data and image on picturebox if no new record within 30 second then image and data will displayed 

    after 30 second data will hidden 

    • Moved by CoolDadTx Monday, June 10, 2019 7:03 PM Winforms related
    Saturday, June 8, 2019 12:57 AM

Answers

  • You just need to use a different timer. Try using the System.Threading.Timer, something like this:
     
    private long TIME_INTERVAL_IN_MILLISECONDS = 1000; // 1 second
    private System.Threading.Timer TimerGate1;
    private void BtnStart_Click(object sender, EventArgs e)
    {
        this.TimerGate1 = new System.Threading.Timer(TimerDoWork, null, TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
    }
    private void TimerDoWork(object state)
    {
        // Here you would put the code that you currently have in the TimerGate1_Tick (you won't have that event handler anymore)
        // You shouldn't need to bother with the Task.Run stuff ... this will actually run on a separate thread and
        // won't interfere with the UI thread
        // ...
        // ... all that code
        // ...
        
        // This will get the timer going again for the next 1 second
        this.timerThreading.Change(TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
    }
    

    Of course, you'll need some way of breaking out of this, but I don't think you've shown anything in the code you've posted for doing that. You could just set a boolean somewhere and test it in the TimerDoWork and don't call Change if you're ready to stop, which effectively stops the Timer.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, June 21, 2019 3:47 PM

All replies

  • Hello,

    Consider asynchronous delays even with the query running at an acceptable speed the control have no time to render which most likely is the reason for the hanging.

    e.g. await Task.Delay(x) where it might be 100.

    Consider not using SELECT *, spell out the fields you need. If possible consider using a data reader rather than a DataTable for returning data.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, June 8, 2019 4:15 AM
    Moderator
  • thank you for reply

    i modified code to accept reader but remaining used task.delay

     private void TimerGate1_Tick(object sender, EventArgs e)
            {
                //===========using data reader==================
                //2- Get Last Record On Database 
                
                SqlDataReader dr = annaul.executeReader(sql);
               
    
                    while (dr.Read())
                    {
                        CurrentId = Utilities.ObjectConverter.ConvertToLongInteger(dr["id"]);
                        MemberCodeMachine1 = Utilities.ObjectConverter.ConvertToString(dr["Badgenumber"]);
                        MachineId = Utilities.ObjectConverter.ConvertToInteger(dr["device_id"]);
                        //For Gate Number 1
                        if (MachineId == GetMachine1)
                        {
                            if (CurrentId == PreviousIdForMachine1)
                            {
                                CountGate1++;
                                GetMembersData(MemberCodeMachine1, GetMachine1);
                            }
                            else
                            {
                                CountGate1 = 0;
                                PreviousIdForMachine1 = CurrentId;
                            }
                            if (CountGate1 >= 30)
                            {
                                GatePic1.Image = null;
                                lblwelcome1.Visible = false;
                            }
                            else
                            {
                                lblwelcome1.Visible = true;
                            }
                        }
                        //For Gate Number 2
                        if (MachineId == GetMachine2)
                        {
                            if (CurrentId == PreviousIdForMachine2)
                            {
                                CountGate2++;
                                GetMembersData(MemberCodeMachine1, GetMachine2);
                            }
                            else
                            {
                                CountGate2 = 0;
                                PreviousIdForMachine2 = CurrentId;
                            }
                            if (CountGate2 >= 30)
                            {
                                GatePic2.Image = null;
                                lblwelcome2.Visible = false;
                            }
                            else
                            {
                                lblwelcome2.Visible = true;
                            }
                        }
                        //For Gate Number 3
                        if (MachineId == GetMachine3)
                        {
                            if (CurrentId == PreviousIdForMachine3)
                            {
                                CountGate3++;
                                GetMembersData(MemberCodeMachine1, GetMachine3);
                            }
                            else
                            {
                                CountGate3 = 0;
                                PreviousIdForMachine3 = CurrentId;
                            }
                            if (CountGate3 >= 30)
                            {
                                GatePic3.Image = null;
                                lblwelcome3.Visible = false;
                            }
                            else
                            {
                                lblwelcome3.Visible = true;
                            }
                        }
                        //For Gate Number 4
                        if (MachineId == GetMachine4)
                        {
                            if (CurrentId == PreviousIdForMachine4)
                            {
                                CountGate4++;
                                GetMembersData(MemberCodeMachine1, GetMachine4);
                            }
                            else
                            {
                                CountGate4 = 0;
                                PreviousIdForMachine4 = CurrentId;
                            }
                            if (CountGate4 >= 30)
                            {
                                GatePic4.Image = null;
                                lblwelcome4.Visible = false;
                            }
                            else
                            {
                                lblwelcome4.Visible = true;
                            }
                        }
                    }
    
                dr.Close();
    can you help me how to use task.delay

    Saturday, June 8, 2019 4:25 PM
  • I have .net framework 4.0

    How to use task.delay after one second and after 30 second

    as above

      TaskEx.Delay(ts).ContinueWith((x) =>
                   {
                       //run the code at the time
                //  methodToCall(date);
    
                       //setup call next day
                       runCodeAt(getNextDate(date, scheduler), scheduler);
    
                   }, m_ctSource.Token);

    Saturday, June 8, 2019 5:48 PM
  • How to modify code above accept 

    task.delay

    Sunday, June 9, 2019 7:50 AM
  • Hi 

    Thank you for posting here.

    Based on your description, I have some questions. If you provide the key information, it will be better for us to solve your problem.

    1. I think the sql sentence you provided is not used in the c# code, then what it is used for?

    2.It seems that you did not give the full information about the c# code, such as GetMachine1,CurrentIdPreviousIdForMachine1 field and GetMembersData method.

    Besides, If you wan to use task.delay method, you could refer to the following link.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.delay?view=netframework-4.8

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 10, 2019 8:12 AM
  • thank you for reply

    scenario i work above is consist of 4 gates every gate have number

    and i select last record for every machine from table acc_monitorlog zk database

    on form load i get machines 

     

    int GetMachine1,GetMachine2,GetMachine3,GetMachine4

    bool GeneralAssembly

    GetMachine1 = annaul.GetMachineForFirstRow(); GetMachine2 = annaul.GetMachineForSecondRow(); GetMachine3 = annaul.GetMachineForThirdRow(); GetMachine4 = annaul.GetMachineForFourRow(); public int GetMachineForFirstRow() { int machineid1 = 0; string SqlGetMachine1 = "select top 1 machineid from MachineSettingsData order by machineid asc "; machineid1 = Utilities.ObjectConverter.ConvertToInteger(DataAccess.ExecuteScalar(SqlGetMachine1)); return machineid1; } public int GetMachineForSecondRow() { int machineid2 = 0; string SqlGetMachine2 = "select MachineId from MachineSettingsData a where 2 = (select count(MachineId) from MachineSettingsData b where a.MachineId >= b.MachineId) "; machineid2 = Utilities.ObjectConverter.ConvertToInteger(DataAccess.ExecuteScalar(SqlGetMachine2)); return machineid2; } public int GetMachineForThirdRow() { int machineid3 = 0; string SqlGetMachine3 = "select MachineId from MachineSettingsData a where 3 = (select count(MachineId) from MachineSettingsData b where a.MachineId >= b.MachineId)"; machineid3 = Utilities.ObjectConverter.ConvertToInteger(DataAccess.ExecuteScalar(SqlGetMachine3)); return machineid3; } public int GetMachineForFourRow() { int machineid4 = 0; string SqlGetMachine4 = "select top 1 machineid from MachineSettingsData order by machineid desc "; machineid4 = Utilities.ObjectConverter.ConvertToInteger(DataAccess.ExecuteScalar(SqlGetMachine4)); return machineid4; }

      

    // under timer tick_gate 1 tick as first post

    public void GetMembersData(string MemberCodeValueData, int GateNumber) { string sqlImage = "select MemberImage,Name,GeneralAssembly FROM Members WHERE MemberCode = " + MemberCodeValueData + ""; SqlDataReader dr = annaul.executeReader(sqlImage); while (dr.Read()) { MemberName = Utilities.ObjectConverter.ConvertToString(dr["Name"]); GeneralAssembly = Utilities.ObjectConverter.ConvertToBoolean(dr["GeneralAssembly"]); if (Utilities.ObjectConverter.ConvertToString(dr["MemberImage"]) != "") { ImageMember = (byte[])dr["MemberImage"]; } //==============for Gate Number 1 if (GateNumber == GetMachine1) { GatePic1.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome1.ForeColor = System.Drawing.Color.Red; lblwelcome1.Visible = true; lblwelcome1.Text = "عفوا " + MemberName; } else { lblwelcome1.ForeColor = System.Drawing.Color.Green; lblwelcome1.Visible = true; lblwelcome1.Text = "مرحبا" + MemberName; } } //==============for Gate Number 2 if (GateNumber == GetMachine2) { GatePic2.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome2.ForeColor = System.Drawing.Color.Red; lblwelcome2.Visible = true; lblwelcome2.Text = "عفوا " + MemberName; } else { lblwelcome2.ForeColor = System.Drawing.Color.Green; lblwelcome2.Visible = true; lblwelcome2.Text = "مرحبا" + MemberName; } } // ==============================for Gate Number 3 if (GateNumber == GetMachine3) { GatePic3.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome3.ForeColor = System.Drawing.Color.Red; lblwelcome3.Visible = true; lblwelcome3.Text = "عفوا " + MemberName; } else { lblwelcome3.ForeColor = System.Drawing.Color.Green; lblwelcome3.Visible = true; lblwelcome3.Text = "مرحبا" + MemberName; } } //========================for Gate Number 4 if (GateNumber == GetMachine4) { GatePic4.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome4.ForeColor = System.Drawing.Color.Red; lblwelcome4.Visible = true; lblwelcome4.Text = "عفوا " + MemberName; } else { lblwelcome4.ForeColor = System.Drawing.Color.Green; lblwelcome4.Visible = true; lblwelcome4.Text = "مرحبا" + MemberName; } } //========================= } dr.Close(); }

    currentId and previousIdForMachine1 for every machine represent 
    currentid is last row per every machine
    previousIdForMachine1 is last row before current
    meaning if last id on table is 5 this is now current
    when new member come then new id =6 then 6 is current
    and 5 is previous 
    this idea applied for all machines 
    and currentId and previousIdForMachine1  is integer values
    

    first under button start timer click i write following

     private void BtnStart_Click(object sender, EventArgs e)
            {
               
    
                
    
         
               
                sql = "select* from ViewAllMachinesData";
                TimerGate1.Enabled = true;
                TimerGate1.Start();
                lblwelcome1.Visible = true;
                lblwelcome2.Visible = true;
                lblwelcome3.Visible = true;
                lblwelcome4.Visible = true;
            }


    second after that timer tick event work

    SqlDataReader dr = annaul.executeReader(sql);
    sql statment written under button start

    and 

    ViewAllMachinesData view  is written on first post

    and Gatepic1,Gatepic2,Gatepic3,Gatepic4 is picture boxes recieve images 

    based on last row

    gate1 is represent by lblwelcome1 and GatePic1

    gate2 is represnt by lblwelcom2 and Gatepic2

    gate3 is represnted by lblwelcom3 and Gatepic3 

    gate4 is represented by lblwelcom4 and Gatepic4


    Tuesday, June 11, 2019 8:03 AM
  • if any data needed no problem i can provide
    Tuesday, June 11, 2019 8:04 AM
  • code above is working but according to performance some times

    hangs with black screen to more clear to form code 

    timer tick 1 second =1000milisecond

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Forms; namespace gatesformslibrary { public partial class FrmTempCheck { string sql = ""; int GetMachine1 = 0; int GetMachine2 = 0; int GetMachine3 = 0; int GetMachine4 = 0; int CountGate1 = 0; int CountGate2 = 0; int CountGate3 = 0; int CountGate4 = 0; long PreviousIdForMachine1 = 0; long PreviousIdForMachine2 = 0; long PreviousIdForMachine3 = 0; long PreviousIdForMachine4 = 0; AnnualMember annaul = new AnnualMember(); DataTable dtgetlastpunch = new DataTable(); long CurrentId = 0; int HourValue = 0; int MinutesValue = 0; string MemberCodeMachine1 = ""; int MachineId = 0; DataTable dtgetmemberdata = new DataTable(); string MemberName = ""; bool GeneralAssembly = false; byte[] ImageMember; public FrmTempCheck() { InitializeComponent(); } public void FrmTempCheck_load(object sender, EventArgs e) { GetMachine1 = annaul.GetMachineForFirstRow(); GetMachine2 = annaul.GetMachineForSecondRow(); GetMachine3 = annaul.GetMachineForThirdRow(); GetMachine4 = annaul.GetMachineForFourRow(); }

    private void BtnStart_Click(object sender, EventArgs e)
            {
               
    
                
                sql = "select* from ViewAllMachinesData";
                TimerGate1.Enabled = true;
                TimerGate1.Start();
                lblwelcome1.Visible = true;
                lblwelcome2.Visible = true;
                lblwelcome3.Visible = true;
                lblwelcome4.Visible = true;
            }

    private void TimerGate1_Tick(object sender, EventArgs e) { //===========using data reader================== //2- Get Last Record On Database SqlDataReader dr = annaul.executeReader(sql); while (dr.Read()) { CurrentId = Utilities.ObjectConverter.ConvertToLongInteger(dr["id"]); MemberCodeMachine1 = Utilities.ObjectConverter.ConvertToString(dr["Badgenumber"]); MachineId = Utilities.ObjectConverter.ConvertToInteger(dr["device_id"]); //For Gate Number 1 if (MachineId == GetMachine1) { if (CurrentId == PreviousIdForMachine1) { CountGate1++; GetMembersData(MemberCodeMachine1, GetMachine1); } else { CountGate1 = 0; PreviousIdForMachine1 = CurrentId; } if (CountGate1 >= 30) { GatePic1.Image = null; lblwelcome1.Visible = true; } else { lblwelcome1.Visible = true; } } //For Gate Number 2 if (MachineId == GetMachine2) { if (CurrentId == PreviousIdForMachine2) { CountGate2++; GetMembersData(MemberCodeMachine1, GetMachine2); } else { CountGate2 = 0; PreviousIdForMachine2 = CurrentId; } if (CountGate2 >= 30) { GatePic2.Image = null; lblwelcome2.Visible = true; } else { lblwelcome2.Visible = true; } } //For Gate Number 3 if (MachineId == GetMachine3) { if (CurrentId == PreviousIdForMachine3) { CountGate3++; GetMembersData(MemberCodeMachine1, GetMachine3); } else { CountGate3 = 0; PreviousIdForMachine3 = CurrentId; } if (CountGate3 >= 30) { GatePic3.Image = null; lblwelcome3.Visible = true; } else { lblwelcome3.Visible = true; } } //For Gate Number 4 if (MachineId == GetMachine4) { if (CurrentId == PreviousIdForMachine4) { CountGate4++; GetMembersData(MemberCodeMachine1, GetMachine4); } else { CountGate4 = 0; PreviousIdForMachine4 = CurrentId; } if (CountGate4 >= 30) { GatePic4.Image = null; lblwelcome4.Visible = true; } else { lblwelcome4.Visible = true; } } } dr.Close(); }

    public void GetMembersData(string MemberCodeValueData, int GateNumber) { string sqlImage = "select MemberImage,Name,GeneralAssembly FROM Members WHERE MemberCode = " + MemberCodeValueData + ""; SqlDataReader dr = annaul.executeReader(sqlImage); while (dr.Read()) { MemberName = Utilities.ObjectConverter.ConvertToString(dr["Name"]); GeneralAssembly = Utilities.ObjectConverter.ConvertToBoolean(dr["GeneralAssembly"]); if (Utilities.ObjectConverter.ConvertToString(dr["MemberImage"]) != "") { ImageMember = (byte[])dr["MemberImage"]; } //==============for Gate Number 1 if (GateNumber == GetMachine1) { GatePic1.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome1.ForeColor = System.Drawing.Color.Red; lblwelcome1.Visible = true; lblwelcome1.Text = "عفوا " + MemberName; } else { lblwelcome1.ForeColor = System.Drawing.Color.Green; lblwelcome1.Visible = true; lblwelcome1.Text = "مرحبا" + MemberName; } } //==============for Gate Number 2 if (GateNumber == GetMachine2) { GatePic2.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome2.ForeColor = System.Drawing.Color.Red; lblwelcome2.Visible = true; lblwelcome2.Text = "عفوا " + MemberName; } else { lblwelcome2.ForeColor = System.Drawing.Color.Green; lblwelcome2.Visible = true; lblwelcome2.Text = "مرحبا" + MemberName; } } // ==============================for Gate Number 3 if (GateNumber == GetMachine3) { GatePic3.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome3.ForeColor = System.Drawing.Color.Red; lblwelcome3.Visible = true; lblwelcome3.Text = "عفوا " + MemberName; } else { lblwelcome3.ForeColor = System.Drawing.Color.Green; lblwelcome3.Visible = true; lblwelcome3.Text = "مرحبا" + MemberName; } } //========================for Gate Number 4 if (GateNumber == GetMachine4) { GatePic4.Image = annaul.ByteArrayToImagebyMemoryStream(ImageMember); if (GeneralAssembly == false) { lblwelcome4.ForeColor = System.Drawing.Color.Red; lblwelcome4.Visible = true; lblwelcome4.Text = "عفوا " + MemberName; } else { lblwelcome4.ForeColor = System.Drawing.Color.Green; lblwelcome4.Visible = true; lblwelcome4.Text = "مرحبا" + MemberName; } } //========================= } dr.Close(); } } }




    Tuesday, June 11, 2019 9:43 AM
  • can any one help me
    Wednesday, June 12, 2019 12:39 AM
  • Hi

    Thanks for giving the code.

    Because of two much code, I could not load it correctly. Could you provide the full project with me via onedrive?

    We are waiting for your update.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 12, 2019 9:41 AM
  • this is link of files 

    https://www.mediafire.com/file/86mar21ar6c2s8z/12-06-2019Gatefiles.rar/file

    have form and views scripts and class have functions

    Wednesday, June 12, 2019 12:10 PM
  • Hi 

    Thanks for giving the file.

    I see 'Dataform' in your code, so I want to know what your current project is, is winform applications or other applications?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 13, 2019 9:49 AM
  • thank you for reply

    Dataform is only one form have common design for my app

    same as master page on web not have any related data to this task

    to answer you question this is windows form app not other application

    my current project is windows form app .

    Thursday, June 13, 2019 10:54 AM
  • can any one help me
    Saturday, June 15, 2019 11:09 AM
  • what i need actually task.delay in place of timer in case of one second

    or after 30 second are this post already close and no answer or what

    are you need any data provide 

    Saturday, June 15, 2019 11:40 PM
  • i can abbrev my code as below :

    How to use thread or task.delay to work in small sample below

    i summarize code to two gates only

    private void TimerGate1_Tick(object sender, EventArgs e)
            {
               
    
               
                dtgetlastpunch = annaul.GetAllMachinesData();
                if (dtgetlastpunch.Rows.Count > 0)
                {
                    
                    foreach (DataRow row in dtgetlastpunch.Rows)
                    {
                        CurrentId = Utilities.ObjectConverter.ConvertToLongInteger(row["id"]);
                        MemberCodeMachine1 = Utilities.ObjectConverter.ConvertToString(row["Badgenumber"]);
                        MachineId = Utilities.ObjectConverter.ConvertToInteger(row["device_id"]);
                        //For Gate Number 1
                        if (MachineId == GetMachine1)
                        {
                            if (CurrentId == PreviousIdForMachine1)
                            {
                                CountGate1++;
                                GetMembersData(MemberCodeMachine1, GetMachine1);
                            }
                            else
                            {
                                CountGate1 = 0;
                                PreviousIdForMachine1 = CurrentId;
                            }
                            if (CountGate1 >= 30)
                            {
                                GatePic1.Image = null;
                                lblwelcome1.Visible = false;
                            }
                            else
                            {
                                lblwelcome1.Visible = true;
                            }
                        }
                        //For Gate Number 2
                        if (MachineId == GetMachine2)
                        {
                            if (CurrentId == PreviousIdForMachine2)
                            {
                                CountGate2++;
                                GetMembersData(MemberCodeMachine1, GetMachine2);
                            }
                            else
                            {
                                CountGate2 = 0;
                                PreviousIdForMachine2 = CurrentId;
                            }
                            if (CountGate2 >= 30)
                            {
                                GatePic2.Image = null;
                                lblwelcome2.Visible = false;
                            }
                            else
                            {
                                lblwelcome2.Visible = true;
                            }
                        }
                        
                    }
                  
    
    
                }
                
              
    
    
                //==============
            }
    
           

    Saturday, June 15, 2019 11:49 PM
  • I would also look at using Task.Run to do anything that can be slow like getting data from the sql server, converting bytes to an image, etc..  This will help keep the form from appearing unresponsive 

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=netframework-4.8

    Sunday, June 16, 2019 12:47 PM
  • can you modify code above with task.run and i will test it

    i dont know how to using task.run in my case 

    Sunday, June 16, 2019 1:16 PM
  • if this available to use task.run in place of timer tell me

    in all these post i ask to know are this available to use multi thread in place of code above

    until now i dont know that and how

    Sunday, June 16, 2019 1:27 PM
  • You can still use the timer. I would just do anything that can be slow in a task.Run

    var t = Task.Run(() => {

    GetMachine1 = annaul.GetMachineForFirstRow(); GetMachine2 = annaul.GetMachineForSecondRow(); GetMachine3 = annaul.GetMachineForThirdRow(); GetMachine4 = annaul.GetMachineForFourRow();

    });

    t.Wait();

    Monday, June 17, 2019 9:20 AM
  • thank you for reply

    this code above exist on load

    and load not have any slow performance

    the slow done one timer every second get data for members

    so how to apply task.run on code exist on timer that work every second as below

    private void TimerGate1_Tick(object sender, EventArgs e)
            {
               
    
               
                dtgetlastpunch = annaul.GetAllMachinesData();
                if (dtgetlastpunch.Rows.Count > 0)
                {
                    
                    foreach (DataRow row in dtgetlastpunch.Rows)
                    {
                        CurrentId = Utilities.ObjectConverter.ConvertToLongInteger(row["id"]);
                        MemberCodeMachine1 = Utilities.ObjectConverter.ConvertToString(row["Badgenumber"]);
                        MachineId = Utilities.ObjectConverter.ConvertToInteger(row["device_id"]);
                        //For Gate Number 1
                        if (MachineId == GetMachine1)
                        {
                            if (CurrentId == PreviousIdForMachine1)
                            {
                                CountGate1++;
                                GetMembersData(MemberCodeMachine1, GetMachine1);
                            }
                            else
                            {
                                CountGate1 = 0;
                                PreviousIdForMachine1 = CurrentId;
                            }
                            if (CountGate1 >= 30)
                            {
                                GatePic1.Image = null;
                                lblwelcome1.Visible = false;
                            }
                            else
                            {
                                lblwelcome1.Visible = true;
                            }
                        }
                        //For Gate Number 2
                        if (MachineId == GetMachine2)
                        {
                            if (CurrentId == PreviousIdForMachine2)
                            {
                                CountGate2++;
                                GetMembersData(MemberCodeMachine1, GetMachine2);
                            }
                            else
                            {
                                CountGate2 = 0;
                                PreviousIdForMachine2 = CurrentId;
                            }
                            if (CountGate2 >= 30)
                            {
                                GatePic2.Image = null;
                                lblwelcome2.Visible = false;
                            }
                            else
                            {
                                lblwelcome2.Visible = true;
                            }
                        }
                        
                    }
                  
    
    
                }
                
              
    
    
                //==============
            }
    

    Wednesday, June 19, 2019 3:26 PM
  • You just need to use a different timer. Try using the System.Threading.Timer, something like this:
     
    private long TIME_INTERVAL_IN_MILLISECONDS = 1000; // 1 second
    private System.Threading.Timer TimerGate1;
    private void BtnStart_Click(object sender, EventArgs e)
    {
        this.TimerGate1 = new System.Threading.Timer(TimerDoWork, null, TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
    }
    private void TimerDoWork(object state)
    {
        // Here you would put the code that you currently have in the TimerGate1_Tick (you won't have that event handler anymore)
        // You shouldn't need to bother with the Task.Run stuff ... this will actually run on a separate thread and
        // won't interfere with the UI thread
        // ...
        // ... all that code
        // ...
        
        // This will get the timer going again for the next 1 second
        this.timerThreading.Change(TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
    }
    

    Of course, you'll need some way of breaking out of this, but I don't think you've shown anything in the code you've posted for doing that. You could just set a boolean somewhere and test it in the TimerDoWork and don't call Change if you're ready to stop, which effectively stops the Timer.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, June 21, 2019 3:47 PM