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. .NET (Core and Framework)
  4. call to the main thread

call to the main thread

Scheduled Pinned Locked Moved .NET (Core and Framework)
winformslearningworkspace
5 Posts 3 Posters 2 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.
  • O Offline
    O Offline
    OmegaSupreme
    wrote on last edited by
    #1

    Hi all, I create a worker thread to do some stuff, when that thread completes I would like to make a call from that thread to main thread to tell it to take a new course of action. In a windows forms environment you can call: this.Invoke but what if your developing an assembly or console app , is there an equivalent. I've tried using delegates etc but when you look at the Threading.Thread.CurrentThread.Name property your still 'in' the worker thread. Anyone know about this. TIA.:)

    A 1 Reply Last reply
    0
    • O OmegaSupreme

      Hi all, I create a worker thread to do some stuff, when that thread completes I would like to make a call from that thread to main thread to tell it to take a new course of action. In a windows forms environment you can call: this.Invoke but what if your developing an assembly or console app , is there an equivalent. I've tried using delegates etc but when you look at the Threading.Thread.CurrentThread.Name property your still 'in' the worker thread. Anyone know about this. TIA.:)

      A Offline
      A Offline
      Andy Davey
      wrote on last edited by
      #2

      Are you trying to make sure that after something happens in your worker thread that a method is executed within the context of the main thread, or are you just trying to signal the main thread from your worker thread. Probably the easiest way would be to pass a waithandle to your worker thread and get your main thread to wait on the handle (or check it regularly) for the handle to be signaled and then go from there. But if thats the case then you might as well recode the worker thread as an asynchronous method call. The above may not be to your liking :) Looking at the Mono project, the way they have implemented System.Windows.Forms.Control.Invoke() is place the delegate passed ot Invoke() in a queue. This queue is then checked in the windows message loop (which executes in the same thread as the control was created in), and any delegates in the queue are then called. So you could create a synchronized queue, get your worker thread to place a delegate in this queue, and then either get your main thread to periodically check this queue or signal a waithandle, and then get the main thread to execute the delegates in the queue.

      O D 2 Replies Last reply
      0
      • A Andy Davey

        Are you trying to make sure that after something happens in your worker thread that a method is executed within the context of the main thread, or are you just trying to signal the main thread from your worker thread. Probably the easiest way would be to pass a waithandle to your worker thread and get your main thread to wait on the handle (or check it regularly) for the handle to be signaled and then go from there. But if thats the case then you might as well recode the worker thread as an asynchronous method call. The above may not be to your liking :) Looking at the Mono project, the way they have implemented System.Windows.Forms.Control.Invoke() is place the delegate passed ot Invoke() in a queue. This queue is then checked in the windows message loop (which executes in the same thread as the control was created in), and any delegates in the queue are then called. So you could create a synchronized queue, get your worker thread to place a delegate in this queue, and then either get your main thread to periodically check this queue or signal a waithandle, and then get the main thread to execute the delegates in the queue.

        O Offline
        O Offline
        OmegaSupreme
        wrote on last edited by
        #3

        Hi Andy, I was looking to actually call a method on the main thread in the same way as Control.Invoke() 'marshals' the call to the main thread in a windows form. Your suggestions of passing a waithandle to the worker thread or using an asynchronous method call sound good and I will certainly look into them ( especially the async call, that hadn't occured to me at all :) ) Your description of how Mono works is very interesting and I imagine that microsoft do it in a similar way or at least thats how it appears to work. But it does seem to be a bit tricky for my feable brain and right now I need a quick and dirty solution. Thank you very much for help, it was very useful to me.

        1 Reply Last reply
        0
        • A Andy Davey

          Are you trying to make sure that after something happens in your worker thread that a method is executed within the context of the main thread, or are you just trying to signal the main thread from your worker thread. Probably the easiest way would be to pass a waithandle to your worker thread and get your main thread to wait on the handle (or check it regularly) for the handle to be signaled and then go from there. But if thats the case then you might as well recode the worker thread as an asynchronous method call. The above may not be to your liking :) Looking at the Mono project, the way they have implemented System.Windows.Forms.Control.Invoke() is place the delegate passed ot Invoke() in a queue. This queue is then checked in the windows message loop (which executes in the same thread as the control was created in), and any delegates in the queue are then called. So you could create a synchronized queue, get your worker thread to place a delegate in this queue, and then either get your main thread to periodically check this queue or signal a waithandle, and then get the main thread to execute the delegates in the queue.

          D Offline
          D Offline
          david minor
          wrote on last edited by
          #4

          I am having the same problem. I am calling the FileWatcher component in the .NET framework from a server (middleware) class. If I place this on a form, I'm set, I just hand the FileWatcher class a reference to my form. The file watcher class then calls the form's Invoke method. However I don't have a form, so am forced to implement the ISynchronizeInvoke interface myself. Your suggestion to go look at the mono project seems like a good start. Can you tell me where to find this project? My solution so far is to implement ISnychronizeInvoke and to create an delegate instance that will be executed when the callback is executed. 1. I register a call back. 2. The call back is executed. The sender is the operating system. 3. I then execute a delegate that I instantiated on the "target" thread. David Minor Applications Programmer NC State Archives

          A 1 Reply Last reply
          0
          • D david minor

            I am having the same problem. I am calling the FileWatcher component in the .NET framework from a server (middleware) class. If I place this on a form, I'm set, I just hand the FileWatcher class a reference to my form. The file watcher class then calls the form's Invoke method. However I don't have a form, so am forced to implement the ISynchronizeInvoke interface myself. Your suggestion to go look at the mono project seems like a good start. Can you tell me where to find this project? My solution so far is to implement ISnychronizeInvoke and to create an delegate instance that will be executed when the callback is executed. 1. I register a call back. 2. The call back is executed. The sender is the operating system. 3. I then execute a delegate that I instantiated on the "target" thread. David Minor Applications Programmer NC State Archives

            A Offline
            A Offline
            Andy Davey
            wrote on last edited by
            #5

            The mono project is located at www.go-mono.com[^] HTH Andy

            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