Click here to Skip to main content
15,881,741 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have a Winform app that has a backgroundWorker thread and does all operations via that only.

After completing one particular operation i.e. to connect to the server, I want to create a monitor to check the connectivity with the server is alive or not. A web service will be executed to find out the connectivity and check the results to know. If not reconnect it. I believe I should create a thread for monitoring connectivity. The logic is like : After each X secs monitor checks the connectivity, again comes back after X secs and again does its job i.e check connectivity and know the status. I don't think their is any need to block the thread as I can react whenever I get the results. I also don't need to deal with any UI inside this process. Might only need once I find the connectivity is lost, then have to call for other function that reconnects and this thread again restarts or something like that.

To be very frank, I am week in Threading. I read the 2-3 tutorials, but can't make out how to implement this. Can anyone please give me some hint/idea to implement the above.

Implemented Code of Form :
C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    if (currentState == OPENING)
        LoadAfterLogin();
    else if (currentState == CONNECTING)  
         Connect();    // CONNECTS TO THE SERVER
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   ......
   else if (value == CONNECTED)
   {
    currentState = CONNECTED;
    statusLabel.Text = "CONNECTED Successfully to server";
    //  HERE MONITOR THREAD SHOULD BE STARTED ONCE APP KNOWS FOR SURE, THAT IT IS CONNECTED TO SERVER
    monitorConn = new MonitorConnection();
   } else if (value == EXP)
   {
        currentState = ERR_CONNECTING;
        ExceptionData ed = (ExceptionData)e.UserState;
        .....
   }
}  

////////////////////////////////////////
ANOTHER MonitorConnection thread class 

public class MonitorConnection : Thread
{
    private DateTime startTime, lastCheckedTime;
    private bool conneced;
    private int intervalDuration, nextTime;

    public MonitorConnection()
    {
        intervalDuration = 10000;
    }


    public bool IsConneced
    {
        get { return conneced; }
        set { conneced = value; }
    }

    // SHOULD CALL THIS WHEN THE THREAD IS STARTED
    public void start()
    {
        startTime = DateTime.Now;
        nextTime = DateTime.Now;
    }

    // THIS SHOULD BE SOMEHOW CALLED BY run()
    public void checkConnection()
    {
        // IS CURRENT TIME IS >= SCHEDULED NEXTTIME
        if (DateTime.Now >= nextTime) {
            // Checks if the connection to server is alive or not
            conneced = UltimateLibrary.http.HTTPUtility.isConnectionAvailable();
            lastCheckedTime = DateTime.Now;
            nextTime = lastCheckedTime + intervalDuration;
        }

    }

}


Here is the code that have implemetned to show the logic that I want to achieve. I agree and know the code is not fully to its mark. There still needs changes to make it proper by not blocking, running on continous intervals and so on. I think some other class than Thread should be extended (not sure which one will be appropriate).

Any help is highly appreciated.

Thanks
Posted
Updated 27-Feb-11 20:20pm
v6
Comments
All Time Programming 25-Feb-11 12:49pm    
Marcus & OriginalGriff, Kindly have a look at the updated question. Thwie was mistake in my question and try to help me regarding it. -Thanks
All Time Programming 28-Feb-11 6:13am    
Any help/guidance for the above query ?

You should perhaps read this article. It is a little dated, but a good way to understand this a bit better.
The MSDN Documentation is pretty good as well.
I think what I'm curious about is why you need to monitor the connectivity. In the newer technologies, the connection is better served to be opened when beginning a transaction with the database. Multiple connections will be managed and pooled under the hood, so I'm not sure if you are just misunderstanding this or if you have a different logical process that requires your thought process.
 
Share this answer
 
Comments
Espen Harlinn 25-Feb-11 11:17am    
Good links :)
All Time Programming 28-Feb-11 1:16am    
Kindly refer the changes doen in the question and try to help accordingly. Please. I don't understand which aspect of threading to use and how to start with it. Once I get that part, will be ble to move ahead with it. Would be glad if you can help for it also. Thanks.
No! Don't do it!

Database connections are scarce system resources and should be open for as short a time as possible: Open it, read or write the database, then close and dispose the connection.

Having a thread there just to check it is open and if not re-open it is not going to help anybody: If it is closed in the main thread, the background thread cannot always get in to open it again before it is used...
 
Share this answer
 
Comments
fjdiewornncalwe 25-Feb-11 11:26am    
Agreed. +5. That is why I ask him for his thought processes regarding why he is trying to do it.
All Time Programming 28-Feb-11 6:15am    
OriginalGriff, my mistake regardng asking for DB connectivity. Sorry its for Server connectivity. It would be good if you can look at the code and guide me accordingly. - Thanks
Dave Kreskowiak 14-Jun-11 12:38pm    
What kind of server are we talking about? There is no persistent connection to a web service, so this would be completely pointless.
Hi to All,

This is the solution if at all it might be helpful for any :
I didn't extend any class to MonitorConnection.
Added a event in MonitorConnection that fires when the connection is lost (after starting the monitor) and is handled in main application.

This way I got control of things and its working also perfectly.


Thanks to all.
 
Share this answer
 

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