
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Notes: Think .Net Managed code for all my statements below.
I'm just starting to get into the habit of attempting to use Threading when I can within my ASP.Net Web Applications and have a little confusion that I hoped anyone could clarify.
Here is a simple implementation that I am doing right now. I have some code that writes a log record to a database and I don't want this step to impact the users web page delivery experience so I thought I would use a different Thread to do this. Ok so far so good and I began to look a the Thread class. With the Thread class I see that I can create an object and then create a new Thread. Question is for all the samples that I have seen the examples do not use populated object properties for the Thread function instance method call. In all cases it appears that they choose to use Asynch Delegate calls so I went that route. So here are my questions.
1>Can you instance a class and use populated properties of that class from the Thread Method Instance Call or is that a bad idea.
2> Is there a way to set thread priorities on Delegate calls.
3> Are there any guidelines of when to useclass instances or when to use static methods. Samples I see do not declare Public or Private on these methods but I found that it didn't seem to matter as long as the methods for the class that creates the thread is in the same class.
4> I was wondering if there are any recommended best approachesor and safety considerations to take into account when using managed Multi-threaded code.
5> Am I really making best us of Dual CPU Xeon system?
FYI - Here is the final code that I came up with..
using System.Net;
using System.IO;
using System.Threading;
using System.Runtime.Remoting.Messaging;
///
/// Summary description for Download.
///
public class Do wnload : System.Web.UI.Page
{
private const string INTERNET_EXPLORER_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461; .NET CLR 1.0.3705)";
delegate void SaveDownloaderInfo(StoreTheData dataToStore);
SaveDownloaderInfo saveDownloaderInfo = new SaveDownloaderInfo(SaveData);
saveDownloaderInfo.BeginInvoke( new StoreTheData(fileName, email, referrer, from), new AsyncCallback(AfterSaveData), "this is state");
private static void SaveData( StoreTheData storeTheData)
{
string fileName = storeTheData.fileName;
string email = storeTheData.email;
string referrer = storeTheData.referrer;
string from = storeTheData.from;
string ConnectionString = GetSqlConnection();
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand("dbo.usp_I_RecordDownloadOfFile", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandTimeout = 90;
myCommand.Parameters.Add("@fileName", SqlDbType.NVarChar, 256).Value = fileName;
myCommand.Parameters.Add("@email", SqlDbType.NVarChar, 1024).Value = email;
myCommand.Parameters.Add("@referrer", SqlDbType.NVarChar, 1024).Value = referrer;
myCommand.Parameters.Add("@from", SqlDbType.NVarChar, 1024).Value = from;
myCo nnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
private static void AfterSaveData(IAsyncResult result)
{
AsyncResult async = (AsyncResult) result;
SaveDownloaderInfo saveDownloaderInfo = (SaveDownloaderInfo)async.AsyncDelegate;
saveDownloaderInfo.EndInvoke(result);
}
private static string GetSqlConnection()
{
return System.Configuration.ConfigurationSettings.AppSettings["SiteSqlServer"].ToString();
}
and finally
public class StoreTheData
{
public string fileName = ""; //
public string email = "";
public string referrer = "";
public string from = "";
public StoreTheData(string FileName, string Email, string Referrer, string From)
{
fileName = FileName;
email = Email;
referrer = Referrer;
from = From;
}
}
Are there any safety considerations I should take into account..
Link Copied

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page