3 Replies - 9345 Views - Last Post: 29 July 2012 - 12:57 AM

#1 Ciganjo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-September 11

How to trigger Main function through a Thread?

Posted 04 April 2012 - 03:33 AM

Hi, I've attempted to build a simple program with a main function and a thread. The purpose is the Thread will act as a timer, and will simply count seconds. I want to trigger the main function that once the thread has reached 10 seconds, to exit the program

heres what I have so far (I know that I dont really need the 'Simple' class, but i'll worry about that later)...

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <ctime>
#include <Windows.h>

using namespace std;
using namespace System;
using namespace System::Threading;

int SecondCount = 0;


public ref class ServerClass
{
public:
    // The method that will be called when the thread is started.
    void InstanceMethod()
    {
        while (1)
		{
			Thread::Sleep(1000);
			SecondCount++;
			Console::WriteLine(SecondCount);
			if(SecondCount == 10)
			{
				cout<<endl;
				SecondCount = 0;
			}
		}
    }
};

public ref class Simple
{
public:
    static void Main()
    {
        Console::WriteLine("Thread Simple Sample");

        ServerClass^ serverObject = gcnew ServerClass();

        Thread^ InstanceCaller = gcnew Thread(gcnew ThreadStart(serverObject, &ServerClass::InstanceMethod));

        // Start the thread.
        InstanceCaller->Start();


    }
};

int main(int argc, char *argv[])
{
	
	Simple::Main();

	if(SecondCount == 10)
	{
		exit(1);
	}


	

	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to trigger Main function through a Thread?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: How to trigger Main function through a Thread?

Posted 04 April 2012 - 03:35 AM

Moved to CLI C++
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,009
  • Joined: 05-May 12

Re: How to trigger Main function through a Thread?

Posted 06 June 2012 - 01:56 AM

The typical approach is to use a mutex, event, or semaphore. The thread that is to be "triggered" will wait on the WaitHandle object, and the thread that is doing the work that says it is done will signal the WaitHandle.

In your case, though, since all you are doing is waiting for the other thread to be done, you could simply use Join(). See the sample code: http://msdn.microsof...y/95hbf2ta#Y500
Was This Post Helpful? 0
  • +
  • -

#4 Cenron  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 13
  • Joined: 23-May 12

Re: How to trigger Main function through a Thread?

Posted 29 July 2012 - 12:57 AM

View PostSkydiver, on 06 June 2012 - 01:56 AM, said:

The typical approach is to use a mutex, event, or semaphore. The thread that is to be "triggered" will wait on the WaitHandle object, and the thread that is doing the work that says it is done will signal the WaitHandle.

In your case, though, since all you are doing is waiting for the other thread to be done, you could simply use Join(). See the sample code: http://msdn.microsof...y/95hbf2ta#Y500



Not to knock your approach man but why use a thread to maintain a timer? Seems like a waste of resources. You can make a simple timer using 2 variables.

// NOT CLI

int main()
{
  const u_long interval = 10; // 10 seconds for example.
  u_long startTimer = GetTickCount(); // This is in mili secs.

  // The thread that checks for the timer.
  while( true )
  {
     // DO whatever you want here and when 10 seconds goes by the loop exists.
     if( (startTimer + (interval * 1000)) >= GetTickCount() )
       break;
  }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1