none
passing data from one thread to another RRS feed

  • Question

  • Hi,

    I have created a process that's runing a thread that open some windows form, now i want that other

    running threads from the same process will be able to pass data to this windows form or even close it or reopen it.

    The Windows form gui is always in use of the end users so i cant lock.

    what is the best way to implement it in c# ?  i would like a short example, thanks.



    Thursday, February 15, 2018 8:13 AM

All replies

  • I would to create some queue or something where another threads can write data instead of send it directly to form. In forms there will be method to read this queue which can be written as async and lunched by timer, or another thread. 

    Thursday, February 15, 2018 11:18 AM
  • The Windows form gui is always in use of the end users so i cant lock.

    What does that mean? I don't understand what "end users" mean and I am not sure I understand what you mean by "lock".

    Probably you can use a BackgroundWorker. There are very many samples of it. The members of BackgroundWorker execute as a separate thread except the ReportProgress member executes in the thread that created the BackgroundWorker (usually the UI thread).

    This question should have been put into the Windows Forms forum.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, February 15, 2018 7:43 PM
  • Hi galba1191,

    Thank you for posting here.

    If you want to pass the specified data to UI interface, you can try to use delegate to achieve.

    The you can achieve the constructor of UI using delegate type as parameter. The following code you can reference.

    namespace sent_value
    {
        public delegate void _del(string str);
    
        public partial class Form2 : Form
        {
            public _del del;
            public Form2(_del d)
            {
                this.del = d;
                InitializeComponent();
             
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                del(richTextBox1.Text);
            
            }
        }
    }

    Then you can call the form class.
     private void button1_Click(object sender, EventArgs e)
            {
                Form2 form2 = new Form2(showstring);
                 form2.Show();
            }
    
            private void showstring(string str)
            {
                
            label1.Text = str;
              
            }

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 16, 2018 4:34 AM
  • I don't think that is what the question is asking. My understanding is that there is only one form. The question is not as clear as it could be but it definitely says "other running threads" and nothing about another form.


    Sam Hobbs
    SimpleSamples.Info

    Friday, February 16, 2018 6:51 AM
  • Hello,

     For your project, please follow the link below. Will instruct you on "HowTo" Thread data

    exchanges;

     

     https://docs.microsoft.com/en-us/dotnet/standard/threading/creating-threads-and-passing-data-at-start-time

     

     Hope this helps :)

    Friday, February 16, 2018 7:30 AM
  • I don't think that is what the question is asking. My understanding is that there is only one form. The question is not as clear as it could be but it definitely says "other running threads" and nothing about another form.


    Sam Hobbs
    SimpleSamples.Info

    Yea Sam,

    It is amazing, I see that more and more in the forums. Somebody asks something complete abracadabra. 

    A contributor like you asks to describe the problem in another way. 

    Senseless, we get endless replies including from MSFT with all kind probably not related code and funny enough the OP then marks one of those as answers to cover that they probably not know anymore what they wanted to ask.

    I've read the question at least ten times and even skipping parts, it is about threads, it is about forms, it is about processes but what is the relation between them. 


    Success
    Cor


    Friday, February 16, 2018 8:26 AM
  • Yes it often happens that someone asks a vague question and people ask for clarification but the person asking the question does not respond so then people debate what the question is and what the answer should be. I agree that people should get clarification first and if I have contributed to the confusion here then I apologize.


    Sam Hobbs
    SimpleSamples.Info

    Friday, February 16, 2018 6:24 PM
  • I agree that the question is a bit vague, and making suggestions without fully understanding the question may be a waste of time.

    However the mention of threads and passing data to a Form from multiple threads, makes me tend to agree with the first reply ... the one by @Petr, who suggested using a queueing mechanism of some sort.  But, I'll go a step further, without working too hard, because I already have a blog post about using a database as a queue: It might be worth a look:

    https://geek-goddess-bonnie.blogspot.com/2017/09/transactionscope-and-database-queues.html

    And, yes, MSMQ is made for queueing, and that's another route you could take, but I don't happen to have a blog post on the topic!  ;0)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, February 18, 2018 10:14 PM
    Moderator
  • The questioner has two questions:

    1) How to send data from another thread to form and display it. 

    As I mentioned and you agree with me I would to create some queues system. It is because of displaying data is form responsibility.

    2) Second question is how to close/open form by another thread. It tends to use messages which say to form Close/Open! In this case is better hide/show form only, not destroy. 

    If it is simple application questioner can use only simple queue object and can send objects with command (Show data, Close, Open, ...) and relevant data. There could be some Command Factory which depends on form and create Command object which has Execute method that transforms data to some form action.

    Saturday, February 24, 2018 12:57 PM