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

Dialog function execution before OK is pressed

0

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(&quot;Opening file: &quot; .. filename)</code></pre></div><div id="comment-42479-info" class="comment-info"><span class="comment-age">(17 May '15, 21:42)</span> <span class="comment-user userinfo">Kurt Knochner ♦</span></div></div><span id="42501"></span><div id="comment-42501" class="comment"><div id="post-42501-score" class="comment-score"></div><div class="comment-text"><p>This is the output in the console</p><pre><code>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


One Answer:

0

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(&quot;Opening file: &quot; .. filename)
local csv_file = assert(io.open(filename, &quot;r&quot;))

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 )

answered 29 Jun ‘15, 16:16

Hadriel's gravatar image

Hadriel
2.7k2939
accept rate: 18%