Click here to Skip to main content
15,888,968 members
Home / Discussions / C#
   

C#

 
GeneralRe: DaraReader problem ... Pin
PIEBALDconsult6-Jul-14 4:29
mvePIEBALDconsult6-Jul-14 4:29 
GeneralRe: DaraReader problem ... Pin
nassimnastaran6-Jul-14 7:51
nassimnastaran6-Jul-14 7:51 
QuestionMood(emotion) detection in real time Pin
JanaRMS5-Jul-14 6:29
JanaRMS5-Jul-14 6:29 
AnswerRe: Mood(emotion) detection in real time Pin
Pete O'Hanlon5-Jul-14 11:55
mvePete O'Hanlon5-Jul-14 11:55 
QuestionReactiveCommand CanExecute is not updating button Pin
J. Holzer5-Jul-14 1:22
J. Holzer5-Jul-14 1:22 
Questionreferenced parameter Pin
Gilbert Consellado5-Jul-14 0:24
professionalGilbert Consellado5-Jul-14 0:24 
AnswerRe: referenced parameter Pin
OriginalGriff5-Jul-14 0:38
mveOriginalGriff5-Jul-14 0:38 
GeneralRe: referenced parameter Pin
Gilbert Consellado5-Jul-14 1:04
professionalGilbert Consellado5-Jul-14 1:04 
sorry for that, i will include now the real code

C#
public abstract class DBSynchronizer<T> : IDBSynchronizer
        where T : GCSC.Interface.IEditableObject
    {
        protected EditableObjectEventHandler onEditableObjectRaised;
        protected EditableList<T> referencedItem;
        protected bool hasNewItems, hasModifiedItems, hasRemovedItems;
        protected List<int> idsNew, idsOld;
        protected EditableList<T> tempSourceType;
        protected StringBuilder tempSB;
        protected MySQLSelect tempMySQLSelect;

        Timer Ticker;
        int track, defaultIterationBeforeSleep, cycle;
        bool isTickerStarted;
        int kickStarter;

        public DBSynchronizer(ref EditableList<T> itemCollection)
        {
            referencedItem = itemCollection;
            IsInitialized = referencedItem.Count < 1 ? false : true;
            IterationBeforeSleep = 5;
            MaxSleepTime = 60000;
            Interval = 1000;
        }

        protected virtual void CollectNewItems(string query)
        {
            tempMySQLSelect = new MySQLSelect(query);
            if (tempMySQLSelect.RowCount < 1)
            {
                hasNewItems = false;
                return;
            }
            hasNewItems = true;
            tempSourceType = new EditableList<T>(tempMySQLSelect.Reader());
            tempMySQLSelect.Clear();
            foreach (var ni in tempSourceType)
                referencedItem.Add(ni);
            tempSourceType = null;
        }

        public virtual void ForceCollectNewItem()
        {
            CollectNewItems(NewItemQuery);
        }

        protected abstract void CollectModifiedItems();
        protected abstract void CollectRemovedItems();
        public abstract void NotifyAllSubscriber();
        private void Ticker_Tick(object sender, EventArgs e)
        {
            if (referencedItem.Count > 0)
            {
                CollectRemovedItems();
                CollectModifiedItems();
            }
            CollectNewItems(NewItemQuery);
            //some long line of code for timing
        }

        protected virtual void Initialize()
        {
            if (isTickerStarted)
                return;
            defaultIterationBeforeSleep = IterationBeforeSleep;
            IsInitialized = true;
            isTickerStarted = true;
            Ticker = new Timer() { Interval = Interval, Enabled = true };
            Ticker.Tick += Ticker_Tick;
            Ticker.Start();
        }

        public virtual void ForceAwake()
        {
            if (!isTickerStarted)
                return;
            if (Ticker != null)
                if (Ticker.Interval == Interval)
                    return;
            Ticker.Dispose();
            Ticker = new Timer() { Interval = Interval, Enabled = true };
            Ticker.Tick += Ticker_Tick;
            Ticker.Start();
        }

        public virtual void Shutdown()
        {
            if (Ticker == null) return;
            Ticker.Dispose();
        }

        protected virtual int Timing()
        {
            if (MaxSleepTime < 1000) throw new ArgumentException("MaxSleepTime must not be less than or equal 1000", "MaxSleepTime");
            return new RandomNumber(1000, MaxSleepTime).GetRandomNumber();
        }

        public int IterationBeforeSleep { get; set; }
        public bool IsInitialized { get; protected set; }
        public bool IsSleep { get; protected set; }
        public int Interval { get; set; }
        public int ForceShutdownAt { get; set; }
        public int MaxSleepTime { get; set; }
        protected abstract string NewItemQuery { get; }
        public event EditableObjectEventHandler EditableObjectRaised
        {
            add
            {
                Initialize();
                onEditableObjectRaised += value;
            }
            remove { onEditableObjectRaised -= value; }
        }
    }



Then it was inhereted here


C#
internal sealed class SynchronizePersonClass : DBSynchronizer<PersonsClass>
    {
        static readonly object _locker = new object();

        public SynchronizePersonClass(ref EditableList<PersonsClass> itemCollection)
            : base(ref itemCollection) { }

        protected override void CollectModifiedItems()
        {
            //Some Process working fine, long lines
        }

        protected override void CollectRemovedItems()
        {
            //Some Process working fine, long lines
        }

        protected override void Initialize()
        {
            lock (_locker)
            {
                if (!IsInitialized)
                    ProcessClass<PersonsClass>.PopulateCollection(ref ClassCollections.PersonClassColletction,
                        ReadOnlyStrings.TBL_PERSON + " WHERE `is_deleted` = 0 ORDER BY `registration_date` DESC");
                base.Initialize();
            }
        }

        public override void NotifyAllSubscriber()
        {
            if (onEditableObjectRaised == null) return;
            onEditableObjectRaised(this, new EditableObjectEventArgs(referencedItem));
            referencedItem.Finalized();
        }

        protected override string NewItemQuery
        {
            get
            {
                return ReadOnlyStrings.TBL_PERSON + " WHERE `is_deleted` = 0 AND `tbl_person_id` NOT IN(" +
                    getIDCollectionString() + ") ORDER BY `registration_date` DESC";
            }
        }

        private string getIDCollectionString()
        {
            if (referencedItem.Count < 1) return "0";
            tempSB = new StringBuilder(referencedItem.Count * 2);
            foreach (var ri in referencedItem)
                tempSB.Append(ri.PersonID + ", ");
            return tempSB.Remove(tempSB.Length - 2, 2).ToString();
        }
    }



then create a static object of it

C#
internal class Synchronizers
    {
        public static SynchronizePersonClass PersonClassSynchronizer;
    }


the initializing the bindinglist

C#
            ProcessClass<PersonsClass>.PopulateCollection(ref ClassCollections.PersonClassColletction, query);
// in this line if the db is not empty it will populate an items else the binding has (0)zero items on it.



then initial the static synchronizer with the bindinglist from above

C#
Synchronizers.PersonClassSynchronizer = new SynchronizePersonClass(ref ClassCollections.PersonClassColletction);
            Synchronizers.PersonClassSynchronizer.IterationBeforeSleep = 15;
            Synchronizers.PersonClassSynchronizer.MaxSleepTime = 10000;


now my big problem that i dont understand is if the ClassCollections.PersonClassCollection has no items on it, then if the synchronizer found new record it doesnt update the ClassCollection.PersonClass then i tried to debug it, the varible "referencedItem" has some items.

but if the classCollection.PersonClass has an item even just 1 before it was passed to the synchronizer there is no problem about it, any moodification in referencedItem will be reflected to ClassCollection.PersonClass and vice versa.

What your thought about that.


Sorry for my bad english and long post.

Thank you
Gilbert
GeneralRe: referenced parameter Pin
harold aptroot5-Jul-14 0:56
harold aptroot5-Jul-14 0:56 
GeneralRe: referenced parameter Pin
Gilbert Consellado5-Jul-14 1:07
professionalGilbert Consellado5-Jul-14 1:07 
GeneralRe: referenced parameter Pin
sankarsan parida6-Jul-14 7:52
professionalsankarsan parida6-Jul-14 7:52 
GeneralRe: referenced parameter Pin
Gilbert Consellado9-Jul-14 23:32
professionalGilbert Consellado9-Jul-14 23:32 
QuestionMessage Closed Pin
4-Jul-14 20:18
Mehtech4-Jul-14 20:18 
QuestionMessage Closed Pin
4-Jul-14 20:16
Mehtech4-Jul-14 20:16 
Questionc#, VSTO, Template Project, opening Document without error messge? Pin
Kristian Holm4-Jul-14 8:19
Kristian Holm4-Jul-14 8:19 
QuestionHow to Connect Marquee with database Pin
Member 102260014-Jul-14 7:07
Member 102260014-Jul-14 7:07 
AnswerRe: How to Connect Marquee with database Pin
Richard MacCutchan4-Jul-14 7:23
mveRichard MacCutchan4-Jul-14 7:23 
GeneralRe: How to Connect Marquee with database Pin
Member 102260014-Jul-14 8:13
Member 102260014-Jul-14 8:13 
SuggestionRe: How to Connect Marquee with database Pin
Richard MacCutchan4-Jul-14 20:04
mveRichard MacCutchan4-Jul-14 20:04 
Questionresponsive GUI text change / database instant search Pin
Member 817404-Jul-14 5:17
Member 817404-Jul-14 5:17 
AnswerRe: responsive GUI text change / database instant search Pin
Dave Kreskowiak4-Jul-14 5:31
mveDave Kreskowiak4-Jul-14 5:31 
AnswerRe: responsive GUI text change / database instant search Pin
OriginalGriff4-Jul-14 5:31
mveOriginalGriff4-Jul-14 5:31 
AnswerRe: responsive GUI text change / database instant search Pin
Richard Deeming4-Jul-14 6:13
mveRichard Deeming4-Jul-14 6:13 
Answerfeedback: responsive GUI text change / database instant search Pin
Member 817404-Jul-14 8:27
Member 817404-Jul-14 8:27 
QuestionMessage Closed Pin
4-Jul-14 0:51
Mehtech4-Jul-14 0:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.