Solved

Service and class pass data?

Posted on 2006-07-03
7
195 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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