-4

I am working on a project which includes two main processing, I have tried using timer_tick to handle the processing which makes the application really slow. The application needs to be running at all times, I wanted the timer aspect of the timer_tick to trigger the methods every X seconds but with the multiple threading, as this makes it a lot faster.

Can anyone help?

The current structure of the application is below:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        setting_info();
    }

    public void setting_info()
    {
        // takes data from config file for API connections
    }

    private void swis()
    {
        // connects to API and fetches data
        // need to be continuously running - Ideally Interval(1200)
    }

    private void st_processing()
    {
        // processes the data that was fetched in swis() slows down the program without multiple threading
        // need to be continuously running - Ideally Interval(800)
    }

    private void alert_in_timer_Tick(object sender, EventArgs e)
    {            
        while (alert_in == true)
        {
            swis();
            break;
        }
    }

     private void st_processing_timer_Tick(object sender, EventArgs e)
    {
        while (st_processing == true && alert_data_database_has_data == true)
        {
            st_processing();
            //check_error_count();
            alert_data_database_has_data = false;
            break;
        }
    }
}
  • How do you know threading makes it faster if you haven't tried? Are you just asking how to start a thread function or how to get it to talk to a GUI? – doctorlove Jul 29 '13 at 09:45
  • Fair comment about the speed. I have used something like this ThreadStart threadstartswis = new ThreadStart(swis); Thread thread = new Thread(threadstartswis); thread.Start(); which I believe starts the thread, however I am having trouble repeating the thread like a timer_tick say every second. – user2561466 Jul 29 '13 at 09:54
  • Can you change the question to show a smaller example including what you have tried, and tell us what went wrong you had "trouble repeating the thread"? – doctorlove Jul 29 '13 at 09:58

1 Answers1

0

It's not clear what you want to know. There are many good threading tutorials for C# as discussed here for example this one

Starting a thread is easy

Thread worker = new Thread(new ThreadStart(swis));
//...
worker.join();

You will need to be careful if you need to update the GUI as a result of what the thread does. Previously discussed here.

At the moment you break from the alert_in_timer_Tick once it's called the swis function.

while (alert_in == true)
{
    swis();
    break;// why is this here?
}
Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • In swis a query is called, once a match is correct it breaks, I will change the question on provide more detail. Thanks. – user2561466 Jul 29 '13 at 10:02