Good afternoon,
I'm doing a code that every x time you access a web hosting and puts some information there. For that I am using threads, but I must be doing something wrong since the code is only called one time ...
I must be doing something wrong since I am beginner in this language. The idea is the application running continuously.
I'll put here the piece of code that performs what I want to be able to help:
private void Form1_Load(object sender, EventArgs e)
{
ActualizarBB ActualizarBBt = new ActualizarBB();
Thread oThread = new Thread(new ThreadStart(ActualizarBBt.Actualizar));
try
{
oThread.Start();
Thread.Sleep(15);
}
catch (ThreadStateException ext)
{
MessageBox.Show(ext.ToString());
}
}
Loading
Sam HobbsPosted Jan 20, 2011, 4:11 PM
As Suthish implies, using Thread.Sleep will cause the main thread (the form) to wait, which means that the form will be frozen while the Thread.Sleep is executing and therefore there will be no advantage to use the other thread. Look for articles in this web site showing how to use the Timer control. Also, please read the documentaton; I am not sure, but I think that Thread objects cannot be re-used; in ohter words, for each oThread.Start, you probably must do a "new Thread" to create a new thread object.
Alternatively, you can create the other thread only once and put the Thread.Sleep in the other thread. I prefer to not use Thread.Sleep but it would probably work. You need to have a loop to do the accessing of the web hosting in a loop and you need to have a way to stop the thread so the thread could do a while loop that loops until an event indicaters that it should stop. Please read articles about threading and thread synchronization; use of Event objects or their equivalent is called thread synchronization. You need to read about threads to understand how to get the main thread to be able to tell the background thread to stop.
Jaish MathewsPosted Jan 20, 2011, 1:41 PM
Below are my suggestion
Suthish NairPosted Jan 20, 2011, 1:05 PM