# How do you execute code at startup?

**URL:** <https://www.ruby-forum.com/t/how-do-you-execute-code-at-startup/244668>\
**Category:** Ruby-Gnome 2\
**Created:** [May 24, 2016, 4:07am UTC](https://www.ruby-forum.com/t/how-do-you-execute-code-at-startup/244668 "2016-05-24T04:07:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [May 24, 2016, 4:07am UTC](https://www.ruby-forum.com/t/how-do-you-execute-code-at-startup/244668/1 "2016-05-24T04:07:30Z")

</div>

I have a Gtk::TextView in my program, and I’m trying to make the cursor  
in the textview move to line 30, then scroll the textview so the cursor  
is visible on the screen. Sounds simple?

The method to move the cursor and scroll the textview works perfectly.  
The problem is that I can’t get it to run at startup time.

When I try to move the cursor before gtk.main is called, it sets the  
cursor to the proper position, but the screen doesn’t scroll to show the  
cursor.

The scrolling of the textview is animated so it must be done in the gtk  
main loop. That means it must be called AFTER gtk.main. But this  
doesn’t work:

gtk.main  
move\_cursor(3) #never gets here

Gtk.main is blocking.

So once gtk.main is called I need to execute this code one at startup  
when everything is shown in the window, so it can animate. is there an  
event that happens once that signals the start of the loop?

is there an event called start\_of\_processing\_events?

or start\_event\_handling\_to\_help\_eric?

This issue is a lot harder than it looks.

Eric

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [May 24, 2016, 3:55pm UTC](https://www.ruby-forum.com/t/how-do-you-execute-code-at-startup/244668/2 "2016-05-24T15:55:32Z")

</div>

Hi Eric,

Have you tried to use both move\_cursor and a method like scroll\_to\_iter  
before Gtk.main,

> **[Gtk-3.0](https://docs.gtk.org/gtk3/)**
>
> Reference for Gtk-3.0

(it should be Gtk::TextView#scroll\_to\_iter). I have not tested this.

It would be very helpfull if you could provide a simple working example  
that illustrate your problem.

cedlemo
