"while(true)"Loop doesent work permanent

Shynex

Member
Joined
Jan 28, 2020
Messages
11
Programming Experience
Beginner
So currently I am programming a c# Steam Bot with the visual Studio add on selenium.
So far it is working pretty well, but the "while (true)" loop just stops after some time.
Sometimes its running like 600 times, but there are also some runs with only 100 iterations.
I highly appreciate every Idea, which could solve my Problem.:)

Thats basicly the Code:

C#:
while (true)
{
    //Click some Buttons
    //Save some Numbers

    if (I like the Item)
    {
        //buyItem();
    }

    Reload_the_Numbers()
    reloadButton.Click();
    wait(5);
}
 
Last edited by a moderator:
Firstly, please post code snippets properly. Tables are for tabular data. Code is not tabular data. The editor toolbar provides a formatting option specifically for code. Please use it.
 
As for your issue, your description is just too vague. There's nothing in what you have posted that would indicate where and why code would "just stop". That's apart from the fact that code doesn't "just stop". You need to do more investigative work yourself and provide a more detailed description of the issue. Is an exception thrown? If so, where and what? Does a method call fail to return? If so, where and what? Is an infinite loop (other than the one you're trying to create) get entered? Is it the same thing every time? You need to do some debugging on your code.
 
I can only conclude that the OP might be using threading and may be terminating a thread from continuing an execution. Being a beginner, I just hope you are doing it properly and use the ManualResetEvent, instead of just killing the thread. Killing the thread will leave a lock in place, even though you've killed the thread, if another process requires that lock, they will also be stuck. This causes havoc for processes assigned to execute on the same lock. Using the ManualResetEvent prevents this. Currently looking at the code above, as John said, there is nothing currently in view which is resulting in your code suddenly stopping. Unless you've redacted some code from view, there is little else to suggest.

What's with this : reloadButton.Click(); being in a console app?

Edit : Typo fixed
 
Last edited:
What do you mean by OP?
So i guess I understood, what you said. Ich have to "start" a thread at the beginning of the while(true) loop and I have to stop it at the end of the loop.
And when I stop it, all data in the Thread shoult have been deleted.
Or am I completely wrong?

I want that the Code inside the while(true) loop runs 24/7, so there is no break or continue statement inside the code or continue.
Is a while(true) loop even a goobb way for that?
 
Last edited:
OP == Original Post
OP == Original Poster
 
OP => Original Poster.

We don't know why your loop is exiting. I was concluding that what I posted on post 4 could be the only likely scenario. So I was assuming you were executing your code on another thread. Since its not only common practice to do so when using an infinite loop, but I was assuming somewhere in your code, you were probably terminating the thread where your while loop was executing. . .
 
What @Sheepings is trying to say is that this is "the bad way" to stop that infinite loop:
C#:
void BadRunInfinite()
{
    while (true)
    {
        Console.WriteLine($"{DateTime.Now}");
        Thread.Sleep(1000);
    }
}

void BadWayToStop()
{
    var thread = new Thread(BadRunInfinite);
    thread.Start();

    // Let BadRunInfinite() run for about 5 seconds
    Thread.Sleep(5000);

    // Kill the thread
    thread.Abort();
}

Here's the way to tell the loop to break out:
C#:
ManualResetEvent _stop;

void GoodRunInfinite()
{
    while (true)
    {
        Console.WriteLine($"{DateTime.Now}");

        if (_stop.WaitOne(1000))
            break;
    }
    Console.WriteLine("Done.");
}

void GoodWayToStop()
{
    _stop = new ManualResetEvent(false);
    var thread = new Thread(GoodRunInfinite);
    thread.Start();

    // Let GoodRunInfinite() run for about 5 seconds
    Thread.Sleep(5000);

    // Tell the thread to stop
    _stop.Set();

    // Wait for the thread to exit
    thread.Join();
}
 
I dont have my code Right now, but I guess I can explain, what the programm does.
It a selenium code, so it goes to a webside an pretens to be a user.

{
It opens steam witch the ChromerDriver;


After thatits logging in;

After that it Clicks an a specific weapon skin in he steam marketplace

After that its loading the float value of the first skin(by using the (Steam inventory helper)

///create new thread

while(true){

///start new thread

check float and save in a variable

if( Price and float value is ok){
buy();
}

After that the Programms cklicks the reload button, which reloads the listing
and everything starts from the beginning of the while true loop.

///terminate thread
///delete all data wich have been saved in the thread
}
}

Did I understood that correctly?
 
No you didn't understand it correctly. Skydiver already gave you an example to follow.

Do not create threads inside a while loop. Your thread needs to be created, and when passing the action or void method name to that thread, you begin executing the code of that void method on the new thread. Your void method will contain the while loop, just as it has already been described above.
 
You're missing the point completely. We are trying to get you to execute your while loop on an independent thread. You don't have to stop it, but if you did decide to, you now know how to do it.
 

Latest posts

Back
Top Bottom