0

Okay, so this is an advanced method from my yesterday question

switch (status)
    {
        case 0:
            {
                label1.Visible = false;
                label2.Visible = true;
                label3.Visible = false;
                status = 1;
                break;
            }
        case 1:
            {
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = true;
                status = 2;
                break;
            }
        case 2:
            {
                label1.Visible = true;
                label2.Visible = false;
                label3.Visible = false;
                status = 0;
                break;
            }
    }

i using this switch flag in my command button and succes a lot but i figure out something cool with this, so i thinking a big advanced this and possible that is like this

while (true)
{
    switch (status)
    {
        case 0:
            {
                label1.Visible = false;
                label2.Visible = true;
                label3.Visible = false;
                status = 1;
                break;
            }
        case 1:
            {
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = true;
                status = 2;
                break;
            }
        case 2:
            {
                label1.Visible = true;
                label2.Visible = false;
                label3.Visible = false;
                status = 0;
                break;
            }
    }
}

so i just using a single command button then all of the case in switch began repeated and repeated over. But this logic is total failure, nothing comes to my idea when i executing the program. I already done this with incremented loop and failure too, is there any possibility with while loop on this case?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Geofff
  • 27
  • 2
  • 10
  • 2
    What in the world are you trying to do? – nphx Nov 24 '13 at 10:35
  • @nphx i wanna making a repeated switch program – Geofff Nov 24 '13 at 10:37
  • Did you intend for those `break` statements to jump out of the loop? – nphx Nov 24 '13 at 10:41
  • @nphx hm, i didnt get what your mean, my idea is just to looping the switch statement so i didnt have to even click the command button to repeated it more and more – Geofff Nov 24 '13 at 11:19
  • That's exactly what you have done. What is the point of that? – nphx Nov 24 '13 at 11:21
  • what is your problem, i dont get it.. 'How To Make A Child Execute Command Button in C# with Looping'? what is 'child' and where is 'Command Button'? – har07 Nov 24 '13 at 11:32
  • @nphx my while(true) is total failure yet i wanna looping the switch statement and my idea was failure – Geofff Nov 24 '13 at 12:13
  • @har07 see my yesterday question then http://stackoverflow.com/questions/20161768/how-to-make-a-child-execute-command-button-in-c-sharp and this is the advanced method from my idea – Geofff Nov 24 '13 at 12:14
  • how is the failure? is the program not responding? or all labels stay there like before you clicked the button? – har07 Nov 24 '13 at 12:35
  • @har07 yes the program isnt responding – Geofff Nov 24 '13 at 12:39
  • so thats because you do a never ending operation (the while(true) loop) in UI thread. That operation blocking your UI work. – har07 Nov 24 '13 at 12:50
  • If you move this to a different thread, the labels will cycle in visibility. Is it what you're trying to do? – nphx Nov 24 '13 at 12:55
  • @har07 so u saying, its impossible doing a never ending loop in GUI application ? Okay if u say so, but im using incremented loop is failure too. (i dont know, if the program read looping so fast or not, but i didnt even see switching after command button click) – Geofff Nov 24 '13 at 12:55
  • @nphx nah you get what i mean, cool :D – Geofff Nov 24 '13 at 12:55

1 Answers1

0

After clicking button1, this piece of code will show and hide labels indefinitely. See if it is what you wanted.

private void button1_Click(object sender, EventArgs e)
{
    button1.Enabled = false;

    var t = new System.Threading.Tasks.Task(Switcher);
    t.Start();
}

private void Switcher()
{
    int status = 0;

    while (true)
    {
        switch (status)
        {
        case 0:
            label1.Visible = false;
            label2.Visible = true;
            label3.Visible = false;
            status = 1;
            break;
        case 1:
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = true;
            status = 2;
            break;
        case 2:
            label1.Visible = true;
            label2.Visible = false;
            label3.Visible = false;
            status = 0;
            break;
        }

        System.Threading.Thread.Sleep(200);
    }
}
nphx
  • 1,426
  • 3
  • 19
  • 30
  • var t = new System.Threading.Tasks.Task(Switcher); its not a valid object sir, but System.Threading.Thread.Sleep(200); is available – Geofff Nov 24 '13 at 13:48
  • @Geofff Are you using an older version of .NET than 4.0? – nphx Nov 24 '13 at 13:50
  • true, im using .NET 2.0 so this is only available in .NET 4.0 then – Geofff Nov 24 '13 at 13:52
  • @Geofff You can use `var t = new System.Threading.Thread(Switcher);` instead. – nphx Nov 24 '13 at 13:53
  • "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on." by the way what is the intent of code u making ? i wonder ... – Geofff Nov 24 '13 at 14:02
  • It works for me on .NET 4.0. The solution for your problem is explained [here](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c). – nphx Nov 24 '13 at 14:05
  • but it doesnt work on .NET 4.0 sample i make, lemme read the link you give to me for a while – Geofff Nov 24 '13 at 14:17
  • @Geofff If I start the application without debugging, it works fine. With debugging I get the same error. – nphx Nov 24 '13 at 14:21
  • lols, are u have another idea? to be honest i didnt know anything about threading nor multithreading stuff – Geofff Nov 24 '13 at 14:26