168 lines
5.0 KiB
C#

/*********************************************************************************************
* Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
* Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
* ------------------------------------------------------------------------------
* $Workfile: VlnSpinner.cs $ $Revision: 2 $
* $Author: Kathy $ $Date: 7/26/04 1:21p $
*
* $History: VlnSpinner.cs $
*
* ***************** Version 2 *****************
* User: Kathy Date: 7/26/04 Time: 1:21p
* Updated in $/LibSource/VlnStatus
* add comment
*
* ***************** Version 1 *****************
* User: Kathy Date: 5/11/04 Time: 9:56a
* Created in $/LibSource/VlnStatus
* volian spinner control
*********************************************************************************************/
using System;
using System.Diagnostics;
using System.Text;
namespace VlnStatus
{
/*
** The following values are taken by the Spinner class constructor:
**
** spindelay - is the number of seconds to wait before putting
** up the message and spinner.
**
** spinfreq - is the minimum delay time in milliseconds before
** returning to the caller.
**
** spinmsg1 - is a caller message put up with the spinner
** spinmsg2 - is a second caller message concatenated to spinmsg1
** both spinmsg1 and spinmsg2 may be NULL for no messages.
**
** AbortFlag - is a flag. If non-zero takes a DB_Exit(255)
** if the user hits the Esc key while spinning.
** can be defaulted.
**
**
** SpinnerWait puts up a delayed message with a spinner.
**
** AutoTermFlag - spinner returns 0 if this value is non-zero
** or Esc was pressed and AbortFlag not set.
** A 1 is returned if AutoTermFlag is zero.
**
** The purpose of this function is to generate interruptable/abortable
** while or do-while loops in code which is waiting for an event to
** occur. Below is a simple example:
**
** { Spinner spin(2,25,"- Waiting for ", filename, 1);
** while( spin.SpinnerWait( stat(filename,&temp) || temp.st_size ) {;}
** }
**
** This loop returns zero if filename does not exist, or if it does
** exist and has a non-zero length, otherwise it waits at least
** 25 milliseconds, and returns 1 so that the predicate test
** stat(filename,&temp) || temp.st_size
** is tested again. If the loop continues for 2 seconds, a message
** and spinner is posted. If the user presses the Esc key, DB_Exit(255)
** is taken.
**
** Note, that the SpinnerWait function will close the message window
** when the Spinner class object goes out of scope, as the desctructor
** handles the window closures. Note also, that a new instance of the
** object must be generated with each use.
*/
public class VlnSpinner
{
SpinnerFrm spinFrm;
private int spinstart;
private int spindelay;
private int spinfreq;
private string spinmsg1;
private string spinmsg2;
private bool abortflag;
private bool canCancel;
private string msgprefix;
private bool firstspin;
private bool firstpass;
public VlnSpinner(int sd, int sf, string sm1, string sm2, bool af, bool prefix, bool showCancel)
{
spindelay=sd;
spinfreq=sf;
spinmsg1=sm1;
spinmsg2=sm2;
abortflag=af;
canCancel = showCancel;
firstpass=true;
firstspin=true;
spinFrm = null;
if(prefix)
msgprefix="Database File in Use by another user \n";
else
msgprefix=" ";
}
public void Dispose()
{
if(spinFrm!=null) spinFrm.Dispose();
}
public bool SpinnerWait(bool AutoTermFlag)
{
if( !AutoTermFlag )
{
if( firstpass )
{
// Tick is equal to 100 nanoseconds.
// nanosecond is 1 billionth of a second.
spinstart = (int)(DateTime.Now.Ticks/10000000) + spindelay;
firstpass = false;
}
if( !firstspin )
{
SpinnerTick();
TickTock(spinfreq);
}
else
{
if(spinstart <= (int)(DateTime.Now.Ticks/10000000)) SpinnerTick();
TickTock(spinfreq);
}
}
return (!AutoTermFlag);
}
private void TickTock(int spinfreq)
{
long cur = DateTime.Now.Ticks;
long getto = cur+(spinfreq*100);
while (cur <= getto)
cur = DateTime.Now.Ticks;
}
private void SpinMessage(string msg1,string msg2,string msg3,int len)
{
StringBuilder buff = new StringBuilder(80);
buff.Append(msg1);
if (msg2!=null)buff.Append(msg2);
if (msg3!=null)buff.Append(msg3);
spinFrm.UpdateSpinMsg(buff.ToString());
}
private void SpinnerTick()
{
int windowcol;
if(firstspin)
{
windowcol=msgprefix.Length+2;
if(spinmsg1!=null) windowcol+=spinmsg1.Length;
if(spinmsg2!=null) windowcol+=spinmsg2.Length;
spinFrm = new SpinnerFrm(abortflag, canCancel);
spinFrm.Show();
SpinMessage(msgprefix,spinmsg1,spinmsg2,windowcol+1);
firstspin = false;
}
spinFrm.UpdateSpin();
}
}
}