hayal.et
hayal.et

Reputation: 51

multithread pause and resume

i want to learn working logic about thread pause and resume.

Thread th=new Thread(start);
th.Start();

public void start()
{
    command_1;
    command_2;
    command_3;

    if(variable_condition)
        goto Pause;

    command_4;
    command_5;
    command_6;
    command_7;

    Pause:
        pause();
}

private void pause()
{
    th.Suspend();
}

private void button1_Click(object sender, EventArgs e)
{
    th.Resume();
}

Now, When the command which starts the thread continue?

command_1 or command_4 ?

Upvotes: 0

Views: 170

Answers (1)

Riad Baghbanli
Riad Baghbanli

Reputation: 3319

As per code written your Resume will not do a thing since start function execution is already at label Pause. So, you are resuming at the end, and the function simply ends upon resume.

If you want to resume from command_4, then change

if(variable_condition)
    goto Pause;

to

if(variable_condition)
    pause();

and remove label Pause

Alternatively, if gods of coding demand GOTO to be used:

Thread th=new Thread(start);
th.Start();

public void start()
{
    command_1;
    command_2;
    command_3;

    if(variable_condition)
        goto Pause;

    Pause:
        pause();

    command_4;
    command_5;
    command_6;
    command_7;
}

private void pause()
{
    th.Suspend();
}

private void button1_Click(object sender, EventArgs e)
{
    th.Resume();
}

It is very stupid, but ...

Upvotes: 1

Related Questions