Click here to Skip to main content
15,887,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string name = openFileDialog1.FileName;
                FileInfo src = new FileInfo(openFileDialog1.FileName);



                Thread t = new Thread(() => startsending(src));
                // t.IsBackground = true;
                t.Start();

                //  sndfle.RunWorkerAsync(index + ":"+openFileDialog1.FileName);



            }
        }
        private void startsending(FileInfo src)
        {

            fileoperatrionform fm = new fileoperatrionform(cp, src);//this is constructor of fileoperationform and here cp is user defined class;
            

        }
Posted
Updated 3-Nov-14 7:34am
v5
Comments
BillWoodruff 3-Nov-14 13:15pm    
Without knowing what 'cp is, and what new fileoperatrionform(cp, src); does: how can we help you ? One thing is certain: the reference to the new Form created 'fm does not exist outside the 'startSending method.
Member 11118232 3-Nov-14 13:19pm    
sorry here cp is a user defined class and fileoperation(cp,src) is constructor to form fileoperationform
cvogt61457 3-Nov-14 13:59pm    
Need more information !!!
What is the error you are seeing?
Probably cross-thread UI exception. But that is a guess.
Member 11118232 3-Nov-14 14:12pm    
i dnt get any error..form open for 1 sec and then automatically closes
Sergey Alexandrovich Kryukov 3-Nov-14 14:11pm    
There is no such thing as "stopping a form". Besides, it is not related to an extra thread: all UI properties/methods should only be called from the UI thread.
—SA

1 solution

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900