1

ピップスさん、こんにちは。

ボタンを使用してバッチファイルを実行する Tkx gui があります。バッチ ファイルは別のスレッドで実行されます。これは、GUI を引き続き使用できるようにするためです。バッチファイルの実行をキャンセルするキャンセルボタンを実装したい。

Kill シグナルを送信しようとしましたが、スレッドのみが終了し、バッチ ファイルは終了しません。以下は、実行サブルーチンとキャンセル サブルーチンのコードです。

ああ、バッチファイルを編集することはできません。

my $t1;
sub runbutton{
    $bar->g_grid();
    $bar->start();
    $t1 = threads->create(sub { 
        local $SIG{'KILL'} = sub { threads->exit };
        system("timer.bat"); 
        
        });
    
    $t1->set_thread_exit_only(1);
    my $start = time;
    my $end = time;
    while ($t1->is_running()) { 
        $end = time();
        $mytext = sprintf("%.2f\n", $end - $start);
        Tkx::update(); 
   }
   
    $bar->stop();
    $bar->g_grid_forget();
    $b4->g_grid_forget();
}

sub cancelbutton
{
    $t1->kill('KILL')->detach();
}
4

2 に答える 2

0

Perl スレッドは、kill シグナルが実行される前に、システムが戻るのを待っているのではないかと思われます。

Win32::Process を使用してバッチ ファイルを別のプロセスとして開始し、プロセスを終了する必要があることを示す変数を用意することをお勧めします。変数が設定されると、スレッドはプロセスを強制終了してから終了できます。

以下は、Win::Process を使用して、アクティブ ステート Perl バージョン 5.16.1 を使用して別のプロセスとしてバッチ ファイルを作成および強制終了するために使用した小さなテスト ケースです。

use strict;

# Modules needed for items
use Win32::Process;
use Win32;

# Subroutine to format the last error message which occurred
sub ErrorReport
{
    print Win32::FormatMessage( Win32::GetLastError() );
}

print "Starting Process\n";

# Declare a scalar for the process object
my $ProcessObj;

# Create the process to run the batch file
Win32::Process::Create($ProcessObj,
    "C:\\Users\\Glenn\\temp.bat",
    "C:\\Users\\Glenn\\temp.bat",0,
    NORMAL_PROIRITY_CLASS,
    ".") || ErrorReport();

print "Process Started\n";

# Sleep for a few seconds to let items start running
sleep(2);

# Kill the process with a -9
# $ProcessObj->Kill(0) does not seem to work to stop the 
# process.  Kill will kill the process with the process id 
kill -9,$ProcessObj->GetProcessID();

# Done with efforts
print "Complete\n";

Windows 7 を使用している場合、管理者として実行してプロセスを作成できるようにする必要があります。そうしないと、プロセスを作成しようとすると、アクセスが拒否されましたというメッセージが表示されます。

于 2014-05-09T13:23:39.460 に答える