How to start/stop a timer which is an a module from another module?

  • Thread starter Thread starter Zlajoni
  • Start date Start date
Z

Zlajoni

Hi, I am a bigginer at c# so I have problems accesing my timer from
another module. I supose that this is possible to do, I just don't
know how?

What I have is an Form which has a timer, somthing like this (Main
Module):
namespace RasTrailTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btHangup;
private System.Windows.Forms.ListBox liBStatus;
private System.Threading.Timer FtpErrorTimer;

public Form1()
{
InitializeComponent();

if (FtpErrorTimer == null)
// first time through, create the timer
FtpErrorTimer = new System.Threading.Timer(new TimerCallback
onFtpErrorElapsed), null,
System.Threading.Timeout.Infinite,System.Threading.Timeout.Infinite);
}

public void StopErrorTimer()
{
FtpErrorTimer.Change(
System.Threading.Timeout.Infinite,System.Threading.Timeout.Infinite);
}

public void StartErrorTimer(int interval)
{
FtpErrorTimer.Change(interval, 0);
}

void onFtpErrorElapsed(object state)
{
StopErrorTimer();
//rest of the code here
}
}
}

That is a shortend version of my form module.

Here is the module which from I some how want to call
StartErrorTimer() and StopErrorTimer():

public class FtpClient
{
public FtpClient(string server, string username, string password)
{
this.server = server;
this.username = username;
this.password = password;
}

public void Download(string remFileName,string
locFileName,Boolean resume)
{
//SomeCode
//I WOULD LIKE TO CALL THE TIMER HERE!!!!!HOW TO DO THIS?
}

{

Is there anybody who can show me how this can be done? I have tried by
creating new Form1 object in my Ftp Module, but that would't help
because I would create a new Form on: Form1 frm = new Form1();

Best Regards
Zlajoni
 
Just pass an instance of your form to FtpClient.

public FtpClient(Form1 form, string server, string username, string
password)
....

However I'm not sure that it's correct to mix UI with logic. Have you
considered to move your timer from Form1 to FtpClient class and expose
some events that form could handle it:

class Form1
{
...
ftpClientInstance.FtpErrorElapsed += DoSomeWork...
}

class FtpClient
{
public event EventHandler FtpErrorElapsed;
...
}

Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Hi again,

I have considered your suggestion, and I think you are right it isn't
such a good idea to mix UI with logic. So I have now moved my
FtpTimeout timer to my Ftpcli class.
What I am trying to do is to throw an exception in the FtpTimer every
time the timer timeouts, but I just can't catch that exception if the
timer throws it.

Here is how I am trying to catch it:
-------------------------------------------------------------------------------try
{
FtpCli.Download(ReciveDirList, ArmDataOut +
ReciveDirList,true);
}
catch (Exception e)
{
StopErrorTimer();
FtpCli.Close();
FtpCli.IsLoggedIn = false;
}

-------------------------------------------------------------------------------
Here is where I fire my timeout event if the 10 sek's have elapsed, I
start the timer for every packet of data. This peace of code is from
FtpCli.Download ():

while ( timeout > DateTime.Now )
{
StartTimer(10000);
this.bytes = cSocket.Receive(buffer, buffer.Length, 0);

output.Write(this.buffer,0,this.bytes);

if ( this.bytes <= 0)
{
break;
}


//Added by Zlatan: If this isent done, your downloads wil have to be
finished with in the timout seconds.//This is not correct
Timeout logick. The timeout should be the time between each TCP
packet, not the whole transfer.

timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);

StopTimer();
}

-------------------------------------------------------------------------------
Here is my FtpTimeout event where I am throwing the exception:

void onTimerElapsed(object state)
{
StopTimer();
throw new Exception("FTP TIMEOUTED");
}

Exception gets thrown fine, but I just can't catch it the exception
messagebox keeps popping up.

Hope somebody can tell me what am doing wrong? The reason why I
haven't done this with events is that am not sure how do that.

Btw, thanks for the replay Sergey =)

Please help, anybody!

Best Regards
Zlatan
 
Hi again,

I have considered your suggestion, and I think you are right it isn't
such a good idea to mix UI with logic. So I have now moved my
FtpTimeout timer to my Ftpcli class.
What I am trying to do is to throw an exception in the FtpTimer every
time the timer timeouts, but I just can't catch that exception if the
timer throws it.

Here is how I am trying to catch it:
-------------------------------------------------------------------------------try
{
FtpCli.Download(ReciveDirList, ArmDataOut +
ReciveDirList,true);
}
catch (Exception e)
{
StopErrorTimer();
FtpCli.Close();
FtpCli.IsLoggedIn = false;
}

-------------------------------------------------------------------------------
Here is where I fire my timeout event if the 10 sek's have elapsed, I
start the timer for every packet of data. This peace of code is from
FtpCli.Download ():

while ( timeout > DateTime.Now )
{
StartTimer(10000);
this.bytes = cSocket.Receive(buffer, buffer.Length, 0);

output.Write(this.buffer,0,this.bytes);

if ( this.bytes <= 0)
{
break;
}


//Added by Zlatan: If this isent done, your downloads wil have to be
finished with in the timout seconds.//This is not correct
Timeout logick. The timeout should be the time between each TCP
packet, not the whole transfer.

timeout = DateTime.Now.AddSeconds(this.timeoutSeconds);

StopTimer();
}

-------------------------------------------------------------------------------
Here is my FtpTimeout event where I am throwing the exception:

void onTimerElapsed(object state)
{
StopTimer();
throw new Exception("FTP TIMEOUTED");
}

Exception gets thrown fine, but I just can't catch it the exception
messagebox keeps popping up.

Hope somebody can tell me what am doing wrong? The reason why I
haven't done this with events is that am not sure how do that.

Btw, thanks for the replay Sergey =)

Please help, anybody!

Best Regards
Zlatan
 
Back
Top