Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event in a Thread

Event in a Thread

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QzRz
    wrote on last edited by
    #1

    Hello I have created my own event and delegate, they look like this:

    public delegate Answer delegateCallResponse(Request req);
    public event delegateCallResponse eventCallResponse;
    

    the Request and Answer is 2 struct which contain a few strings/ints. I got a method to handle the event, which look like

    public Answer ans = new Answer();
    private Answer OnEvent(Request req) {
    FillRequest(req);
    while (!ans.filled) { Thread.Sleep(1000); }
    return this.ans; }
    

    the ans.filled is a bool indication whether the information is filled or not. My problem is that the Answer can take a while to get filled and the code as it is now, will make the mainthread sleep thus making the GUI unavailable for some time. Is there a way to run the event in a thread? or just another way to make it wait than use the while function? //QzRz

    J K 2 Replies Last reply
    0
    • Q QzRz

      Hello I have created my own event and delegate, they look like this:

      public delegate Answer delegateCallResponse(Request req);
      public event delegateCallResponse eventCallResponse;
      

      the Request and Answer is 2 struct which contain a few strings/ints. I got a method to handle the event, which look like

      public Answer ans = new Answer();
      private Answer OnEvent(Request req) {
      FillRequest(req);
      while (!ans.filled) { Thread.Sleep(1000); }
      return this.ans; }
      

      the ans.filled is a bool indication whether the information is filled or not. My problem is that the Answer can take a while to get filled and the code as it is now, will make the mainthread sleep thus making the GUI unavailable for some time. Is there a way to run the event in a thread? or just another way to make it wait than use the while function? //QzRz

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      Use an Asynchronous method call to run FillRequest: Asynchronous Method Invocation[^] then instead of looping waiting for ans.filled to be true you can set up a callback for when the function finishes.

      1 Reply Last reply
      0
      • Q QzRz

        Hello I have created my own event and delegate, they look like this:

        public delegate Answer delegateCallResponse(Request req);
        public event delegateCallResponse eventCallResponse;
        

        the Request and Answer is 2 struct which contain a few strings/ints. I got a method to handle the event, which look like

        public Answer ans = new Answer();
        private Answer OnEvent(Request req) {
        FillRequest(req);
        while (!ans.filled) { Thread.Sleep(1000); }
        return this.ans; }
        

        the ans.filled is a bool indication whether the information is filled or not. My problem is that the Answer can take a while to get filled and the code as it is now, will make the mainthread sleep thus making the GUI unavailable for some time. Is there a way to run the event in a thread? or just another way to make it wait than use the while function? //QzRz

        K Offline
        K Offline
        K L K
        wrote on last edited by
        #3

        If your event is in a class, you can create a method in that class that invokes the event asynchronously. Specifically, you can do:

        ThreadPool.QueueUserWorkItem( delegate { if(eventCallResponse != null) { eventCallResponse(req) } );

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups