Jump to content
×
×
  • Create New...
  • 0

[Solved] Loop in client source


Cr5x

Question

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
  • Answers 7
  • Created
  • Last Reply

Top Posters For This Question

Popular Days

Top Posters For This Question

7 answers to this question

Recommended Posts

  • 0
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
  • 0
#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
  • 0
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
  • 0

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.

Link to comment
  • 0
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
  • 0

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
  • 0

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Announcements

  • -15% Discount in Metin2 Dev Store (01/10/2021 => 31/12/2021) => Code => 15_PCT_OCT_TO_DEC_2021


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.