How to run the same method in a separating thread without end

I have a project that has a static web browser. I wanted to run this method (which includes the web browser) at the same time without ending. Exist:

1 method 2 button 2 text box

static webbrowser wb;

public static look(string address)
{
    wb = new webbrowser;
    wb.navigate(address);
}

button1.click()
{
    for(int i = 0;i <= converttoint32(textbox1.text);i++)
        look(textbox2.text);
}

button2.click()
{
    //close the threads.
}

      

<I want to use separate threads to create a new web browser. They must continue until button2.click is pressed.

eg. when button1 is clicked, the method will create web browsers (count from textbox1.text) and they will stay connected. Pressing button 2 will close the jobs.

+3


source to share


1 answer


Based on this, you can try.

public static object locker =  new object();

    public static void InitBrowser(int browser)
    {
        var thread = new Thread(() =>
        {
            // Create Browser Here

            Monitor.Wait(locker);
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }

button1.click()
{       
   Monitor.Enter(locker);
   for(int i = 0;i <= converttoint32(textbox1.text);i++)
    InitBrowser(textbox2.text);
}

button2.click()
{
    Monitor.PulseAll(locker);
}

      



You can try this. This solution has not been tested, but perhaps you can use it as the base for the build you want.

0


source







All Articles