C# 時間のかかる処理を非同期で呼び出してフォームを操作する

| コメント(12) | トラックバック(0)
WindowsアプリでWEB巡回ツールのような物を作っているのだが
WEBの巡回なので完了までに非常に時間がかかる。

巡回する数にもよるけど1時間以上かかることもざら。
そんなのを同期処理でやってたらたまらないし 途中で中断したいケースもあるのに
同期処理だと中断ボタンを押すこともでいない

なので非同期で呼び出すように実装してみた。
いろいろ悩んだ部分もあるけど おおむねうまく動いている

独自クラスもいくつか作ってるのでこのままコピペしても動かないけれど
イメージとしてはこんな感じ。

ボタンクリックイベント{
//新しいスレッドを作成
threads = new Thread(new ParameterizedThreadStart(startThreadMain));
threads.IsBackground = true;
//スレッドで処理するqueueを引数で渡して起動
threads.Start(thqueues);
}

delegate void CloseDelegate();

private void startThreadMain(Object o) {
//スレッド内での処理をするQueue
ThreadQueue thqueue = (ThreadQueue)o;
try {
//メイン処理を行う
//MainClass.Main内でWebBrowserをNEWしてWEBから情報を取得し
//その結果をFormアプリに結果を返す
DeleMain main = new DeleMain (MainClass.Main);

//
IAsyncResult ret = this.BeginInvoke(main, new object[] { thqueue });
//登録処理が終わるまで待つ
ret.AsyncWaitHandle.WaitOne();
if (ret.IsCompleted) {
object obj = this.EndInvoke(ret);
}
//終了時の処理

//終了メッセージを画面表示
Invoke(new CloseDelegate(finish));

} catch (Exception ex) {
//エラー処理
}
}


ただ自分の実装方法が悪いのかもしれないけど1日中動かしていると2,3回処理が止まってしまうことがある。
CPU使用量もメモリ使用量もたいしたことないのになぜ。。。

目下原因を調査中(笑)
MainClass.Mainの中でもいろいろ複雑なことやってるからそっちが問題の可能性が高いので とりあえずこちら側は公開しておきます(笑)

トラックバック(0)

トラックバックURL: http://blog.romaji.net/mt4/mt_cgi/mt-tb.cgi/97

コメント(12)

Do you message me with any tips about how you made your site look this awesome, I would enjoy it!

There are numerous fascinating time limits in this article however I don’t determine if I see every one of them center to heart. There is certainly some validity however I'm going to take hold opinion until I check into it further. Good article , thanks and then we want extra! Included with FeedBurner as properly

Is fantastic post 3) Ill surely be peeping with it again soon! ;>

Thanks for making the sincere attempt to provide an explanation for this. I feel very strong approximately it and wish to be informed more. If it's OK, as you attain more extensive wisdom, may you mind including extra posts very similar to this one with additional info? It would be extraordinarily useful and useful for me and my colleagues.

I found your blog using Bing and i also must say this really is the single most informative blogs We've read for a while. I will make sure I go back to read your future posts.

Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!

Your blog came out during my search and i'm stunned at whatever you have published with this topic. I'm currently expanding my inquiry and for that reason cannot contribute further, nevertheless, We have bookmarked your internet site and returning to get caught up with any upcoming updates. Just love it and provides thanks for tolerating my comment.

Between me and my husband we've owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I've settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

Only a smiling visitant here to share the love (:, btw great layout. "Everything should be made as simple as possible, but not one bit simpler." by Albert Einstein.

Hey There. I discovered your blogdebt relief using msn. This is a very well written article. I will make sure to bookmark it and come back to learn more of your useful information. Thanks for the post. I'll definitely comeback.

Zune and iPod: Most people compare the Zune to the Touch, but after seeing how slim and surprisingly small and light it is, I consider it to be a rather unique hybrid that combines qualities of both the Touch and the Nano. It's very colorful and lovely OLED screen is slightly smaller than the touch screen, but the player itself feels quite a bit smaller and lighter. It weighs about 2/3 as much, and is noticeably smaller in width and height, while being just a hair thicker.

Between me and my husband we've owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I've settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

コメントする

2011年9月

        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