Thread Access Datenbank Zugriff aus einem thread (11 answers)
Opened by vitek at 2021-10-13 11:05

vitek
 2021-10-13 13:25
#194010 #194010
User since
2009-12-02
36 Artikel
BenutzerIn
[default_avatar]
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main;

use threads;
use threads::shared;
use warnings;

# Create shared variables
my $DB_finished_flag : shared = 0;
my $DB_request_flag : shared  = 0;

# Create database worker thread
my $DB_Thread = threads->create( \&worker_DB );
$DB_Thread->detach();

# Tk Codeausschnitt
sub createMainWindow() {
...
        # [makeDBthr] Button
        my $makeDBthr_bttn = $button_frm1->Button(
                -text    => "makeDBthr",
                -state   => 'active',
                -command => sub { 
                              # only start once
                          return if $DB_request_flag;
                          \&process_DB_request();
                                  } 
...
        $mw->MainLoop();
}

sub process_DB_request {
        $DB_request_flag = 1;
        &check_status_1();
}

sub check_status_1 {
        $mw->after(
                500,
                sub {
                        if ( $DB_finished_flag == 1 ) {
                                $DB_finished_flag = 0;
                                $DB_request_flag  = 0;
                                print "DB_request finished\n";
                        } else {
                                &check_status_2;
                                print "DB_request processing\n";
                        }
                }
        );
}

sub check_status_2 {
        $mw->after(
                500,
                sub {
                        &check_status_1;
                }
        );
}

sub worker_DB() {
        while (1) {
                sleep 0.5;
                if ( $DB_request_flag == 1 ) {
                        print "begin worker_DB\n";
                        db_access::openDB( Globals::getDB_PATH() );
                        $DB_finished_flag = 1;
                } else {
                        #print "waiting for DB_request\n";
                }
        }
}

package db_access;

sub openDB() {
        $DriverType = "Microsoft Access Driver (*.mdb)";
        $DSN = "Win32 ODBC --MAOmaoMAOmaoMAO--";
        $Dir = shift; # Parameter
        print("\ndb_access::open ".Globals::getDB_PATH()."\n");
        
        Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType,("DSN=$DSN", "Description=MAO Win32 ODBC Test DSN for Perl", "DBQ=$Dir", "DEFAULTDIR=$Dir", "UID=", "PWD=")) or die "ConfigDSN(): Could not add temporary DSN" . Win32::ODBC::Error();

        $db=new Win32::ODBC($DSN) or die "couldn't ODBC $DSN because ", Win32::ODBC::Error(), "\n";
        
        return $db;
}

View full thread Access Datenbank Zugriff aus einem thread