wrong

R

RobcPettit

Hi, Im experimenting with threading and classes and Im getting wrong
return type. my class is called bjresults with:
public int getrecordleft()
{
return linestartotal;
}
linestartotal has already been set and works ok, just trying to get the
value. my thread is
BJresults tr = new BJresults();

ThreadStart recs = new ThreadStart(tr.getrecordleft);
Thread thread = new Thread(recs);
thread.Start();

{
Console.WriteLine();
Thread.Sleep(500);
}
I know the consolewritelines wrong, cant get past the tr.getrecordleft.
I think Im calling it wrong but dont know whats wrong.
Ive managed to get one thread working with
ThreadStart job = new ThreadStart(sr.GethtmlStream);
Thread thread = new Thread(job);
thread.Start();
but it doesnt ask for any return values. So thats ok. Any ideas.
Regards Robert
 
B

Barry Kelly

Hi, Im experimenting with threading and classes and Im getting wrong
return type. my class is called bjresults with:
public int getrecordleft()

ThreadStart requires a void return type. The alternative (2.0+),
ParameterizedThreadStart, takes an object argument as well as returning
void.
ThreadStart recs = new ThreadStart(tr.getrecordleft);

So, you should get a compiler error here, saying that the return type of
getrecordleft is wrong.
but it doesnt ask for any return values. So thats ok. Any ideas.

To get some results out of the thread, you must stuff them away
somewhere that the thread delegate has access to. It wouldn't make sense
for the function to return a value anyway, because if it did, where
would you retrieve it? The logical way to get return values is to block
until the function returns, and that defeats the purpose of threads. To
return a value out of the thread routine, you need to store it
somewhere. For example:

---8<---
using System;
using System.Threading;

class Calculator
{
private int _result;

public void Calculate()
{
Thread.Sleep(TimeSpan.FromSeconds(2));
_result = 42;
}

public int Result
{
get { return _result; }
}
}

class App
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine("Starting value: {0}", calc.Result);
Thread calcThread = new Thread(new ThreadStart(calc.Calculate));
calcThread.Start();

// Do other work in this thread...

calcThread.Join();
Console.WriteLine("Final value: {0}", calc.Result);
}
}
--->8---

HTH,

-- Barry
 
R

RobcPettit

Thankyou for your reply. I can see I will have to take some time with
threading, to understand it. Best to start with some small projects and
work my way up. In my first thread it creates the linestartotal, and
counts down every time it procceses a record. What im trying to do with
the second thread is give the user some feedback as to what number
record linestartotal is upto. Ive read about invoking, am i right in
thinking this is when you want to access another thread for some
results. At the momment Im writing this as a console application.
thanks again
Regards Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top