Running in threads...

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

Ok,
If I have a process start in a thread other than the "main" of the
program, can that process call other functions that are not in its
thread without any problems? Or do I have to include those functions
in the same "thread" class?

example:
public class mythread
{
public static void thread
{
///do something here
///now call function in another class <-is this OK?
}
}

Thanks,
Trint
 
Trint,

The methods do not exist on the thread itself. Rather, the thread
provides an execution context for that method. You can call ANYTHING that
you want in the thread. You can call static members, or instance members.
For instance members, you have to have a reference to the instance somehow
(there are many ways to do this).

What you need to worry about is how calling properties/fields/methods
will be affected if two or more threads are trying to access it at the same
time. Take a look at the lock statement, as it will be your primary
mechanism for ensuring that you maintain the state of the objects you call
correctly.

Hope this helps.
 
UI events should be via invoke on the UI thread.
[not exactly the original question but worth knowing]

- Colin

Nicholas Paldino said:
Trint,

The methods do not exist on the thread itself. Rather, the thread
provides an execution context for that method. You can call ANYTHING that
you want in the thread. You can call static members, or instance members.
For instance members, you have to have a reference to the instance somehow
(there are many ways to do this).

What you need to worry about is how calling properties/fields/methods
will be affected if two or more threads are trying to access it at the same
time. Take a look at the lock statement, as it will be your primary
mechanism for ensuring that you maintain the state of the objects you call
correctly.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

trint said:
Ok,
If I have a process start in a thread other than the "main" of the
program, can that process call other functions that are not in its
thread without any problems? Or do I have to include those functions
in the same "thread" class?

example:
public class mythread
{
public static void thread
{
///do something here
///now call function in another class <-is this OK?
}
}

Thanks,
Trint
 
Back
Top