Hello, Newsgroupians:
I've two questions regarding the System.Threadin g.Thread namespace.
First, what is the point of having a ThreadStart parameter? For example...
using System.Threadin g;
Thread myThread;
myThread = new Thread(new ThreadStart(myF unc)); // This works
myThread = new Thread(myFunc); // And this works
Why not just make it easy and always use the second calling?
My second question is slightly more detailed.
I have a thread that I have created in a class...
using System.Threadin g;
public struct MyPoint
{
public float x;
public float y;
}
Thread m_myThread;
MyPoint m_pt;
class MyClass
{
void ThreadStartFunc ()
{
m_pt.x = 1;
m_pt.y = 2;
m_myThread = new Thread(this.Som eThreadFunc);
}
void SomeThreadFunc( )
{
m_pt.x = 5;
m_pt.y = 6;
}
}
Now suppose I call ThreadStartFunc (); in it, I initialize x and y. When I
debug, I notice the value of x and y are INITIALLY 1 and 2 in the
SomeThreadFunc( ). Later in the thread I change the values, and the thread
terminates. However, when looking at the values in the main thread, I notice
that the values are still 1 and 2. Can someone explain why this is
happening? Thank you for your assistance.
Trecius 2 1682
Trecius <Tr*****@discus sions.microsoft .comwrote:
I've two questions regarding the System.Threadin g.Thread namespace.
First, what is the point of having a ThreadStart parameter? For example...
using System.Threadin g;
Thread myThread;
myThread = new Thread(new ThreadStart(myF unc)); // This works
myThread = new Thread(myFunc); // And this works
Why not just make it easy and always use the second calling?
The second is just shorthand for the first - it's the same type of
parameter. The syntax in the second form is only available as of C# 2.
My second question is slightly more detailed.
I have a thread that I have created in a class...
using System.Threadin g;
public struct MyPoint
{
public float x;
public float y;
}
Thread m_myThread;
MyPoint m_pt;
class MyClass
{
void ThreadStartFunc ()
{
m_pt.x = 1;
m_pt.y = 2;
m_myThread = new Thread(this.Som eThreadFunc);
}
void SomeThreadFunc( )
{
m_pt.x = 5;
m_pt.y = 6;
}
}
Now suppose I call ThreadStartFunc (); in it, I initialize x and y. When I
debug, I notice the value of x and y are INITIALLY 1 and 2 in the
SomeThreadFunc( ). Later in the thread I change the values, and the thread
terminates. However, when looking at the values in the main thread, I notice
that the values are still 1 and 2. Can someone explain why this is
happening? Thank you for your assistance.
See http://pobox.com/~skeet/csharp/threads/volatility.shtml
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
The second question depends on a few things that aren't clear from
your sample, since your code is invalid... mainly *exactly* how m_pt
is handled. In your code it hangs off the namespace, which is illegal.
However, since it is a struct it will follow value-type semantics, so
normally (when benig handed around) it will be cloned. Hence changes
are local to each individual clone, and a good illustration of why
mutable structs are dangerously confusing.
If (in your real code) m_pt is actually a field on an instance that
both can se, it is possible that it *is* updated directly, but if
there is a property in the way it will be cloned... there are also
some scenarios involving "captured variables" that could add
confusion.
Bottom line: unless you really, really know what you are doing (and
why), stick to classes or immutable structs. In this case (point) an
immutable struct seems entirely appropriate.
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: RitaG |
last post by:
Hi.
I Have a VB.Net Windows Application that starts up using Sub Main.
It's a program that runs in the background and checks something every minute.
I use a Timer to accomplish this and it's working but to get it to work I
use a boolean variable (mbLoop) and the program runs until mbLoop is set to
False.
Do While mbLoop
'
Loop
|
by: Hagay Lupesko |
last post by:
Hi,
I've encountered a strange phenomena which appears to me as a bug:
I have an engine that uses a System.Threading.Timer to invoke a delegate
every X minutes.
The code looks something like this:
TimerCallback callBack = new TimerCallback(Run); // run is obviously a
method
|
by: Deiussum |
last post by:
I'm running into an issue where I have a timer that appears to be timing
out immediately, when it shouldn't be timing out for about int.MaxValue
milliseconds. I have written a small app that isolates the problem and
was wondering if anyone had any ideas on what I am doing wrong, or if
there is some known bug, or I am exceeding some known...
|
by: Vjay77 |
last post by:
In this code:
Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not (Me.downloadUrlTextBox.Text = "")
Then
Me.outputGroupBox.Enabled = True
Me.bytesDownloadedTextBox.Text = ""
Me.totalBytesTextBox.Text = ""
|
by: solex |
last post by:
Hello All,
Hopefully someone has run into this error. I have written a class(source
below) that launches a thread to monitor the StandardOutput of a
System.Diagnostics.Process, in particular I am executing the find.exe
program.
The application works perfectly in the development environment. As soon as
I execute the compiled program and...
| |
by: Stressed Out Developer |
last post by:
We have an application that has a 200 count loop that does the following:
' Each time thru the loop we pass the next IP Address is a range (aka
192.168.4.50 thru 192.168.4.254)
Try
If mUIO_Threads(i) Is Nothing Then
mUIO_Threads(i) = New System.Threading.Thread(AddressOf
mUIO_DAQ(i).InitDAQ)
mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr...
|
by: clevrmnkey |
last post by:
I've had nothing but trouble from the System.Net.Mail objects, but I
finally need to make them work, and I can't for the life of me see what
I'm doing wrong.
I pared back my mail transaction to the bare minimum:
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("<my
mail server IP>", 25);
smtp.Send("<one of my email...
|
by: PimpDaddy |
last post by:
Here is the source code of my C++/ME app. When I run it I can use "Windows Task Manager" to see that thread HANDLEs are leaking.
#include "stdafx.h"
#include <omp.h>
public __gc class TestClass
{
public:
void ThreadProc()
|
by: Paul Schwann |
last post by:
Hi group,
I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:
Suppose you have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |