c# -- thread id 구하기

C# 2015. 6. 18. 12:46
반응형

c# -- thread id 구하기


환경 : windows 7 64bit, visual studio 2013 community version



Invoke, BeginInvoke 이용하여 thread id 구하기


using System;
using System.Threading;
namespace thread_check
{
class Program
{
delegate void del_thread(string str);
// static 으로 선언안하면, error 발생.
public static void new_thread(string name)
{
Thread cur_thread = Thread.CurrentThread;
Console.WriteLine("current {0} thread = {1}", name, cur_thread.ManagedThreadId);
}
static void Main(string[] args)
{
Thread cur_thread = Thread.CurrentThread;
Console.WriteLine("current main thread = {0}", cur_thread.ManagedThreadId);
del_thread new_th = new del_thread(new_thread);
// new_th.Invoke("test"); // -- 동기
new_th.BeginInvoke("test", null, null); // -- 비동기
Thread.Sleep(2000); // sleep 없으면, BeginInvoke 결과 안보일수 있음.
}
}
}
view raw thread_test.cs hosted with ❤ by GitHub



< 결과 >





반응형

'C#' 카테고리의 다른 글

C# -- Debug, Trace 사용하여 출력하기  (0) 2015.08.12
c# -- winform, console 동시 사용하기  (0) 2015.08.12
c# - 시스템 상태 확인 ; cpu, ram  (0) 2015.06.20
c# -- multi-thread winform 만들기  (0) 2015.06.18
c# 변수 Type 구하기  (0) 2015.06.16
Posted by 자유프로그램
,

c# 변수 Type 구하기

C# 2015. 6. 16. 14:48
반응형

c# 변수 Type 구하기  - GetType()


환경 : windows 7 64bit, visual studio 2013 community 



using System;
namespace typetest
{
class Program
{
static void Main(string[] args)
{
int a = 33;
string bb = "abc";
var cc = "test";
Console.WriteLine(a.GetType());
Console.WriteLine(bb.GetType());
Console.WriteLine(cc.GetType());
Console.WriteLine(a.GetType().Name);
Console.WriteLine(bb.GetType().Name);
Console.WriteLine(cc.GetType().Name);
}
}
}
view raw type_test.cs hosted with ❤ by GitHub





반응형

'C#' 카테고리의 다른 글

C# -- Debug, Trace 사용하여 출력하기  (0) 2015.08.12
c# -- winform, console 동시 사용하기  (0) 2015.08.12
c# - 시스템 상태 확인 ; cpu, ram  (0) 2015.06.20
c# -- multi-thread winform 만들기  (0) 2015.06.18
c# -- thread id 구하기  (0) 2015.06.18
Posted by 자유프로그램
,
반응형

postgresql 접속하기


환경 : windows 7 64bit, python 2.7.9 64bit, postgresql 9.4.2, psycopg2 2.6


참고 : http://zetcode.com/db/postgresqlpythontutorial/

       http://www.stickpeople.com/projects/python/win-psycopg/

       http://pythonhosted.org/psycopg2/usage.html

       http://initd.org/psycopg/docs/usage.html#with-statement


참고서적 : https://www.packtpub.com/big-data-and-business-intelligence/postgresql-cookbook  chapter 11.



** postgresql 과 oracle 은 한번 연결에 하나의 database 만 사용할 수 있다.

  -- http://www.sqlines.com/postgresql/pgadmin/change_database



1. psycopg2 설치하기

    다운로드 사이트 : http://www.stickpeople.com/projects/python/win-psycopg/

    에서 psycopg2-2.6.0.win-amd64-py2.7-pg9.4.1-release.exe 파일 다운받아 설치하면 끝.


2. psycopg2 를 이용하여 postgresql db 접속하기

  -- 2 가지 방법을 가능.

  -- http://pythonhosted.org/psycopg2/module.html

     ; host = postgresql server ip 

     ; dbname = database = 접속할 postgresql database name

     ; user = postgresql 에 접속할 user name

     ; password = 접속할 user 의 password

     ; port = postgresql server port ( 생략하면 5432)


# -*- coding: utf-8 -*-
import psycopg2 as pg2
conn = ppg2.connect("host = localhost dbname=test user=postgres password=1234 port=5432")
view raw pg_connect.py hosted with ❤ by GitHub
# -*- coding: utf-8 -*-
import psycopg2 as pg2
# conn = pg2.connect("host = localhost dbname=test user=postgres password=1234 port=5432")
conn=pg2.connect(database="test",user="postgres",password="1234",host="127.0.0.1",port="5432")
view raw pg_connect2.py hosted with ❤ by GitHub



3. database 만들고, table 만들기



# -*- coding: utf-8 -*-
import psycopg2 as pg2
try:
# port 기본값 = 5432
conn = pg2.connect("host = localhost dbname=postgres user=postgres password=1234")
# autocommit 없으면, InternalError: CREATE DATABASE cannot run inside a transaction block
conn.autocommit = True
cur = conn.cursor()
# database 만들기
cur.execute('create database testdb') # superuser 만 create database 명령어 가능..
cur.execute('SELECT version()')
ver = cur.fetchone()
except Exception as e:
print 'postgresql database connection error!'
print e
else:
print ver
finally:
if conn:
conn.close()
try:
# 다른 db 로 바꿀려면 재접속 해야함...
conn = pg2.connect("host = localhost dbname=testdb user=postgres password=1234")
cur = conn.cursor()
# table 만들기
cur.execute("CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price INT)")
# data insert
cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
cur.execute("SELECT * FROM Cars")
rows = cur.fetchall()
conn.commit()
except Exception as e:
print 'postgresql database connection error!'
print e
if conn:
conn.rollback()
else:
print rows
finally:
if conn:
conn.close()
view raw pgsql.py hosted with ❤ by GitHub


4. with 문 사용하기

    ; http://initd.org/psycopg/docs/usage.html#with-statement 

    ; psycopg2 2.5 부터 사용 가능.


  -- connection 이 with 문을 벗어날때, 예외가 없으면 commit 발생하고, 예외시에는 rollback 발생. 

      connection close 발생 안함.

  -- cursor 가 with 문 벗어날때, resource 해제하며(cursor.close()), transaction 상태는 바뀌지 않는다.





# -*- coding: utf-8 -*-
import psycopg2 as pg2
conn = None
try:
with pg2.connect("host = localhost dbname=test_db user=postgres password=1234") as conn:
with conn.cursor() as cur:
sql = "create table Car(Id integer primary key, Name varchar(20), Price int)"
cur.execute(sql)
cur.execute("INSERT INTO Car VALUES(1,'Audi',52642)")
cur.execute("INSERT INTO Car VALUES(2,'Mercedes',57127)")
cur.execute("SELECT * FROM Car")
rows = cur.fetchall()
# cur.close()
# conn.commit()
except Exception as e:
print 'Error : ', e
else:
print rows
finally:
if conn:
conn.close()


반응형
Posted by 자유프로그램
,
반응형

ubuntu client 에서 postgresql client 설치 하기기


환경 : ubuntu 14.04 LTS 64bit, postgresql 9.4.2, psql 9.4.2, pgadmin3 1.20.0


참조 :  https://help.ubuntu.com/community/PostgreSQL



1. 설치 준비 작업


$ sudo vim /etc/apt/sources.list.d/pgdg.list

  --> 해당 디렉토리에 pgdg.list 라는 파일을 만들어 다음 내용을 입력후 저장한다.

  --> deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main  


   


$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -




$ sudo apt-get update

    --> 리포지토리 반영하기위해 update 하면, ubuntu에서 postgresql-9.4 를 인식한다.



2. psql 9.4.2 설치


$ sudo apt-get install postgresql-client

 --> psql 설치 끝.



-- postgresql database server ip = 192.168.10.58 이고, tcp port = 5432

    접속한 database 명 = mytest1,  접속한 user 명 = postgres 인경우..

    아래의 방식들로 접속 할수있다.


$ psql -h 192.168.10.58 mytest1 postgres

혹은 

$ psql -h 192.168.10.58 -d mytest1 -U postgres



3. pgadmin3 1.20.0 설치


$ sudo apt-get install pgadmin3

 --> pgadmin3 설치 끝.







반응형
Posted by 자유프로그램
,
반응형

windows client 에 pgadmin III 설치, 실행하기


환경 : windows 7 32bit, postgresql 9.4.2, pgadmin III 1.20.0


참고 : http://www.postgresql.org/ftp/pgadmin3/release/v1.20.0/win32/



1. windows client 에 pgadmin III 다운 받아 설치하기

   다운로드 사이트 : http://www.postgresql.org/ftp/pgadmin3/release/v1.20.0/win32/


  위 사이트에서 pgadmin3-1.20.0.zip 를 다운받아 압축풀고 설치하면 끝.



2. pgadmin III 설정하기

   -- 예) postgresql database server ip = 192.168.10.58   port = 5432

           postgresql database name = mytest1

           postgresql username = postgres

           password 는 postgres 에 설정한 password 입력하고, 

           Store password 는 필요에 따라 선택, 비선택할것.




3. pgadmin III 로 원격의 postgresql database 접속 성공한 화면.














반응형
Posted by 자유프로그램
,