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. Passing Parameters to a running thread

Passing Parameters to a running thread

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • C Offline
    C Offline
    Christopher Thornburg
    wrote on last edited by
    #1

    Okay, I am trying to implement a way to pass parameters to an already running thread or application. I have added some "Run Once" logic to an application of mine and it works great, if you try to load it twice...it looks for the thread by name, and if it finds it it just activates it. It works great...BUT...I want to pass 2 params to the running thread and this is how I am doing it and it is not working...not totally. So once my app is running, I will call the exe again and pass 2 params to it, when the second instance tries to start, I receive the params and save them to my properties.settings.default.variable1, variable2. then save the settings, check my thread, and if its running I just activate the running thread and exit. When the activated event fires I then look at the value of my properties.settings.default.whatever and the data has not changed, BUT if I exit the app at that point and start it again...the values have changed. Its like the running thread has a different session of the data open and it dosen't see the changes. So my question is: Is there a way to refresh the data session of the properties.settings stuff so I can see the last changes made??? or is there another way to pass the params to my running thread from the started thread???? Thanks...

    Christopher J. Thornburg Senior Systems Analyst Ideal Card

    P S A 3 Replies Last reply
    0
    • C Christopher Thornburg

      Okay, I am trying to implement a way to pass parameters to an already running thread or application. I have added some "Run Once" logic to an application of mine and it works great, if you try to load it twice...it looks for the thread by name, and if it finds it it just activates it. It works great...BUT...I want to pass 2 params to the running thread and this is how I am doing it and it is not working...not totally. So once my app is running, I will call the exe again and pass 2 params to it, when the second instance tries to start, I receive the params and save them to my properties.settings.default.variable1, variable2. then save the settings, check my thread, and if its running I just activate the running thread and exit. When the activated event fires I then look at the value of my properties.settings.default.whatever and the data has not changed, BUT if I exit the app at that point and start it again...the values have changed. Its like the running thread has a different session of the data open and it dosen't see the changes. So my question is: Is there a way to refresh the data session of the properties.settings stuff so I can see the last changes made??? or is there another way to pass the params to my running thread from the started thread???? Thanks...

      Christopher J. Thornburg Senior Systems Analyst Ideal Card

      P Offline
      P Offline
      pbraun
      wrote on last edited by
      #2

      If I am understanding your problem correctly, what is needed is to have a data location where the modifications are made and the thread can access the data. This should eliminate the need for parameters. Keep in mind that when multiple threads are accessing a data location, it should be protected in such a way that only one thread can access/modify the data at a time. Phil

      C 1 Reply Last reply
      0
      • P pbraun

        If I am understanding your problem correctly, what is needed is to have a data location where the modifications are made and the thread can access the data. This should eliminate the need for parameters. Keep in mind that when multiple threads are accessing a data location, it should be protected in such a way that only one thread can access/modify the data at a time. Phil

        C Offline
        C Offline
        Christopher Thornburg
        wrote on last edited by
        #3

        Such as writing the params to a text file and then reading them in duing the activate event of the form???

        Christopher J. Thornburg Senior Systems Analyst Ideal Card

        P 1 Reply Last reply
        0
        • C Christopher Thornburg

          Such as writing the params to a text file and then reading them in duing the activate event of the form???

          Christopher J. Thornburg Senior Systems Analyst Ideal Card

          P Offline
          P Offline
          pbraun
          wrote on last edited by
          #4

          That is one method used when dealing with multiple applications. There are other methods as well. For instance using a registry key, a memory mapped file, interprocess communication, and so on. Phil

          1 Reply Last reply
          0
          • C Christopher Thornburg

            Okay, I am trying to implement a way to pass parameters to an already running thread or application. I have added some "Run Once" logic to an application of mine and it works great, if you try to load it twice...it looks for the thread by name, and if it finds it it just activates it. It works great...BUT...I want to pass 2 params to the running thread and this is how I am doing it and it is not working...not totally. So once my app is running, I will call the exe again and pass 2 params to it, when the second instance tries to start, I receive the params and save them to my properties.settings.default.variable1, variable2. then save the settings, check my thread, and if its running I just activate the running thread and exit. When the activated event fires I then look at the value of my properties.settings.default.whatever and the data has not changed, BUT if I exit the app at that point and start it again...the values have changed. Its like the running thread has a different session of the data open and it dosen't see the changes. So my question is: Is there a way to refresh the data session of the properties.settings stuff so I can see the last changes made??? or is there another way to pass the params to my running thread from the started thread???? Thanks...

            Christopher J. Thornburg Senior Systems Analyst Ideal Card

            S Offline
            S Offline
            Stefan Prodan
            wrote on last edited by
            #5

            You can make a static class that exposes properties that you can set/get from different threads. So when your activated event fires the thread will get the value of the property. Because you are using a multi-threading application the static properties should use lock {...} inside the get/set.

            http://stefanprodan.wordpress.com

            1 Reply Last reply
            0
            • C Christopher Thornburg

              Okay, I am trying to implement a way to pass parameters to an already running thread or application. I have added some "Run Once" logic to an application of mine and it works great, if you try to load it twice...it looks for the thread by name, and if it finds it it just activates it. It works great...BUT...I want to pass 2 params to the running thread and this is how I am doing it and it is not working...not totally. So once my app is running, I will call the exe again and pass 2 params to it, when the second instance tries to start, I receive the params and save them to my properties.settings.default.variable1, variable2. then save the settings, check my thread, and if its running I just activate the running thread and exit. When the activated event fires I then look at the value of my properties.settings.default.whatever and the data has not changed, BUT if I exit the app at that point and start it again...the values have changed. Its like the running thread has a different session of the data open and it dosen't see the changes. So my question is: Is there a way to refresh the data session of the properties.settings stuff so I can see the last changes made??? or is there another way to pass the params to my running thread from the started thread???? Thanks...

              Christopher J. Thornburg Senior Systems Analyst Ideal Card

              A Offline
              A Offline
              AFSEKI
              wrote on last edited by
              #6

              Use: Thread.Start(object) method as: public void Start ( Object parameter ) where you pass a static or a dynamic but(reference is kept to prevent garbage collection "WeakReference can also be considered for this case") object which includes your parameters that you can change while the thread is running. ALSO, searching a Thread by it's name is not preferred, try keeping a reference to each thread you create, better an integer value or byte(if you don't have more than 255 threads in your application ;) ) and access these threads with these indexes. You can also use a Dictionary, anyway. As a result you can modify your parameters in the objectargument you pass and also keep a reference of in you main thread. BUT, be careful chosing and a using a type for your object argument. If you are using value types, modify the real data accessing it through "ref" keyword.

              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