Solved

Service and class pass data?

Posted on 2006-07-03
7
193 Views
Last Modified: 2010-05-18
I have a nicely running service.

Now I need to include a restriction for licensing sake.

The service must 'call' a method in a seperate class to continune running.

The problem is that the service keeps running regardless.

So, it appears the class data is not successfully passed to the service.

Here's the code:

[service1.cs]

protected override void OnStart(string[] args)
{
    Class1 class1 = new Class1();
    bool ok = class1.Check();
   if (ok)
   {
     run = true;
   }
   else
  {
     run = false;
  }
}


[Class1.cs]

public bool Check()
        {          
            bool result;
           
            if (info1 != info2)
            {
                result = false;
            }
            else
            {              
                result = true;
            }
            return result;            
        }    

0
Comment
Question by:kvnsdr
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17030799
I don't see where run = true/false does anything.

Bob
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17030929
You need to instantiate a servicecontroller for your service, and then use it to stop your service.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 17031095
Sorry, maybe I was too breif with my code example.

I learned the "Whie(Run)" on another web site that effectively stops code execution.

So, if I can get the correct bool info from Class1.cs to either allow or disallow the service to run..


bool Run = true;

 protected override void OnStart(string[] args)
 {
      Thread thread = new Thread(new ThreadStart(StartCode));
      thread.Start();
}

 public void StartCode()
{          
     while (Run)
     {
          // code runs here
     }
}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Expert Comment

by:gbzhhu
ID: 17031225
Your assumption ios based on those 2 variables being equal or not

 if (info1 != info2)

Were are the values coming from?

I think the question is slightly on the vague side
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 17031333
The values of (info1 != info2) represent the results of a very long matmatical equation.

I DO know the result is == and my return bool = true.

I simply would like to know how to pass the bool value from the Class1.cs to the Service1.cs...


0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 17031357
Are you saying that you know the class method Check returns true BUT the line
bool ok = class1.Check();
is not assigning the ok variable a true value?

If check method returns true then passing that value back to the service is exactly what you are doing, I can't see anything wrong with it
0
 
LVL 12

Accepted Solution

by:
gbzhhu earned 250 total points
ID: 17031396
Then only thing questionable that i can see is that you are using a new thread.  I am not sure setting the Run = somevalue in the main thread and accessing it on the new spawned thread is thread safe.  have you tried without the threading?

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now