i have written a code in vb.net. Its design contains buttons to execute various steps. Now i want my code to execute automatically run when I debug without pressing any buttons :(

Dani AI

Generated

Thread summary and practical next steps based on the replies from , and .

Moving UI-triggered code into a reusable method or class (what ended up doing) is the right design: it makes the work callable from a timer, from a startup event, or from automated tests (as suggested) without duplicating logic. If the work must run once when the program appears on screen, prefer the Form.Shown event over Form.Load when you need the UI fully created. For headless runs (unit tests, console runner) call the same core method from a test or a different entry point so the code is testable and decoupled from controls.

Timer and threading practical notes

  • For GUI periodic tasks that update controls, use System.Windows.Forms.Timer (it ticks on the UI thread). For background-only work, prefer System.Timers.Timer or System.Threading.Timer so the work runs on thread-pool threads. See the docs: System.Windows.Forms.Timer and System.Timers.Timer.
  • Avoid blocking the UI thread. If the work is long-running, offload it with Task.Run and marshal UI updates back with Control.Invoke/BeginInvoke. See Control.Invoke.
  • Prevent reentrancy: stop or disable the timer before starting work and restart it when the work finishes. Dispose timers on FormClosing. Use CancellationTokenSource to cancel mid-work if needed.

Example patterns (summary):

  • Use a single shared method that performs the task.
  • Timer handler disables the timer, starts the work on a background task, and invokes back to the UI to re-enable or update controls.
  • Use an interval of 300000 ms for five minutes and guard against overlapping runs.

Troubleshooting checklist: verify timer interval units (ms), ensure UI updates are invoked from the UI thread, handle exceptions inside background tasks, and dispose or stop timers when closing the form.

Recommended Answers

All 3 Replies

I'm not sure what you mean, but you can write a unit test method that runs the event handlers for each of the buttons as if you clicked on them. That automates a sequence of button clicks...

Member Avatar for Member #46692

Can't you just have put the code on form load instead of button_clicked then?

Thank you for replying guys but I have solved the problem. I inserted my code, which I first inserted in the buttons, in a function and then I called it in a timer, as I also wanted to call it at the interval of 5mins

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.