CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2003
    Posts
    12

    Using a form in Windows Service

    Hi.

    I'm trying to use a windows form in my Service app.
    Can anyone please tell me how to do this.

    I'm trying to call the form in the "OnStart" function in the service.

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    you need to include the dlls and references, using statements that are already required in usual windowsForm application. so u need to copy all those ......
    best is make a normal windows app and compair what u haven't got.

    Paresh

  3. #3
    Join Date
    Feb 2003
    Posts
    12
    Thanx for your reply.

    I think I've allready done that.

    The problem is that the form is shown but it "hangs" and doesn't accept any inputs and isn't fully painted???????

    /mabj

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    hi

    the problem is when windows service starts it starts in thread ! I would rather suggest you not to open a form. normally windows services are process/memory based.

    the another problem with form is when
    you run the form
    it needs something like this

    Application.run(...form.....)
    may be u can try that.

    but remember concepts of services are diff. and they need to be in handled in specific way like opening file etc..

    you can have another form/windows app seperately interacting with this service
    and getting thru-fro the data ..
    have a service socket based and have a form client based thus u can access the data. i am sure there are otherways of
    doing this also.

    Paresh

  5. #5
    Join Date
    Feb 2003
    Posts
    12
    I know what you mean. Normaly a service would'nt have any forms. But in this case this app needs in some cases som user input.
    I was trying to come up with a way to have a windows app to handle the userinput and to interact with the service (calling some functions in the service app) but it seems to be quite difficult, or?

    I thought that I could create an instance of the form in the service app and simply call Form.Show(). I've also tried to create a new thread to run the form but the result would be the same.

    Any Idea of a way to have a Windows app to comunicate with the service? Using Winsock will not work in this case.


    /mabj

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    TCPClient or .NET sockets should work. if you use it properly then it should be fine. since what happens is service waits for a while to complete the thread.
    so u can have a form like this.

    open a thread in the form finish the thread and in that thread open the form. note here form object should be public. and make that open.. so it won't distroy..

    Paresh

  7. #7
    Join Date
    Feb 2003
    Posts
    12
    I don't think I quite understand what you mean by your last reply.
    Isn't there any sample code somewhere that will show this?

  8. #8
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    what I meant was,

    when you have a onstart on service ! right.
    then you can start a thread which will open a form. this thread will just open the form and complete.
    now this form stays open till the class is live. or form is canceled. shouldn't be hard.

    Paresh

  9. #9
    Join Date
    Feb 2003
    Posts
    12
    I might be incredible stupid and still don’t understand.
    As I described earlier I have tried to create a new thread in the OnStart section and using this thread to load the Form.

    If that is what you are trying to explain this doesn’t work (not the way I’m doing it). The form is displayed, yes, but it freezes and it seems that no part of the code in the Form class is executing. You can’t even close the form.

  10. #10
    Join Date
    Feb 2003
    Posts
    12

    Thumbs up

    Never mind.

    I've got what you mean and I've got it to work.

    Thanks for your help Paresh

  11. #11
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    oh! that's pretty cool. I was just about to write a windows service program and try that out. glad to know.



    Paresh

  12. #12
    Join Date
    Sep 2001
    Posts
    169

    Windows form in service

    I read all the post regarding this,i created a thread in OnStart method and in that thread tried to create a message box ,what is the difference b/w any form and a message box if a form can be created in that thread why not a message box?
    i tried to do that but even when service gets started successfullly,Message box is not visible.


    i don't know why?

    anticipating an earliest reply
    regards
    Anushree

  13. #13
    Join Date
    May 2002
    Location
    Hyderabad
    Posts
    76
    hi,
    A difference between executing a Windows service and other Visual Studio .NET applications is that the dialog boxes raised from a Windows service are not visible to the user. In addition, the error messages generated from a Windows service are logged in the event logs.
    so u can't see message box generated from the windows service, I don't about forms.
    Ashish Sheth

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured