954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Background Working

Hi all,

I am looking to have a long process running in the background to my main form with a timer ticking on the main form animating the elipsis on the end of some text.

Was looking for some insight into the best way to go about this and any decent resources worth reading on the subject, or a starter.

Im aware of the BackgroundWorker class so im assuming this will be used but implementation is where im falling down.

Cheers,

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

Look into the System.Threading.Thread class and the System.Threading namespace to run code in parallel.

.Net 3.5 and up also have something called a TaskFactory, which handles a lot of the overhead and complications that can arise when making multithreaded apps. I typically do all the threading myself, but my cooworkers swears by the TaskFactory.

Keep in mind that if you access something on one thread at the same time as on a different thread you will have big problems (cross threading violations, race conditions, and other unexpected results).

Here's a simple example of a multithreaded app using System.Threading.Thread.

volatile bool bKillThread = false;
delegate void myCallBackDelegate();
void StartNewThread()
{
   Thread t = new Thread(new ThreadStart(myParallelMethod))
   t.Start();
   //Stuff here will run in parallel with the thread we just started
}
void myParallelMethod()
{
   while (!bKillThread)
   {
       CallbacktoMainThread();
       Thread.Sleep(500); //Pause for 500 ms to avoid overloading cpu
   }
}
void CallbacktoMainThread()
{
   //thread safe checking
   if (InvokeRequired) Then
      Invoke(new myCallBackDelegate(CallbacktoMainThread))
   else
      DoSomething(); 
}
skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Thank you Skata, I will look at this over the weekend and come back to you :)

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

Sorry for the delayed reply, that has worked perfectly thanks, problem solved :)

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: