Does not support a STA thread for multiple handles WaitAll

The experimental course is going to make a multi thread computing m*n numbers and procedures, the number stored in the txt document,
When I set [STAThread]
"That does not support a STA thread for multiple handles WaitAll"
When set to [MTAThread]
"Before you can call OLE, must be the current thread is set to single threaded apartment (STA) mode"
Would like to ask is:
What caused the problem,
What is the.Net set the com threading model benefits, but also the default is[STAThread]
For example, in this process I should do not have a COM component.,
Application is not a COM interop.,


namespace CEvent_File
{
    public partial class Form1 : Form
    {
        private string[] nums;
        private int sum;
        private ManualResetEvent[] doneEvents;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgFile = new OpenFileDialog();
            dlgFile.Title = "Select the file";
            dlgFile.InitialDirectory = @"H:\ personal practice, multi thread, multi thread computing read file";
            dlgFile.Filter = "Txt files |*.txt| all files (*.txt)(*.*)|*.*";
            dlgFile.FilterIndex = 1;
            //Thread.CurrentThread.SetApartmentState(ApartmentState.STA); 
            if (dlgFile.ShowDialog() == DialogResult.OK)
            {

                FileStream fs = (FileStream)dlgFile.OpenFile();
                StreamReader read = new StreamReader(fs);
                string fileContent = read.ReadToEnd();
                if (fileContent == "")
                {
                    MessageBox.Show("File is empty´╝ü");
                    return;
                }
                nums = fileContent.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                read.Close();

                int count = nums.Length;
                lblCount.Text = count.ToString();
                int threadCount = ((count % 10 == 0) ? (threadCount = count / 10) : (threadCount = count / 10 + 1));
                lblThread.Text = threadCount.ToString();
            }
        }
        private void btnCount_Click(object sender, EventArgs e)
        {
            int threadCount=Convert.ToInt32(lblThread.Text.ToString());
            Caculate []cas=new Caculate[threadCount];
            doneEvents = new ManualResetEvent[threadCount];
            string[] _nums = new string[10];

            for (int i = 0; i <threadCount; i++)
            {
                doneEvents[i] = new ManualResetEvent(false);
                if ((i + 1) != threadCount)
                {
                    for (int s = i * 10; s <i * 20; s++)
                    {
                        _nums[s-10] = nums[s];
                    }
                }
                else
                {
                    for (int s = i * 10; s <threadCount; s++)
                    {
                        _nums[s - 10] = nums[s];
                    }
                }
                cas[i] = new Caculate(_nums, doneEvents[i]);
                Thread th = new Thread(cas[i].threadMethod);
                //th.SetApartmentState(ApartmentState.MTA);
                th.Start();
            }
            //Thread.CurrentThread.SetApartmentState(ApartmentState.MTA);
            WaitHandle.WaitAll(doneEvents, -1);
            foreach (Caculate c in cas) 
            {
                sum += c.sum;
            }
            MessageBox.Show("Results for the"+sum.ToString());
        }
    }
    public class Caculate
    {
        private string[] _nums;
        private ManualResetEvent doneEvent;
        public int sum;
        public Caculate(string[] nums, ManualResetEvent e)
        {
            _nums = nums;
            doneEvent = e;
        }
        public void threadMethod()
        {
            sum = getSum(_nums);
            doneEvent.Set();
        }
        public int getSum(string[] _nums)
        {
            int sum = 0;
            foreach (string num in _nums)
            {
                sum += Convert.ToInt32(num);
            }
            return sum;
        }
    }
}

Started by Wendy at November 16, 2016 - 8:27 PM

There was no

Posted by Wendy at November 25, 2016 - 9:10 PM

Some of the shell component of Winforms, such as FolderBrowserDialog
And if you refer to some of the COM components, for compatibility, you must use the[STAThread]

Even if the program itself is not exposed as a COM component and design.

Posted by Johnny at December 07, 2016 - 9:25 PM