none
Attempting to run threading but stuck - please help RRS feed

  • Question

  • So I have a button on my c# ASP.Net app that has this code:

    private void startDownload_Click(object sender, EventArgs e)
    {
    					string connectionString = "Data Source=dataSource;Initial Catalog=initialCatalog;Integrated Security=SSPI;";
                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            SqlCommand command = new SqlCommand(sqlQueryTxtBox.Text, connection);
                            try
                            {
                                connection.Open();
                                SqlDataReader dr = command.ExecuteReader();
                                try
                                {
                                    while (dr.Read())
                                    {
                                        try
                                        {
                                            Thread t = new Thread(() => {
                                                downloadFiles(dr["a"].ToString(), dr["b"].ToString(), dr["c"].ToString(), dr["d"].ToString());
                                            });
                                            t.Start();
                                        }
                                        catch
                                        {
                                            status.text = "Failed";
                                        }
                                    }
                                }
                                finally
                                {
                                    dr.Close();
                                }
                                connection.Close();
    					}
    	}
    }

    Which I ultimately want to be able to run three threads at a time so I get three times as many files in the same time frame as it calls this code:

    public void downloadFiles(string a, string b = null, string c = null, string d = null){
    	DoSomething(a, b, c, d);
    }

    but I keep getting an error when I use the above to thread my calls. I keep looking online but have not stumbled across anything. Any help would be greatly appreciated!

    • Moved by Fei Hu Thursday, April 5, 2018 6:53 AM Moved From C#
    Wednesday, April 4, 2018 3:00 PM

All replies

  • Can you provide the error details. What kind of error you are getting and on which specific line it is ?

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, April 4, 2018 6:14 PM
  • You don't show us what DoSomething does and that is probably the important part. You don't tell us where the error is or what the error is. You probably should put "need a psychic" in the title.

    There is probably a limit on the number of files that can be downloaded simultaneously. You might be hitting that limit. There might be a good reason for the limit.

    Downloads typically occur asynchronously, or at least there is probably an option to do them asynchronously. It is likely a waste of resources to do a download in a separate thread.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, April 5, 2018 4:11 AM
  • Hi Chuck,

    According to your description, your issue is related about ASP.NET apps. it will be more appropriate to ask your question at ASP.NET Forums.

    Thank you for your understanding.

    Regards,

    Frankie


    MSDN Community Support
    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.

    Thursday, April 5, 2018 6:52 AM