Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a developer who has a home project to make an exercise app.

I have my exercise list in and xml file as so.

<routine>
    <exercise>
        <order>1</order>
        <name>Row Close</name>
        <time>30</time>
    </exercise>
    <exercise>
        <order>2</order>
        <name>Arm Row Right</name>
        <time>30</time>
    </exercise>



From this I create a struct

struct workout
   {
       private string _name;
       private int _time;
       private bool _complete;
       private int _id;



       public string Name { get => _name; set => _name = value; }
       public int Time { get => _time; set => _time = value; }
       public bool Complete { get => _complete; set => _complete = value; }
       public int Id { get => _id; set => _id = value; }
   }


This struct has a list of it as I repeat the exercise.

the struct has a time and I want the program to show an image for the time period.

stated in the struct.

I loop over the list and want to do this at every object in the list void exercise

private void Exercise(workout w)
        {
            lbActivity.Text = w.Name;
            lbTime.Text = w.Time.ToString();
            string v = "\\";
            string iFileLoc = Properties.Settings.Default.ExercisePics.ToString() + v + w.Name + ".jpg";
            pBxExercise.ImageLocation = iFileLoc;
            pBxExercise.SizeMode = PictureBoxSizeMode.AutoSize;
            //Thread.Sleep(3000);
            // setting A forms timer to Start.
        }


What I have tried:

to get it to pause I have tried the below

My issue is htta the loop never change the picture, when I want it to.

The tread sleep pause it and the timer runs after the loop.

I just need some guidance, as I have not worked with task or threading before any help is appreciated.
Posted
Updated 3-Sep-20 6:22am
Comments
Maciej Los 3-Sep-20 11:52am    
Have you tried to set image source for pBxExcercise?
MarcusCole6833 3-Sep-20 12:00pm    
this is the first time I used the picture box, I work in a factory and generally do not need it.

pBxExercise.ImageLocation = iFileLoc;

The above code does not cover hte image source, as I did not find a property of image source?
MarcusCole6833 3-Sep-20 14:12pm    
thank you
MarcusCole6833 3-Sep-20 14:12pm    
thank you

Thread.Sleep blocks the current thread for the specified amount of time.

You are calling this from the UI thread, which means your user interface is unable to update.

Use a timer instead:
Timer Class (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
MarcusCole6833 3-Sep-20 14:12pm    
thank you
Use a Workout class

C#
public class Workout
{
    public string Name { get; set; }
    public int Time { get; set; }
    public bool Complete { get; set; }
    public int Id { get; set; }
}


In my example I'm using a simple label (lblWorkoutName) to show the current workout, you can keep your picture box. Next add a Timer to the form (I've left mine called "timer1" but you should give it a sensible name). The timer is then used to trigger when the next workout should be shown.

C#
public partial class Form1 : Form
{
    private List<Workout> workouts;
    private int currentWorkout;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // I'm hard-coding the data, yours will come from the xml
        workouts = new List<Workout>
        {
            new Workout {Id = 1, Name = "Row Close", Time = 3},
            new Workout {Id = 2, Name = "Arm Row Right", Time = 5}
        };

        currentWorkout = 1;

        ShowWorkout(currentWorkout);
    }

    private void ShowWorkout(int id)
    {
        timer1.Stop();

        Workout workout = workouts.SingleOrDefault(w => w.Id == id);

        if (workout == null)
        {
            lblWorkoutName.Text = "Workout over";
            return;
        }

        lblWorkoutName.Text = workout.Name;

        timer1.Interval = workout.Time * 1000;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        currentWorkout++;
        ShowWorkout(currentWorkout);
    }
}
 
Share this answer
 
Comments
MarcusCole6833 3-Sep-20 14:13pm    
thank you
I assume that you do not see an image...

Based on this: c# - How to Load Image to PictureBox; based on the image Location stored in the DataBase - Stack Overflow[^]

Quote:
MSDN isn't exactly clear on this matter - ImageLocation property, but it seems that setting the ImageLocation property may not exactly load the image, so you may try to use PictureBox.Load Method (System.Windows.Forms) | Microsoft Docs[^] instead:
 
Share this answer
 
Comments
MarcusCole6833 3-Sep-20 14:13pm    
thank you
Maciej Los 3-Sep-20 14:19pm    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900