Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
My application (Windows Forms) has two thread (A, B). Now i want to run 2 threads in
my app (after 5 seconds to repeat 2 thread)????

Can you help me????
Posted
Updated 17-Apr-11 5:33am
v2

Once a thread is aborted or terminates normally, you can't just restart it. You have to reinstantiate it.
 
Share this answer
 
Comments
Nguyen Tuan 17-Apr-11 11:47am    
Hi John Simmons,
Have you a small example???
Sergey Alexandrovich Kryukov 17-Apr-11 13:28pm    
Correct, my 5.
Therefor, the ultimate solution is to keep it running infinitely.
Please see my Answer.
--SA
Are you going to continuously create new threads at every 5 seconds interval? or think about using a Thread Pool?

If thread pool you can start with here http://msdn.microsoft.com/en-us/library/3dasc8as%28v=vs.80%29.aspx[^] + google + code project has many expert answers already in threading I guess.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Apr-11 13:29pm    
I voted 5 but later noticed you offer a pool -- this is better but not a right solution.
I thing the ultimate solution is mine, please see.
--SA
Nguyen Tuan 17-Apr-11 13:37pm    
Hi Albin Abel,
Not create new thread. I want use created thread.
I use Timer (Windows Control) but my app runs slowly. I thinks thread is solutions
Don't start it again! Make it run forever but keep it in a wait state when not used.

See my recent Solution here: Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^]. Trust me (but try it by yourself), this is much better solution.

—SA
 
Share this answer
 
v2
Comments
Albin Abel 17-Apr-11 13:34pm    
I was thinking about your tip and trick to give in my answer as an additional point, but then just forgot the link. I will bookmark it now. :). My 5
Sergey Alexandrovich Kryukov 17-Apr-11 13:44pm    
Thank you. I got my chance to answer by myself :-)
--SA
Thanks all,

I solved my solution. I change Timers (System.Windows.Forms) to Timers (System.Timers.Timer). My app runs faster...

My experience:
- If you use Timers (System.Windows.Forms): my app is only one thread (UI Thread). This thread must be share to process UI, get data and show data.
- If you use Timers (System.Timers.Timer): my app has 2 threads (UI Thread, second thread - get and show data). Both of two threads can transfer data.

Now, i research Timers (System.Threading.Timers). If i finish, i post the solution (use System.Threading.Timers)
 
Share this answer
 
v3
http://msdn.microsoft.com/en-us/library/ms173178(v=VS.100).aspx[^]

Look there for more information on threads.
 
Share this answer
 
Comments
Nguyen Tuan 17-Apr-11 11:47am    
Hi Fabio V Silva,
Have you a small example???
Fabio V Silva 17-Apr-11 11:48am    
Can you post a snippet of your code and explain in more detail what you are trying to do?
Nguyen Tuan 17-Apr-11 13:49pm    
Hi Fabio V Silva,

My app:

private Thread threadGet;
private Thread threadSet;
private string strHTML = "";

private void GetHTML()
{
try
{
HtmlWindow mainFrame = webBrowserTest.Document.Window.Frames["mainFrame"];
HtmlElement element = mainFrame.Document.GetElementById("element");
strHTML = (element.OuterHtml != null) ? element.OuterHtml : "";
}
catch {}
}

private void SetAreaText()
{
txtHTML.Text = strHTML;
}

private void cmdGetHtml_Click(object sender, EventArgs e)
{
threadGet = new Thread(GetHTML);
threadSet = new Thread(SetAreaText);
threadGet.Start();
threadSet.Start();
}

When i click button cmdGetHtml, HTML shows in txtHTML.
Now i want to auto click button 5 seconds interval??

Note: I fixed safe-thread in Windows Control
Fabio V Silva 17-Apr-11 13:56pm    
I think I would use a Timer in this case if all you want to do is call the function every specified interval of time.
Nguyen Tuan 17-Apr-11 14:01pm    
Hi Fabio V Silva,
I used Timer but my app runs slowly (CPU load 100% at 6th second). Now i want to optimize it. I think Thread solve this solution??????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900