이벤트 StatusChanged
& ProgressChanged
를 통해 진행 상황을 보여주는 요청 데이터를 업로드하는 GetResponseStream()
클래스가있는 HttpHelper
클래스가 있다고 가정합니다.기능 비동기/비 차단을 만드는 방법에 대한 지침이 필요합니다.
public MemoryStream GetResponseStream() {
...
Status = Statuses.Uploading; // this doesn't raise StatusChanged
// write to request stream
... // as I write to stream, ProgressChanged doesn't get raised too
Status = Statuses.Downloading; // this too
// write to response stream
... // same here
Status = Statuses.Idle; // this runs ok. Event triggered, UI updated
}
코드 @pastebin. GetRequestStream()
on line 76. 사용하는 클래스가 아래처럼 호출해야하는 경우를 제외하고는 클래스 자체가 잘 작동합니다.
HttpHelper helper = new HttpHelper("http://localhost/uploadTest.php");
helper.AddFileHeader("test.txt", "test.txt", "text/plain", File.ReadAllBytes("./test.txt"));
helper.StatusChanged += (s, evt) =>
{
_dispatcher.Invoke(new Action(() => txtStatus.Text = helper.Status.ToString()));
if (helper.Status == HttpHelper.Statuses.Idle || helper.Status == HttpHelper.Statuses.Error)
_dispatcher.Invoke(new Action(() => progBar.IsIndeterminate = false));
if (helper.Status == HttpHelper.Statuses.Error)
_dispatcher.Invoke(new Action(() => txtStatus.Text = helper.Error.Message));
};
helper.ProgressChanged += (s, evt) =>
{
if (helper.Progress.HasValue)
_dispatcher.Invoke(new Action(() => progBar.Value = (double)helper.Progress));
else
_dispatcher.Invoke(new Action(() => progBar.IsIndeterminate = true));
};
Task.Factory.StartNew(() => helper.GetResponseString());
helper.GetResponseString();
를 사용하여 클래스를 호출 한 경우 클래스 자체는 작동하지만 이벤트 제기 된 것 같지 않습니다. 나는 그것이 차단되는 UI 스레드와 관련이 있다고 생각한다. _dispatcher
& Task
자료를 사용하지 않고 클래스를 쉽게 사용할 수 있도록 클래스를 다시 코딩하는 방법은 무엇입니까?
또한, 나는 이벤트/UI가 업데이트되지 않도록하는 원인을 확실히 알고 싶습니다. 코드가 동기식이라 할지라도 어쨌든 속성 변경/이벤트를 실행할 수 없으며 읽기/쓰기 afterall 이후에?
PasteBin 구문 강조를 C#으로 설정해야합니다. http://pastebin.com/FeAPB6rU – SLaks
@SLaks, 저에게 통보 해 주셔서 감사합니다. PHP yest를 사용하고 있었고 언어 변경을 잊어 버렸을 것입니다. –