I'm still pretty new to VS.NET/C#, so please be easy on me even
though this is probably an easy question. I have the following code
snippet in which I am trying to run a thread and have it return
values so that I can update my progress bar.
Every time I do so, I get the following message:
"The name 'sqlProcess' does not exist in the current context."
Here is the code:
///////////////////////////////////////////
private void button1_Click(object sender, System.EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Interval = 1;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler
(myTimer_Elapsed);
myTimer.Enabled = true;
ghv_sql_res sqlProcess = new ghv_sql_res();
myTimer.Enabled = false;
progressBar1.Value = 80;
}
protected void myTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
progressBar1.Value = sqlProcess.currentCountReturn;
}
///////////////////////////////////////////
I appreciate any and all help.
Thank you,
Russ

Problem with timer and progress bar
tjanuario
I see now... and I’m kicking myself a bit for not catching it... few problems with your last code block...
First up... you are doing a blocking loop in button1_Click() to update the progress bar value... that prevents button1_Click() from ever returning and allowing updating to the screen.
Next, when you are calling sqlProcess.currentCountReturn you are trying to access it as if it were a property, not a method, instead you need to add the parentheses out at the end ala: sqlProcess.currentCountReturn()
Also... you are still declaring your reference to your ghv_sql_res instance inside of your method... making it very difficult for something like a timer to access currentCountReturn
Try this code:
ghv_sql_res sqlProcess;
private void button1_Click(object sender, System.EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 1000;
progressBar1.Value = 0;
sqlProcess = new ghv_sql_res();
ThreadStart currentThreadStart = new ThreadStart(sqlProcess.ghv_sql_res_process);
Thread currentThread = new Thread(currentThreadStart);
currentThread.Start();
timer1.Enabled = true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
public class ghv_sql_res
{
int currentCount = 0;
public void ghv_sql_res_process()
{
DateTime dt = DateTime.Now;
while (dt.AddSeconds(20) > DateTime.Now)
{
currentCount += 50;
System.Threading.Thread.Sleep(1000);
}
}
public int currentCountReturn()
{
return currentCount;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Value = sqlProcess.currentCountReturn();
}
Note I did add my own functionality to ghv_sql_res_process() to force it to take a little extra time.
Thomas Koch
Rajeev Karunakaran
That absolutely makes sense, but with that I am getting this error message:
Error 1 Cannot convert method group 'currentCountReturn' to non-delegate type 'int'. Did you intend to invoke the method C:\Documents and Settings\Russ\Desktop\WindowsApplication1\Form1.cs 124 34 WindowsApplication1
Also, is this setup, is my ghv_sql_res() running as a new thread, or within the current one
Thanks,
Russ
talitore
Basically the function of the program that I have written is to add new records to a database. It iterates through each record and increments the totalCount + 1.
I then pass totalCount value into the totalCountReturn method to return the value to the initial thread. It used to work fine before, and I don't know what the problem is. I even tried to set the totalCount = 1 within the totalCountReturn method, and that still doesn't work.
Thanks for all your help,
Russ
LordDark
Brendan,
Setting this up my process to run in another thread was very easy, and I've worked out everything except one issue. I am getting the following error message:
"Error 1 Cannot convert method group 'totalCountReturn' to non-delegate type 'int'. Did you intend to invoke the method C:\Documents and Settings\Russ\Desktop\WindowsApplication1\Form1.cs 116 24 WindowsApplication1"
My code is basically as follows:
///////////////////////////////////////////////////////////////////////////////////////////////////////////
private
void button1_Click(object sender, System.EventArgs e){
progressBar1.Minimum = 0;
progressBar1.Maximum = 1000;
progressBar1.Value = 0;
ghv_sql_res sqlProcess = new ghv_sql_res(); ThreadStart currentThreadStart = new ThreadStart(sqlProcess.ghv_sql_res_process); Thread currentThread = new Thread(currentThreadStart);currentThread.Start();
int blah = 0; for (int i = 0; i < 10; i++){
progressBar1.Value = sqlProcess.currentCountReturn; Thread.Sleep(1000);}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
public
class ghv_sql_res{
int currentCount = 0; public void ghv_sql_res_process(){
//do work in here
//iterate currentCount
}
public int currentCountReturn()
{
return currentCount;}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Any thoughts on what I am doing wrong
Thanks for all your help,
Russ
chenthorn
What thread ghv_sql_res() is running within depends on how it is working internally... if it internally spawns a thread to do it’s work then it is within that thread that it is running... if however (and more likely) that it along with many internal functions are blocking calls... then it is running in the context of the thread that fired it off.
Without knowing more about the inner workings of your class it is difficult to say exactly how things will behave... one thing I do suspect though is that your constructor is blocking not only execution of button1_Click(), but also the timer’s ability to fire as well. To avoid this, consider having it spawn a thread internally to do its work... so that you are able to return control to the main thread so that the timer can fire.
Berggreen
Any thoughts on this at all
Thanks,
Russ
Krishi
In button1_Click() you are declaring a reference sqlProcess in the line:
ghv_sql_res sqlProcess = new ghv_sql_res();
Because it is declared locally in that function it is not accessible to other functions. One way around this would be to make the declaration an available member of the class and accessible to any method within the class like so:
ghv_sql_res sqlProcess;
private void button1_Click(object sender, System.EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Interval = 1;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler
(myTimer_Elapsed);
myTimer.Enabled = true;
sqlProcess = new ghv_sql_res();
myTimer.Enabled = false;
progressBar1.Value = 80;
}
protected void myTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
progressBar1.Value = sqlProcess.currentCountReturn;
}
You are still creating the object the reference points to in the same place and way... only now the reference is more widely available. Does this make sense
tkmiller
Yes... launch a new thread instead of having the bulk of your work occur in the main thread and luckily threads in .NET are trivially easy... Rather than bore you with a long post of my own on it... take a look at this article from MVP Jon Skeet: http://www.yoda.arachsys.com/csharp/threads/
If you don’t follow what he is doing there feel free to ask more here.
Dmitry Polyakovsky
When you say:
"To avoid this, consider having it spawn a thread internally to do its work... so that you are able to return control to the main thread so that the timer can fire."
Does this mean to launch a new thread, or to continue to use it in the same thread
Also, if it is a new thread, how would I go about doing this
I hate to ask such a basic question, but I can't seem to find a way to make this work based on the examples I've found on the web.
I appreciate your help.
Thanks,
Russ