solved deadlock problems by using stopstream before closestream