Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Sinisa Hajnal 3-Nov-14 2:15am    
What have you tried? You say Page_Load, I'm assuming asp.net?
BillWoodruff 3-Nov-14 2:17am    
As Sinisa asked you, please identify what your dev stack is: Win Forms ? ASP ?
Black_Rose 3-Nov-14 2:22am    
Sorry for that.it's a windows application.
and i tried to put one timer.i gave my scan() in timer_tick event but problem is after scanning also it is going back to the scan() again and again.so it's throwing error.
DamithSL 3-Nov-14 2:32am    
update the question with your code
BillWoodruff 3-Nov-14 2:46am    
"windows application" doesn't tell us whether it's ASP.NET or WinForms, etc.

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:
C#
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();
        }
    }
 
Share this answer
 
v2
Comments
BillWoodruff 3-Nov-14 3:09am    
But, we don't know if this is WinForms ... yet (although, g-0-d knows we tried ... to know).
OriginalGriff 3-Nov-14 3:14am    
He did say: "Sorry for that.it's a windows application."
Which eliminates web stuff, pretty much - as does the barcode requirement.
And if you look at his other questions they are WinForms related again.

So the balance of probabilities is that this is WinForms, and he doesn't know anything else exists yet.
BillWoodruff 3-Nov-14 4:01am    
A side-effect of frequenting CP QA is: I just assume that posts can mean anything, and ... with genetic engineering and quantum computing ... that anything can be made to work with anything ... these days ? :)
OriginalGriff 3-Nov-14 5:46am    
It's probably quantum: add another zero!
Black_Rose 3-Nov-14 3:19am    
Thanks for your reply griff..
I tried the shown event
private void PassbookInsert_Shown(object sender, EventArgs e)
{
scan();
}
But it is at the same time like load.
means, it's not showing the form but the function is getting executed.
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:
C#
BackgroundWorker m_oWorker = new BackgroundWorker();
m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
m_oWorker.RunWorkerAsync();


- Add this Method:

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


Hope that helps
 
Share this answer
 
v2
Comments
Black_Rose 3-Nov-14 3:42am    
can you give me some example for this.
Pikoh 3-Nov-14 3:51am    
See updated solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900