Jump to content

[Solved] Loop in client source


Recommended Posts

Hello,

i have the client source and i try to make a function which should be called in a loop.

I tried threading, but it doesn´t work. If i use while and threads, the game doesn´t appear, but the function gets called in a loop.

Does somebody know how to call a function in a loop from UserInterface without freezing the game?

Thank you :)

 

 

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

8 minutes ago, Cr5x said:

Hello,

i have the client source and i try to make a function which should be called in a loop.

I tried threading, but it doesn´t work. If i use while and threads, the game doesn´t appear, but the function gets called in a loop.

Does somebody know how to call a function in a loop from UserInterface without freezing the game?

Thank you :)

 

 

Please provide test code

Link to comment
Share on other sites

#include <Windows.h>
#include <iostream>
#include <thread>
#include <fstream>


void TestFunc(){
	while (1){
		ofstream myfile;
		myfile.open("example.txt");
		myfile << "Just a test";
		myfile.close();
	}
}
  

And the call in the mainfunc:

 

std::thread t1(TestFunc);
t1.join();

 

The function works, but the game doesnt appear..

Link to comment
Share on other sites

56 minutes ago, Cr5x said:

#include <Windows.h>
#include <iostream>
#include <thread>
#include <fstream>


void TestFunc(){
	while (1){
		ofstream myfile;
		myfile.open("example.txt");
		myfile << "Just a test";
		myfile.close();
	}
}
  

And the call in the mainfunc:

 


std::thread t1(TestFunc);
t1.join();

 

The function works, but the game doesnt appear..

 

The thread destructor will terminate your program if the object is destroyed while the thread is still running.  Do you know what is a thread ?

Is not better to create a class for  TestFunc and after you call? Or you can even make function inline.  while(1) in above example these statements will be executed forever... and will never stops. You are sure about that? You may use an "if condition" and inside it a break statement to terminate loop in some specific situation.

 

I do not really understand what you want to do. :) 

inline
void TestFunc() {
	std::ofstream ofs("example.txt", std::ofstream::out);
	ofs << "Just a test";
	ofs.close();
}

int main() {

	for (;;)
	{
		TestFunc();
	}
}

 

Link to comment
Share on other sites

5 minutes ago, Cr5x said:

Thank you. I just want to make a function do detect if a program is running. Just a test.

So yes, it must run forever.. other solutions?

The function "TestFunc" is just a test to see how to loop without freezing the game.

Is this what you need ?

http://stackoverflow.com/questions/13179410/check-whether-one-specific-process-is-running-on-windows-with-c

You not need a loop for that. Just search on google :)

Link to comment
Share on other sites

But this isn´t in a loop, is it? I think the function there is called once..

 

int main(int argc, char* argv[])
{

  bool bAnyBrowserIsOpen = false;

  if ( FindProcessId(L"chrome.exe") || FindProcessId(L"firefox.exe") || FindProcessId(L"iexplore.exe"))
  {
     bAnyBrowserIsOpen = true;
  }

  return 0;
}

 

It just checks once. I need to check it the whole time. With timers it would be easy, but in the metin2 source i can´t use timers..

Thank you in advance :)

 

Edit: Solved :D

Link to comment
Share on other sites

The error in your first post is pretty much the join-statement.

if you use t1.join() then the game will synchronize with the thread meaning it's waiting for the thread to be finished. A thing that will actually never occur since your thread is running into infinity. The precise point where you're using join() is the moment when the game stops executing. Yes, your thread will still run, but the actual game won't because it's always waiting for your thread. At the very moment you're spawning the thread, execution starts. So there's no need for join anyway.

We are the tortured.
We're not your friends.
As long as we're not visible.
We are unfixable.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.