Math math = new Math();
math.Value1 = 1;
math.Value2 = 3;
ThreadPool.QueueUserWorkItem(math.Add);
Now when math.Add is called, it has all the information it needs to do its work.
Thursday, August 6, 2009
Another way to pass data into a thread
In the previous thread post I showed a way you can pass information into a thread. Here is another way in which you instantiate the class containing the method you want your thread to call first.
Easiest way to start a thread
The easiest way to start a process on another thread is to call ThreadPool.QueueUserWorkItem.
Example:
More Info: MSDN: QueueUserWorkItem
Example:
// DoSomeWork is a method.
ThreadPool.QueueUserWorkItem(DoSomeWork);
Note: The DoSomeWork method has to have an Object as a parameter.
private static void DoSomeWork(object info) { // Code that does work. }If you need to pass data into your method, you can do so:
ThreadPool.QueueUserWorkItem(DoSomeWork, "Info")
You will just have to cast the object back into the appropriate data type.
private static void DoSomeWork(object info) { string workInfo = (string) info; // Code that does work. }
More Info: MSDN: QueueUserWorkItem
Wednesday, August 5, 2009
Adding Text to Images
Adding text to an image requires 2 steps:
Why would you do this?
Most likely you wouldn't. You would use a label instead. But you can also draw strings on images and that is most likely what you would use this method for.
More Info: MSDN: Graphics.DrawString Method
- Create a Graphics object
- Create a Font object
- Call Graphics.DrawString method
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Font f = new Font("Arial", 12, FontStyle.Bold); g.DrawString("Copyright 2009", f, Brushes.Black, 10, 10); }
Why would you do this?
Most likely you wouldn't. You would use a label instead. But you can also draw strings on images and that is most likely what you would use this method for.
More Info: MSDN: Graphics.DrawString Method
Tuesday, August 4, 2009
Assigning Images to Windows Controls
Use a Bitmap or Image class.
More Info: MSDN: Image Class
button1.BackgroundImage = new Bitmap("bm.jpg"); // or pictureBox1.BackgroundImage = Image.FromFile("bm.jpg");
More Info: MSDN: Image Class
Creating and Saving a Picture
There are 3 steps to creating and saving a picture:
If you would like to edit an existing image instead, you can use:
- Create a Bitmap object.
- Edit it using Graphics object.
- Call Bitmap.Save method to save it.
Bitmap bm = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bm); g.DrawEllipse(new Pen(Color.Red), new Rectangle(5, 5, 90, 90)); bm.Save("bm.jpg", ImageFormat.Jpeg);Here is what it looks like:
If you would like to edit an existing image instead, you can use:
Bitmap bm = new Bitmap("existingImage.jpg");More Info: MSDN: Bitmap Class
Thursday, July 23, 2009
Nullable
When declaring a value type (int, bool, char, DateTime, etc.) you cannot initialize it to null or you'll get this error message:
To fix this you need to declare your variable as nullable:
More Info: MSDN: Nullable(T) Structure
To fix this you need to declare your variable as nullable:
Nullable<bool> answer1 = null; // or bool? answer2 = null;When this is done, 2 new properties become available to you, HasValue and Value. Now you can check if your variable has been assigned a value yet or if it's still null with HasValue.
if (answer1.HasValue) Console.WriteLine("answer1 is {0}", answer1);
More Info: MSDN: Nullable(T) Structure
Wednesday, June 24, 2009
Authenticated User Information
So you have used the built-in ASP.NET Login controls (Login, LoginView, etc). Now what if you want to get at the authenticated user information in code?
Access it through the HttpContext.Current.User property.
Examples:
More Info: MSDN: HttpContext.Current.User
Access it through the HttpContext.Current.User property.
Examples:
// Get the user's name string userName = HttpContext.Current.User.Identity.Name;
More Info: MSDN: HttpContext.Current.User
Subscribe to:
Posts (Atom)
