1

我很乐意为我的基于Windows的桌面应用程序提供一个 Web 界面,反之亦然。我的桌面应用程序是用 wxRuby 编写的,网络服务器是 Sinatra(使用 webrick)。最简单的想法就是把它们混在一起,这是行不通的。

此代码不起作用。网络服务器和 gui 应用程序不会同时运行。桌面应用程序先运行,后关闭;辛纳屈开始。

require 'wx'
require 'sinatra'
configure do set :server, 'webrick' end

get '/' do
  "Sinatra says hello"
end

class MyApp < Wx::App
  def on_init
    @frame = Wx::Frame.new( nil, -1, "Application" )
    @frame.show
  end
end

app = MyApp.new
app.main_loop

所以我想把最后两行改成

Thread.new do
    app = MyApp.new
    app.main_loop
end

再次。桌面应用程序运行直到关闭,然后网络服务器启动。所以我尝试在一个线程中启动 Sinatra。

Thread.new do
    require 'sinatra'
    configure do set :server, 'webrick' end

    get '/' do
        "Sinatra says hello"
    end
end

require 'wx'

class MyApp < Wx::App
    def on_init
        @frame = Wx::Frame.new( nil, -1, "Application" )
        @frame.show
    end
end

app = MyApp.new
app.main_loop

再次。桌面应用程序运行直到关闭,然后网络服务器启动。

请提出建议,但请记住,我真的很想只有一个过程。 如果您的解决方案是两个过程;我想要不需要轮询的强大的进程间通信。

谢谢!杰夫

4

2 回答 2

2

这至少启动了,不确定这是否违反了一些线程规则。

require 'win32/process'
require 'sinatra/base'

class MyWebServer < Sinatra::Base
  get '/' do
    'Hello world!'
  end
end

Thread.new do
  MyWebServer.run! :host => 'localhost', :port => 4567
end

require 'wx'

class MyGui < Wx::App
    def on_init
        t = Wx::Timer.new(self, 55)
        evt_timer(55) { Thread.pass }
        t.start(1)
        evt_idle { Thread.pass }
        @frame = Wx::Frame.new( nil, -1, "Application" )
        @frame.show
        true
    end
end

app = MyGui.new
app.main_loop
于 2010-01-19T05:17:52.817 回答
0

你可以使用Bowline,但我还没有使用它。

于 2011-02-28T17:22:13.520 回答