This is our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

I am trying to launch a dialog window from an add_button API call. However, the function assigned to the new_dialog executes before I click OK and the code runs through to the function and fails to refresh.

   local function reloadAPList()
      -- Request new filename from user
      new_dialog("Enter File (without extension)",
                 function (basename) filename = basename end,
                 "Filename")

      message("Opening file: " .. filename)
      local csv_file = assert(io.open(filename, "r"))

      if csv_file==nil then
          warn(err)
          return
      end

      -- Read CSV line
      local line = csv_file:read()

      -- Flush out old table contents
      for k,v in pairs(apList) do
         apList[k] = nil
      end

      -- Build AP Object Name List table
      apList = fromCSV(line)
   end

   aw:add_button("Reload", reloadAPList )

If I click the reload button again I can see that it attempts to open the file matching the string I passed in on the previous attempt.

Any ideas why it does not take the filename I pass in the first time round?

asked 15 May '15, 17:34

carlwain74's gravatar image

carlwain74
1334
accept rate: 0%

I guess, we would need more of your code to recreate the problem.

(17 May '15, 06:14) Kurt Knochner ♦

Essentially all you need to do is invoke a new dialog from within a button function. You can see above that aw:add_button calls reloadApList when the button is clicked. In reloadApList I call new_dialog with an inline function to assign the string to a local variable (not shown in code snippet).

The dialog box launches, but the message call after the new_dialog prints the filename from the previous button click.

I hope that helps..

(17 May '15, 20:55) carlwain74

can you please test the following code and post the output of message() here?

   local function reloadAPList()

   local filename = "_notset_"

   local function assign_filename(basename)
      message("Filename before:" .. filename)
      filename = basename
      message("Filename after:" .. filename)
   end

      -- Request new filename from user
      new_dialog("Enter File (without extension)", assign_filename, "Filename")

      message("Opening file: " .. filename)
(17 May '15, 21:42) Kurt Knochner ♦

This is the output in the console

5/18/2015 8:16:05 AM MESSAGE: Opening file: c:\temp\AP_Batch.csv
5/18/2015 8:16:34 AM MESSAGE: Filename before:c:\temp\AP_Batch.csv
5/18/2015 8:16:34 AM MESSAGE: Filename after:test.csv

The first log entry appeared when I clicked the button. The second and third appeared after I entered a string and clicked OK.

Note that test.csv does not exist and I was expecting the assert on io.open to detect that.

(18 May '15, 08:19) carlwain74

The assert came when I clicked the button for a second time. It looks like the code after the new_dialog executes before the dialog is closed.

(18 May '15, 08:30) carlwain74

strange thing... please file a bug report at https://bugs.wireshark.org

(18 May '15, 08:46) Kurt Knochner ♦
showing 5 of 6 show 1 more comments

I don't think this is a bug. The new_dialog() function is non-blocking, creating a non-modal dialog window. So in the question's script it's creating the dialog window but immediately returning to process more of the Lua code, as it should.

I think the right way to code this in Lua is to put the rest of the logic that is currently after new_dialog() into the callback function assigned by new_dialog(). In other words, this:

local function reloadAPList(basename)
    filename = basename

    message("Opening file: " .. filename)
    local csv_file = assert(io.open(filename, "r"))

    if csv_file==nil then
        warn(err)
        return
    end

    -- Read CSV line
    local line = csv_file:read()

    -- Flush out old table contents
    for k,v in pairs(apList) do
        apList[k] = nil
    end

    -- Build AP Object Name List table
    apList = fromCSV(line)
end

local function get_filename()
    -- Request new filename from user
    new_dialog("Enter File (without extension)",
               reloadAPList,
               "Filename")
end

aw:add_button("Reload", get_filename )
permanent link

answered 29 Jun '15, 16:16

Hadriel's gravatar image

Hadriel
2.7k2939
accept rate: 18%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×1,620
×431

question asked: 15 May '15, 17:34

question was seen: 1,355 times

last updated: 29 Jun '15, 16:16

p​o​w​e​r​e​d by O​S​Q​A