none
How to implement backgroundworker for two picture box when get last record based on certin time ? RRS feed

  • Question

  • problem

    How to implement background worker for two picture box when get last record based on certain time ?

    I dont need function to retrieve image i need How to use background worker when using function below to retrieve image ?

    I make windows form access from two computers

    every computer related to picture box meaning 

    I have windows form have two controls picture box

    picturebox1 represent last image per Machine number 1

    picturebox2 represent last image per Machine Number 2

    I need to get last Image based on ID  on table checkinout

    Every second
    if machineid =1
    {
    then it will show image per last record on picture box1
    }
    if (machineid =2)
    {
    then it will show image per last record on picture box2

    And after that will display image per every picture box for 30 second based on Machine ID


    Machine 1


    if (no record added withine 30 second per machine 1)

    {

    then picturebox1 will display image per last id on machine 1 for 30 second

    }

    else if after 30 second

    {

    picturebox1.image will be null

    }

    else

    {

    check every second
    if there are new record added per machine 1
    then show image per that record on picturebox1

    }

    Machine 2


    if no record added within 30 second per machine 2

    {

    then picturebox2 will display image per last id on machine 2

    }

    else if (after 30 second)

    {

    picturebox2.image will be null

    }

    else

    {

    check every second
    if there are new record added per machine 2
    then show image per that record on picturebox2
    }
    checkinout TABLE

    UserId  MachineId  checktime  checktype
    202     1          09:05:15    1
    105     2          09:10:15    1
    190     2          09:50:15    1
    300     1          10:05:15    1

    two picture box will work on same time 
    How to achieve that task by background worker please 
    can you help me on that if possible


    public DataTable GetImageById()
            {
                string sql = sql="SELECT      dbo.USERINFO.picture , CAST(format(dbo.CHECKINOUT.CHECKTIME, 'yyyyMMhhmmss') AS numeric) AS CHECKTIME, dbo.CHECKINOUT.USERID, 
                             dbo.USERINFO.CardNo, dbo.CHECKINOUT.CHECKTYPE, dbo.CHECKINOUT.MachineId
    FROM            dbo.CHECKINOUT LEFT OUTER JOIN
                             dbo.USERINFO ON dbo.CHECKINOUT.USERID = dbo.USERINFO.USERID
    WHERE        (dbo.CHECKINOUT.CHECKTIME =
                                 (SELECT        TOP (1) CHECKTIME
                                    FROM            dbo.CHECKINOUT AS CHECKINOUT_1
                                    ORDER BY CHECKTIME DESC))";
                DataTable dtgetpunch = DataAccess.ExecuteDataTable(sql);
                return dtgetpunch;
            }
    when call it
     dtgetlastpunch = annaul.GetImageById();
                if (dtgetlastpunch.Rows.Count > 0)
                {
               byte[] ImageUser= (byte[])dtgetmemberdata.Rows[0]["MemberImage"];
               picturebox1.Image = ByteArrayToImagebyMemoryStream(ImageUser);
    // same thing for picturebox2
                }
    


    Monday, April 1, 2019 2:10 AM

Answers

  • Hi,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    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, April 18, 2019 6:45 AM
    Moderator
  • solved
    Sunday, June 30, 2019 1:15 AM

All replies

  • Hi,

    I don't quite understand the logic of the pseudo code you provide.

    You check the last picture on the machine every second and keep the picture for 30 seconds. If there are new pictures input within 30 seconds, will this not cause conflicts?

    Can you elaborate on it?

    Regards,

    Kyle


    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, April 1, 2019 9:35 AM
    Moderator
  • not make conflict

    if new picture inserted within 30 second it will get last record inserted

    meaning if i have 

    Image1  machine1  09:05:12 show per 30 second

    then one row inserted on machine  1 as

    Image2  machine1  09:05:20

    it will show image 2

    image1 will show per 30 second if no image added for machine 1 within 30 second

    this point not make conflict only i write for clear

    Monday, April 1, 2019 11:25 AM
  • if any thing not clear please tell me i will clear for you

    i dont need function per retrieve images or any thing

    only i need to use function below on background worker 

    Monday, April 1, 2019 3:37 PM
  • Meaning what i write on

    private void bgw_DoWork(object sender, DoWorkEventArgs e)
            {

            }

     private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {

            }

    private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled)
                {

                }

             else

               {

                }

    }

    Monday, April 1, 2019 3:42 PM
  • To clear what i need 

    suppose text is image 

    For Machine 1

    every second i will fetch last row

    suppose time Now 02/04/2019 03:02:01 i fetch last row as below

    UserId  MachineId  checktime  checktype
    202     1          03:02:01    1
    
     

    in time 03:02:02 new row added then it will fetch row below as last row

    UserId  MachineId  checktime  checktype
    110     1          03:02:02    1

    last row added on 03:02:02 and no row added within 30 second

    then it still show last row until 03:02:32

    on second 03:02:33 no row added then it will be set picture box image to null until

    now new row added on time 03:03:01 then it will display that row

    UserId  MachineId  checktime  checktype
    550     1          03:03:01    1

    Summary

    every second i will fetch last row 

    last image or id will display for 30 second if last row per machine 1

    not changed per 30 second 

    if last row still not changed after 30 second


    Tuesday, April 2, 2019 3:19 AM
  • Hi,

    I think the BackGroundWorker is not necessary. You can use "System.Timers" to implement it, similarly, it will also create a new thread to perform related operations.

    Here is the simple code you can refer to. In this demo, I use "Id" to get the latest added item. In your situation, you can replace it with "checktime". (suppose text is image)

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    
        // Use count to record time (per 30s)
        int count = 0;
    
        string constr = @"connection string";
        // Store the picture (here replaced with a string)
        string previous = "";
    
        private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Store the last picture
            string current = "";
            using (SqlConnection sqlcon = new SqlConnection(constr))
            {
                // Get the last pirture by id
                SqlCommand sqlcomm = new SqlCommand(@"select top 1 Picture from Picture order by id desc", sqlcon);
                sqlcon.Open();
                current = sqlcomm.ExecuteScalar().ToString();
                // If there is new one added into database
                if(previous == current)
                {
                    // time (30s) ++
                    count++;
                }
                else
                {
                    // Reset the time
                    count = 0;
                    previous = current;
                }
            }
    
            // If over 30, set to null
            if (count >= 30)
            {
                label1.Text = "Null";
            }
            else
            {
                label1.Text = current;
            }
    
            // Show the time
            label2.Text = count.ToString();
        }
    
        private void btStart_Click(object sender, EventArgs e)
        {
            // Set timer (per 1s)
            System.Timers.Timer myTimer = new System.Timers.Timer(1000);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
            myTimer.AutoReset = true;
            myTimer.Enabled = true;
            myTimer.Start();
        }

    The table in database:

    Result:

    Regards,

    Kyle


    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.

    Tuesday, April 2, 2019 6:03 AM
    Moderator
  • Hi,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer.So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    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, April 18, 2019 6:45 AM
    Moderator
  • solved
    Sunday, June 30, 2019 1:15 AM