0

I have a problem. I have a app which takes data from database and generate this data into txt file. On the beginning I create button for generate this data into txt file But now I want to this app generate this data in txt file for example every 5 min and overide previous file without button. Here is my code:

public partial class MainWindow : Window
{
    private static Timer aTimer;

    private List<tw__Towar> someData;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Generate()
    {

        string path = @"c:\some path";


        var createFile = someData.Select(k => $"{k.tw_Id}\t" + $"{k.column}\t" + $"{k.column2}\t" + $"{k.column3}\t"
          );
        File.WriteAllLines(path, createFile);
        Process.Start(path);

        System.Timers.Timer aTimer = new System.Timers.Timer();
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = some time;
        aTimer.Enabled = true;
    }

    private void OnTimedEvent(object sender, ElapsedEventArgs e)
    {
        Generate();
        Console.WriteLine("It works");
    }

    private void LoadData()
    {

        DataClasses1DataContext cd = new DataClasses1DataContext();
        someData = (from p in cd.datavbase where p.column != null && p.column != ""
                     select p).ToList();
        GT1.ItemsSource = someData;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        LoadData();
    }

    //private void Button_Click_1(object sender, RoutedEventArgs e)
    //{
    //    Generate();
    //}
}
manhart
  • 19
  • 5

1 Answers1

1

This is how i would expect your logic to work :

(keep in mind that the timer needs to elapse before it writes the file, so the first file is generated 5 minutes after the window opens)

public partial class MainWindow : Window
{
    //the timer doesn't need to be static
    //and since we are going to update the UI when the timer elapses,
    //(in LoadData)
    //we should use a dispatchertimer

    private DispatcherTimer aTimer;

    private List<tw__Towar> someData;

    public MainWindow()
    {
        InitializeComponent();

        //set up the timer straight away
        aTimer = new DispatcherTimer();
        aTimer.Tick += new EventHandler(OnTimedEvent);
        aTimer.Interval = TimeSpan.FromSeconds(300); //300 seconds = 5 minutes
        aTimer.Start();
    }

    private void Generate()
    {
        LoadData();

        string path = @"c:\some path";

        var createFile = someData.Select(k => $"{k.tw_Id}\t" + $"{k.column}\t" + $"{k.column2}\t" + $"{k.column3}\t"
          );
        File.WriteAllLines(path, createFile);
        Process.Start(path);
    }

    private void OnTimedEvent(object sender, EventArgs e)
    {
        Generate();
        Console.WriteLine("It works");
    }

    private void LoadData()
    {
        DataClasses1DataContext cd = new DataClasses1DataContext();
        someData = (from p in cd.datavbase where p.column != null && p.column != "" select p).ToList();
        GT1.ItemsSource = someData;

    }
}

for more info on the DispatcherTimer, see https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

and for the reason why we need a DispatcherTimer, see Refresh UI with a Timer in WPF (with BackgroundWorker?)

Also, Jon Skeet said it, so it must be true.

Timothy Groote
  • 8,614
  • 26
  • 52
  • 1
    I've got error like this: and I don't know what to do everything I think is fine : In line aTimer.Tick += new ElapsedEventHandler(OnTimedEvent); there is error: Cannot implicity convert type 'System.Timers.ElapsedEventHandler' to 'System.EventHandler' – manhart Nov 10 '17 at 09:04
  • 1
    @manhart if this helped you then make sure to accept the answer and reward this person for their time. – XAMlMAX Nov 10 '17 at 09:57
  • @manhart i fixed the bug in the example. sorry, missed that the first time around. – Timothy Groote Nov 10 '17 at 11:56
  • Don't worry i managed to solve this problem :) but thanks for that – manhart Nov 10 '17 at 13:30
  • Also I have a question about this app. Is this difficult to convert this app created in wpf to windows service ? – manhart Nov 12 '17 at 13:47
  • @manhart not at all, you'd get rid of the UI stuff and use an ordinary timer rather than a `DispatcherTimer`, et voilá. – Timothy Groote Nov 13 '17 at 08:00