Solved

Service and class pass data?

Posted on 2006-07-03
7
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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