[ Spid3RmaN @ 11.03.2009. 09:01 ] @
Napravio sam mali program u c# i zanima me kako da ga nateram da sačeka da se nešto završi pa da tek onda krene dalje? Probao sam sa tajmerima i sleep(), ali mi nije baš išlo.

Evo konkretan primer(deo koda):

Code:

        private void startButton_Click(object sender, EventArgs e)
        {
            if (startButton.Text == "START SURFING")
            {
                startSurfing();
            }
            else
            {
                stopSurfing();
            }
            statusBox.SelectedIndex = statusBox.Items.Count - 1;
        }
        void startSurfing()
        {
            startButton.Text = "STOP SURFING";
            while (startButton.Text == "STOP SURFING")
            {
                string tURL = targetURL.Text;
                statusBox.Items.Add("Preparing to start surfing...");
                targetURL.Enabled = false;
                execField.Enabled = false;
                execButton.Enabled = false;
                statusBox.Items.Add("Starting an executable...");
                System.Diagnostics.Process.Start(execField.Text);
                // Ovde treba da čeka jedno 10 sekundi ali ne znam kako to da napravim...
                statusBox.Items.Add("Visiting the provided link...");
                miniBrowser.Navigate(tURL);
                // I ovde treba da sačeka jedno 10 sekundi...
            }
        }


        void stopSurfing()
        {
            startButton.Text = "START SURFING";
            statusBox.Items.Add("Surfing session has bin stopped.");
            targetURL.Enabled = true;
            execField.Enabled = true;
            execButton.Enabled = true;
        }


Ovo je u stvari neki mini auto surfer koji pokušavam da napravim.
On sve radi samo što neće da sačeka da se završi beč(batch) fajl koji se pokreće, pa da sačeka da se poseti strana, pa tako u krug, već mi on pokrene milion tih bečeva(što i treba, ali ne tom brzinom) jedan za drugim i moram da resetujem kompjuter...

I čini mi se da u javi ima waitFor() za ovakve stvari, da li tako nešto ima i u c#?

EDIT:

Evo našao sam da što se tiče procesa(pokretanja onog fajla), mogu da sačekam da se on završi tako što na kraju dodam .WaitForExit, tako da mi radi toga nije više potrebna pomoć. A što se tiče čekanja da se stranica učita iskoristio sam tajmer(naučio sam da koristim :P).

Brišite temu slobodno...


[Ovu poruku je menjao Spid3RmaN dana 11.03.2009. u 16:41 GMT+1]
[ Sapphire @ 12.03.2009. 00:07 ] @
Korisne stvari za te namjene:

Timeri (sinhroni i asinhroni)
Asinhroni delegati
Background worker (veoma cist pristup)
Multi-Threading

P.S. Ako si vec postavio temu, sto ne okacis i rjesenje koje si napisao, netkome moze pomoci. Brisanje teme nakon postavljenog pitanja nema smisla...
[ sallle @ 12.03.2009. 00:40 ] @
taj tajmer koji si ostavio treba da zamenis sa eventom webBrowser.DocumentCompleted
[ Spid3RmaN @ 13.03.2009. 19:59 ] @
Evo vam kod.
Code:

private void startButton_Click(object sender, EventArgs e)
        {
            if ((targetURL.Text != targetURLdefVal) & (execField.Text != execFieldDefVal))
            {
                if (startButton.Text == "START SURFING")
                {
                    targetURL.Enabled = false;
                    execField.Enabled = false;
                    execButton.Enabled = false;
                    startButton.Text = "STOP SURFING";
                    statusBox.Items.Add("Starting to surf!");
                    statusBox.SelectedIndex = statusBox.Items.Count - 1;
                    startSurfing();
                }
                else
                {
                    stopSurfing();
                }
            }
            else
            {
                statusBox.Items.Add("Please fill in all required fields!");
            }
            
        }
        void startSurfing()
        {
            
            statusBox.Items.Add("Starting an executable...");
            statusBox.SelectedIndex = statusBox.Items.Count - 1;
            backgroundWorker1.RunWorkerAsync();
        }


        void stopSurfing()
        {
            startButton.Text = "START SURFING";
            statusBox.Items.Add("Surfing session has bin stopped.");
            targetURL.Enabled = true;
            execField.Enabled = true;
            execButton.Enabled = true;
            timerThe.Stop();
            timerThe.Enabled = false;
        }

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Process ProcessObj = new Process();
            ProcessObj.EnableRaisingEvents = false;
            ProcessObj.StartInfo.FileName = execField.Text;
            ProcessObj.StartInfo.UseShellExecute = false;
            ProcessObj.StartInfo.CreateNoWindow = true;
            ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
            ProcessObj.StartInfo.RedirectStandardOutput = true;
            ProcessObj.Start();
            ProcessObj.WaitForExit();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            string bURL = ""; // Ovde ide neki url....
            string tURL = targetURL.Text;
            statusBox.Items.Add("Executable successfully started!");
            statusBox.SelectedIndex = statusBox.Items.Count - 1;
            statusBox.Items.Add("Visiting the provided link...");
            statusBox.SelectedIndex = statusBox.Items.Count - 1;
            miniBrowser.Navigate(tURL);
            bigBrowser.Navigate(bURL);
            timerThe.Enabled = true;
            timerThe.Start();
        }

        private void timerThe_Tick_1(object sender, EventArgs e)
        {
            timerThe.Enabled = false;
            startSurfing();
        }


Nije ovde sve, ali dobijate ideju kako sam uradio... Evo sad pišem v2 i probaću sa DocumentCompleted