Solved

Service and class pass data?

Posted on 2006-07-03
7
194 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

17 Experts available now in Live!

Get 1:1 Help Now