c# - How to refresh form while waiting on process - Maybe Threading?


I have a C# app, which tests for connectivity by pinging a server.

When I start the ping processes, I also start a series of 8 cycling graphics that I want to use to show that the application is "processing" and not "hung".

Right before I start the routine for pinging, I start another routine which is used to process the graphics, basically it is a timer control which is set to unhide 1 graphic at a time, in a series of 8, then rehide it, and on the next pass through the timer, the next graphic would unhide, then rehide, etc. (showing motion)

The graphics come up ok, but when the main routine goes to do it's ping, especially noticable on pings that don't reply, the graphic animation doesn't repaint at all until the ping response or failure has come back.

My guess is that I will need to handle the refresh via a second thread, but I don't have a clue how to go about doing that. I've spent the last couple of days trying to read and figure out threading, but am having a tough time of it.

Basically, what I want to be able to do, is:

sequence of events:

1) other code runs from before the ping events
2) a second thread starts, which cycles through the images, and does the refresh's on the main threads form (which would show the animation of the graphics)
3) the ping events would then fire off, and process.
4) after the ping events are ALL done... the second thread (which was used to do the refresh's of the main threads form, would then be terminated. (the second thread terminated, not the main thread)

I am very new to c# but am learning VERY quickly. I'm a "look at working code, and disect it to figure it out" kind of learner, so if someone could help me with how to accomplish this, I would be very appreciative.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This link will give you a better understanding of threading
This should be your sequence

1) start the pinging events "on a separate thread"
2) a second thread starts, which cycles through the images, and does the refresh's on the main threads form (which would show the animation of the graphics)
    For this you have to use Control.Invoke
3) After the Pinging events terminate, terminate the second thread

This way your main application thread will be free to handle its own events and you will get an application that doesn't freeze up while pinging.
Fernando SotoRetiredCommented:
Hi MiSheps;

Have a look a the BackgroundWorker component which spins off a new thread and make threading a little easier.


If you post the code that does the pinging to the other system I may be able to help a little more.

Also when working with thread the only thread that is allowed to modify the values of controls on the GUI is the thread that created it which means that the new threads can not modify controls directly.


Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

MiShepsAuthor Commented:
Thank you for responding guys!

I didn't think about doing the pinging from the new thread... but that makes perfect sense now that I think about it.

So, my question would be, how do I go about doing the following:

1) As part of my ping process, I update a label on my main form to reflect "good ping" or "bad ping" after I determine success, or failure of the ping. How would I update this information on the main form from the secondary thread?

2) What is the proper way to use the invoke process?
MiShepsAuthor Commented:

I have chosen to do it a little differently and wanted to run the thought by you. It seems to be working very well this way, but not sure how "clean" the code is.

What I'm doing is:

1) Main code runs
2) When the code gets to the part where it does the network test, it first kicks off a timer, (in the main form's natural thread) which cycles, creating the graphics animation.
3) Then the code starts a second thread, which does the actual pinging processes. (as you suggested).
4) The next step in the main form (natural primary thread) is that it kicks off another timer which has the following in it:

    private void timer4_Tick(object sender, EventArgs e)
        if (lblNetworkTestOther.BackColor == System.Drawing.Color.LightGreen)
        if (lblNetworkTestOther.BackColor == System.Drawing.Color.Red)

So basically what it is doing is cycling through, waiting for the lblNetworkTestOther.BackColor to change to either Red, or LightGreen (As part of the process of the second thread that we kicked off, after it gets back either a success or failure from the ping, it changes the backcolor of the label to reflect a good, or bad response (green=good, red=bad).

So, because the primary thread is in a loop, waiting for the second thread to update the label on the main form's background color to either green or red before it continues, it keeps the application from getting ahead of itself.

Then, once the secondary thread finishes, it should auto-close (I think?) itself. and then the process continues in the primary thread:

5) Code stops the timer that is waiting for the label color change and moves on to the next process
6) code stops the timer which controls the animation functionality of the graphic
7) code hides the panel that the animation graphic is contained in
8) the rest of the code for addional testing runs.

So, in doing it this way, isn't it similiar to using an invoke? Is there an advantage of using invoke, vs. the way that I've done, or an advantage of handling it the way I am doing now?

Thanks for your help with this one! I'm learning a TON from this experience!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The main purpose of using Invoke is to access a WinForm control from a thread in which it was not created.
Normally when you add a control to a WinForm, the control is created in the Main thread.
So if you start a new thread from the primary thread and you want to access the control from the second thread, then using the Invoke method is safe.
Otherwise, the control will throw an exception.

What you are doing above is also a good approach.
MiShepsAuthor Commented:
ahh, so when I do my next part, I will likely have to use an invoke then, if I understand you right...

The next part I have to fix is a similiar function to the ping, but this one grabs all data about the network cards, populates a datagrid, and then populates a few text boxes.

I have another open thread on that part if you would like to take a look at it. (Right now I'm leaving that thread open hoping to get some feedback from experts on whether or not the code that I build is worth its weight, or if it is junk - My first attempt at using WMI's in C#)

The link for it is:


I of course am going to be awarding you the points for this thread, you have been very helpful! If  you would like to take a look at the other thread, and let me know what you think, like, would change, etc, I still have to award the points to someone on that thread. (no one really responded much to it)

Thanks again!
I think your second thread question has already been answered by experts.
What you are doing is correct.
MiShepsAuthor Commented:
Thank you for your help CuteBug!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.