在page_load之后执行代码 [英] Execeute code after page_load

查看:120
本文介绍了在page_load之后执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有条形码扫描功能。

我想在page_load之后执行它。



请告诉我怎么做。



谢谢

Hi all

I have a function for barcode scanning.
I want that to be executed after page_load.

Please tell me how to do this.

Thank you

推荐答案

定时器被设计为重复,所以你的扫描方法将被重复调用,除非你通过调用它的Stop方法来禁用定时器 - 你可以随时调用Start来重启它。



但是......如果你只想在表单加载后进行一次性操作,我根本就不会使用Timer - 表单有一个Shown事件,只调用一次和一次当它首先实际显示的表单,以及表单控件被绘制后。这听起来更像你需要的。





但它同时像装载一样。

表示,它没有显示表单,但函数正在执行。


在这种情况下,将代码移动到后台工作者:

Timers are designed to repeat, so your "scan" method will be called repeatedly, unless you disable the timer by calling it's Stop method - you can call Start at any time to restart it.

But...if you only want a one shot operation after the form has loaded, I wouldn't use a Timer at all - forms have a Shown event which is called once and once only when the form it first actually displayed, and once the form controls have been painted. That sounds more like what you need.


"But it is at the same time like load.
means, it's not showing the form but the function is getting executed."

In that case, move the code into a background worker:
private void PassbookInsert_Shown(object sender, EventArgs e)
    {
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += new DoWorkEventHandler(worker_DoWork);
    work.RunWorkerAsync();
    }
private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker worker = sender as BackgroundWorker;
    if (worker != null)
        {
        scan();
        }
    }


也许你的scan()方法阻塞你的主线程?如果是这样,在该方法完成执行之前,您将看不到该表单。在这种情况下,你将不得不使用线程或后台工作程序来执行它。



编辑

你必须做这样的事情:



- 在你的form_load或Form_shown中:

Maybe your scan() method is blocking your main thread? If so,you won't see the form until that method finish executing. In that case,you'll have to use a Thread or Background Worker to execute it.

EDIT
You'll have to do something like this:

- In your form_load or Form_shown:
BackgroundWorker m_oWorker = new BackgroundWorker();
m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
m_oWorker.RunWorkerAsync();





- 添加此方法:





- Add this Method:

void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
  scan();
}





希望有帮助



Hope that helps


这篇关于在page_load之后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆