melt日記

.NETすらまともに扱えないへたれのページ

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  111  : 記事  3  : コメント  8228  : トラックバック  41

ニュース

わんくま同盟

わんくま同盟

C# と VB.NET の質問掲示板

iKnow!


Dictation



書庫

現在 STA で COM の exe サーバを作っているのですが、最近「STA より MTA の方が楽なんじゃね?」とか思うようになってきました。


STA であればシングルスレッドなので、自分のメソッドが呼び出されている間に別のメソッドが呼ばれてしまうといった再入の問題について深く考えずにプログラミング出来るかなと思っていたのですが、大きな間違いでした。

というのも、STA は exe 間での通信はメッセージキューを介して行われるわけですが、メッセージというのは明示的に回さなくても勝手に回っていることが多々あるんですね。


例えばモーダルダイアログやメッセージボックスといったものは内部でメッセージを回しているので、当然呼び出している間に再入される可能性があります。

// COM メソッド
STDMETHODIMP CHoge::Foo()
{
    ...                  // 何かの処理
    m_window.DoModal();  // ここでダイアログを表示している間に
                         // 誰かが CHoge::Foo を呼び出すと、
    ...                  // この処理が呼ばれる前に再入されてしまう
}

これはまあ、内部でメッセージが回っているのは Windows プログラマなら一瞬でわかるのでこれはいいとして。


問題は COM のメソッドを呼び出している間にもメッセージは回るということです。

例えば自分の作っている exe サーバはコネクションポイントがあるので、シンクに対してイベントを配信します。

このイベントを配信している間にもメッセージが回るので、これも再入の可能性があります。

あと最近あったのは、余所のライブラリがこっそりと COM のメソッドを呼び出していたパターンです。

// COM メソッド
STDMETHODIMP CHoge::Foo()
{
    ...               // 何かの処理
    m_library.Bar();  // ここの内部で COM メソッドを呼び出していたので、
                      // この処理中に誰かが CHoge::Foo を呼び出すと、
    ...               // この処理が呼ばれる前に再入されてしまう
}

こういったライブラリをあちこちで使用していたとなると、どこで再入される可能性があるのか分からなくなります。


で、マルチスレッドにおいて再入を防ぐにはクリティカルセクションを使っていたわけですが、STA はシングルスレッドなので使えません。

ではどうするかというと、STA での再入の制御は IMessageFilter という機構を使います。

IMessageFilter については [C++]ビジー状態 で書いたので細かいことは省略するとして、とりあえずこの機構を使えば自分の COM メソッドが呼ばれる前に「今忙しいからちょっと待って!」と言うことが可能になるので、自分の望むタイミングで処理を受け取ることが可能になります。


ただ IMessageFilter にも落とし穴があって、「今忙しいからちょっと待って!」と言われたクライアントはどうするかというと、MFC であれば上記のリンク先にあるビジー状態のダイアログを出します。

しかし IMessageFilter に対して特に何もしていない場合は、サーバが「今忙しいからちょっと待って!」と言っているにもかかわらず RPC_E_CALL_REJECTED というエラーを返します

これは IMessageFilter::RetryRejectedCall のデフォルトの処理がエラーを返すようになっているのが原因なので、クライアント側もちゃんと IMessageFilter を導入し、::CoRegisterMessageFilter で登録してやる必要があります。

MFC なら上記のリンク先にあるメソッドによってビジーダイアログが出ないようにすればいいでしょう。


しかし、まだ IMessageFilter には罠があります。

例えば MFC でアプリケーションを作っているクライアントが、ワーク用のスレッドを起こして GIT 経由でサーバにアクセスしようとしたけど、そのサーバが「今忙しいからちょっと待って!」と言ってきた場合、MFC ならデフォルトの IMessageFilter がセットされているにもかかわらず、クライアントには RPC_E_CALL_REJECTED が返されます

::CoRegisterMessageFilter の説明を読めば分かるのですが、IMessageFilter はスレッド単位で設定する必要があるのです。

(英語の全然読めない自分は、この事実に気が付くのにかなりの時間が必要でした……)



つまり STA な exe サーバを作ると、

1.変なところで再入されてしまう可能性があるので IMessageFilter によってガチガチにしてやる。

2.するとサーバは「今忙しいからちょっと待って!」と言ってリトライをお願いすることが多々あり、

3.サーバ的にはリトライをお願いしてるんだけど、クライアントが全員 MFC を使っているわけでもないし GIT を使う可能性もあるし IMessageFilter についてよく知っているわけでもないので、なぜか RPC_E_CALL_REJECTED が返されてしまう。

となり、クライアントが適切に IMessageFilter を導入しないと恐ろしく信頼性の悪いサーバが出来上がってしまいます。


MTA はまだやったことないので何とも言えないのですが、STA はクライアント側に IMessageFilter の知識を要求する(もしくは RPC エラーを許容させる必要がある)ので、これならやっぱり MTA の方がいいんじゃないかなぁとか思うのですがどうなんでしょう。

投稿日時 : 2007年12月13日 18:34

コメント

# re: STA の難しさ 2008/01/28 19:11 渋木宏明(ひどり)
化石レスです (^^;

exe サーバって STA でしか作れないんじゃなかったでしたっけ?


# re: STA の難しさ 2008/01/29 14:36 melt
そんなことは無いような...。
STA で作った exe サーバで atlbase.h をインクルードする前に _ATL_FREE_THREADED を define してやると、当然排他制御なんてしてなかったのですぐに実行時エラーになりましたが、そのエラーがいつの間にか作られていたスレッドから発生していたので多分 MTA になってるんだと思うのですが...。

# Articles about _atl_free_threaded volume 6 « Article Directory 2010/06/15 4:15 Pingback/TrackBack
Articles about _atl_free_threaded volume 6 « Article Directory

# re: Ruby で数値を 0 埋めする 2019/01/24 13:56 zzyytt
http://www.airmax90.us.org
http://www.goyard-handbags.us.com
http://www.michaeljordanshoes.us.com
http://www.adidasnmds.com
http://www.vans-outlet.us.com
http://www.adidasshoesonline.com
http://www.russellwestbrookshoes.us.com
http://www.adidasnmds.us.com
http://www.nike-hyperdunk.us.com
http://www.goldengoose-outlet.us.com
http://www.jordan11retro.us.com
http://www.nikehuaracheshoes.us.com
http://www.bape-hoodie.us.com
http://www.curry5shoes.net
http://www.hermesbelts.co.uk
http://www.fila-shoes.us.com
http://www.jordanshoes.org.uk
http://www.guccibelt.us.com
http://www.adidaseqts.com
http://www.yeezy-shoes.org.uk


# Pandora Ring 2019/04/05 4:21 gvihgszm@hotmaill.com
gbbqfw,If you want a hassle free movies downloading then you must need an app like showbox which may provide best ever user friendly interface.

# Pandora Jewelry Official Site 2019/04/13 4:56 lbxpdwvekwq@hotmaill.com
hltaghexky,Thanks for sharing this recipe with us!!

# Yeezys 2019/04/16 13:52 yhwnrjwbe@hotmaill.com
ivmxdxxjy Yeezy,If you want a hassle free movies downloading then you must need an app like showbox which may provide best ever user friendly interface.

# Adidas Yeezy 500 2019/04/23 7:02 oixojtl@hotmaill.com
Globally, Spotify is still far ahead of Apple. As of December last year, Spotify said that there are 207 million active users worldwide, of which 96 million are paid users, or subscribers during the trial period, and the remaining Spotify active users have chosen a free advertising version of the service.

# Nike Shox Outlet 2019/04/28 13:06 ayzdxt@hotmaill.com
Police said Raja was in plainclothes and driving an unmarked van when he encountered a car he thought was abandoned on a West Palm Beach highway exit ramp on Oct. 18, 2015, a few hours before sunrise.

# NFL Jerseys 2019 2019/04/28 23:16 bvqajznr@hotmaill.com
At one point, roughly 1,700 people were under quarantine orders, but by Friday health officials had cleared more than 1,000 who had come forward with proof of vaccination, Los Angeles County public health officials said at a news conference.

# Yeezy 2019/04/29 9:27 qspwhqenbzj@hotmaill.com
On Friday, the White House honored the first lady with a tribute on Twitter. “Happy Birthday, @FLOTUS!” the post was captioned along with an image of Mrs. Trump sitting alone on a sofa during a bilateral meeting between Donald Trump and Czech Prime Minister Andrej Babis in the Oval Office.

# Jordan 12 Gym Red 2018 2019/05/03 3:06 erigyskv@hotmaill.com
That said, Josh Jacobs looks like a fitting replacement for Marshawn Lynch, and Johnathan Abram sure can lay the wood.

# cheap jerseys from china 2019/05/03 11:09 ywzwln@hotmaill.com
Sanders, a leading Democratic presidential contender, said during a CNN town hall that he believes every American citizen ? presumably meaning those at least 18 years old ? should have the right to vote, even felons serving life sentences.

# Cheap NFL Jerseys 2019/05/04 3:50 nercwgfufs@hotmaill.com
Following successful contested jump shots over Lillard, Westbrook would occasionally trot back on defense making the rock the baby gesture.

# Nike 2019/05/08 15:02 jcasouvhgp@hotmaill.com
I think in two weeks, I will be 100 percent, he told TideSports.com. They won’t let me play golf for six weeks for some reason, but I am going to try and get that reduced.

# Cheap NFL Jerseys 2019/05/12 6:58 iktnxyvje@hotmaill.com
The Analog bucks that trend, and instead soars into the sky with an upper that combines light tones via its neutral-colored suede overlays, cream leather, and white mesh. Small hits of high-vis reflective material add just enough literal and figurative flash, while the bulky midsole encases a generous helping of Boost cushioning in its white/sail rubber.

# Cheap Sports Jerseys 2019/05/13 2:26 canepa@hotmaill.com
The Warriors in Game 5 met most of their offensive goals. They had 31 assists and eight turnovers. Steph Curry, Klay Thompson and Durant combined for 91 points on 49.1-percent shooting. They know they have the Curry/Durant pick-and-roll, and they'll use it if a boost is needed. But the problem in Game 5, as well as the pivotal portion of Game 2, was an utter lack of defensive focus, execution and effort.

# Yeezy 350 2019/05/17 12:30 xtlsvglhpll@hotmaill.com
http://www.dallascowboysjerseyscheap.us/ Cowboys Jerseys Cheap

# Nike Air Zoom 2019/05/20 0:17 xvkvkujtp@hotmaill.com
http://www.pandora-officialsite.us/ Pandora Jewelry Official Site

# NFL Jerseys Cheap 2019/05/31 1:44 nckutkinq@hotmaill.com
http://www.jordan11concord.us.com/ jordan 11 concord

# Travis Scott Air Jordan 1 2019/05/31 3:10 cqzxfon@hotmaill.com
The first indicator of ruthlessness is effort. The Warriors brought it in Game 1 and for the better part of Game 2,Jordan before they completely and inexplicably lost it ? and the game. They hit 10 on the ruthless meter in Game 3 and brought enough of it to squash a Clippers rally and prevail in Game 4.

# NFL Jerseys 2019/06/06 20:08 xjxyvap@hotmaill.com
http://www.nfljerseys2019.us/ NFL Jerseys

# Nike Store 2019/06/10 13:16 jfmmoryb@hotmaill.com
http://www.nikeoutletonlineshopping.us/ Nike Outlet Store

# Jordan 12 Gym Red 2018 2019/06/11 4:51 ogugtscdqi@hotmaill.com
http://www.pandora-com.us/ Pandora Jewelry Official Site

# Nike Outlet store 2019/06/20 2:49 jtuuihqwy@hotmaill.com
http://www.nike--outlet.us/ Nike Outlet Online

# Balenciaga 2019/06/24 5:39 decrpdbc@hotmaill.com
http://www.cheapjerseysfromchina.us/ cheap jerseys from china

# Cheap Yeezy Shoes 2019/06/24 5:41 gkzkcbe@hotmaill.com
http://www.nikeoutletstoreonlineshopping.us/ Nike Outlet Store

# Pandora Charms Sale Clearance 2019/07/02 10:54 ktlvzrzyfc@hotmaill.com
http://www.basketball-jersey.us/ Basketball Jersey

# Yeezy 350 2019/07/16 4:14 mvoohew@hotmaill.com
http://www.yeezy.com.co/ Yeezy

# Yeezy 2019/08/01 9:52 ovxauxphf@hotmaill.com
http://www.yeezy350.us.com/ Yeezy 350

# Yeezy 350 2019/08/05 3:53 daxvaapnuc@hotmaill.com
http://www.jordan11-concord.com/ air jordan 11 concord

# Yeezy 2019/08/11 9:48 llqoryfgvrp@hotmaill.com
http://www.nikeoutletonlineshopping.us/ Nike Outlet Store

# Adidas Yeezy 2019/08/20 21:11 eycznyk@hotmaill.com
http://www.yeezyboost350v2.de/ Yeezy Boost 350

# Adidas Yeezy 2019/09/02 7:30 ygxnluwzdl@hotmaill.com
jruvmj,This website truly has alll of the information and facts I wanted about this subject and didn't know who to ask.

# hOJbfCviqfrulJPhhIM 2021/07/03 4:26 https://www.blogger.com/profile/060647091882378654
Thanks so much for the post.Thanks Again. Really Great.

# doxycycline 50 mg https://doxycyline1st.com/
doxycycline hyclate 2022/02/26 8:14 Doxycycline
doxycycline 50 mg https://doxycyline1st.com/
doxycycline hyclate

# top ed pills https://erectionpills.best/
top ed pills 2022/06/28 19:43 ErectionPills
top ed pills https://erectionpills.best/
top ed pills

# plaxovid https://paxlovid.best/
paxlovid indications 2022/09/08 7:29 Paxlovid
plaxovid https://paxlovid.best/
paxlovid indications

# ed pills otc https://erectiledysfunctionpills.shop/ 2022/10/14 22:42 Erectile
ed pills otc https://erectiledysfunctionpills.shop/

# european singles dating sites https://datingtopreview.com/
dating web site 2022/10/17 20:32 Dating
european singles dating sites https://datingtopreview.com/
dating web site

# prednisone pill https://prednisone20mg.site/
price for 15 prednisone 2022/11/15 17:51 Prednisone
prednisone pill https://prednisone20mg.site/
price for 15 prednisone

# can i buy prednisone from canada without a script https://prednisonepills.site/
where to buy prednisone in canada 2022/11/28 23:45 Prednisone
can i buy prednisone from canada without a script https://prednisonepills.site/
where to buy prednisone in canada

# best medication for ed https://edpills.science/
ed meds 2023/01/07 13:45 EdPills
best medication for ed https://edpills.science/
ed meds

# best over the counter appetite suppressant 2023/01/26 20:48 Jamessop
https://over-the-counter-drug.com/# over the counter antifungal cream

# over the counter ringworm treatment 2023/02/01 4:41 Thomasneows
https://doxycycline.science/# doxycycline 100mg

# ivermectin 200mg 2023/02/07 1:03 Zacharyunava
Actual trends of drug. Read information now.
https://stromectolst.com/# stromectol usa
Best and news about drug. Read information now.

# earch our drug database. Get here.
https://edonlinefast.com
Read information now. Drug information. 2023/02/17 16:30 EdPills
earch our drug database. Get here.
https://edonlinefast.com
Read information now. Drug information.

# Best and news about drug. Get warning information here.
https://canadianfast.com/
Get warning information here. earch our drug database. 2023/02/19 17:48 CanadaBest
Best and news about drug. Get warning information here.
https://canadianfast.com/
Get warning information here. earch our drug database.

# prednisone tabs 20 mg - https://prednisonesale.pro/# 2023/04/22 15:02 Prednisone
prednisone tabs 20 mg - https://prednisonesale.pro/#

# how to get cytotec online - https://cytotecsale.pro/# 2023/04/28 23:25 Cytotec
how to get cytotec online - https://cytotecsale.pro/#

# buy cytotec pills - https://cytotecsale.pro/# 2023/04/29 4:33 Cytotec
buy cytotec pills - https://cytotecsale.pro/#

# what does over the counter mean https://overthecounter.pro/# 2023/05/08 22:36 OtcJikoliuj
what does over the counter mean https://overthecounter.pro/#

# over the counter ed pills 2023/05/14 10:29 Mickeynix
http://edpills.pro/# ed pills online

# best erectile dysfunction pills 2023/05/14 19:05 MichaelExcup
https://edpills.pro/# ed pills otc

# ed pills: https://edpills.pro/# 2023/05/16 3:10 EdPillsPro
ed pills: https://edpills.pro/#

# ed meds online 2023/06/09 1:31 BennySpuse
http://edpillsfd.com/# non prescription ed pills

# ed medications list 2023/06/09 20:31 BennySpuse
http://edpillsfd.com/# best ed pills online

# sildenafil 50 mg tablet price in india 2023/06/28 2:04 DavidPaync
http://fastpills.pro/# most reliable canadian pharmacies

# best sildenafil prices 2023/06/30 16:55 DavidPaync
https://sildenafilpills.pro/# sildenafil citrate canada

# paxlovid india https://paxlovid.pro/# - buy paxlovid online 2023/07/03 3:50 Paxlovid
paxlovid india https://paxlovid.pro/# - buy paxlovid online

# best male enhancement pills https://edpills.ink/# - best male enhancement pills 2023/07/26 20:00 EdPills
best male enhancement pills https://edpills.ink/# - best male enhancement pills

# cytotec online https://cytotec.ink/# - Abortion pills online 2023/07/27 1:06 PillsFree
cytotec online https://cytotec.ink/# - Abortion pills online

# dating singles 2023/08/09 19:49 WayneGurry
internet dating site: https://datingtopreview.com/# - chat singles

# versandapotheke 2023/09/26 13:41 Williamreomo
http://onlineapotheke.tech/# online apotheke preisvergleich
online apotheke preisvergleich

# online apotheke gГјnstig 2023/09/26 15:41 Williamreomo
http://onlineapotheke.tech/# п»?online apotheke
internet apotheke

# gГјnstige online apotheke 2023/09/26 23:39 Williamreomo
http://onlineapotheke.tech/# online apotheke deutschland
versandapotheke

# farmacia online senza ricetta 2023/09/27 1:15 Archieonelf
https://pharmacieenligne.icu/# Pharmacie en ligne livraison gratuite

# п»їonline apotheke 2023/09/27 6:26 Williamreomo
https://onlineapotheke.tech/# internet apotheke
online apotheke deutschland

# farmacia online migliore 2023/09/27 16:40 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# farmaci senza ricetta elenco 2023/09/27 19:35 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# farmacie on line spedizione gratuita 2023/09/27 22:02 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# farmacia online miglior prezzo 2023/09/30 7:48 Rickeyrof
acheter sildenafil 100mg sans ordonnance

# buying ed pills online 2023/10/07 18:47 BobbyAtobe
An unmatched titan in the world of international pharmacies. https://doxycyclineotc.store/# doxycycline tablets in india

# canada drug store 2023/10/16 19:00 Dannyhealm
Delivering worldwide standards with every prescription. https://mexicanpharmonline.shop/# mexican pharmaceuticals online

# canada pharm 2023/10/17 3:17 Dannyhealm
They always offer alternatives and suggestions. http://mexicanpharmonline.com/# mexico drug stores pharmacies

# prescription canada 2023/10/17 8:48 Dannyhealm
earch our drug database. https://mexicanpharmonline.com/# mexico drug stores pharmacies

# canada drug center promo code 2023/10/17 10:29 Dannyhealm
Providing international caliber services consistently. https://mexicanpharmonline.com/# mexican rx online

# canada mail order drug 2023/10/18 1:15 Dannyhealm
Their international partnerships enhance patient care. https://mexicanpharmonline.shop/# mexico drug stores pharmacies

# canadian pills 2023/10/18 20:21 Dannyhealm
A beacon of international trust and reliability. http://mexicanpharmonline.shop/# mexican pharmaceuticals online

# medications online without prescriptions 2023/10/19 5:27 Dannyhealm
An excellent choice for all pharmaceutical needs. http://mexicanpharmonline.shop/# mexican mail order pharmacies

# paxlovid covid 2023/10/26 18:08 LarryNef
http://stromectol.icu/# stromectol nz

# natural ed remedies 2023/11/22 13:16 WilliamApomb
http://sildenafil.win/# how much is sildenafil in canada

# doxycycline 100 mg https://doxycycline.forum/ order doxycycline 2023/11/25 9:04 Doxycycline
doxycycline 100 mg https://doxycycline.forum/ order doxycycline

# doxycycline 100mg dogs https://doxycycline.forum/ doxycycline 50mg 2023/11/25 13:08 Doxycycline
doxycycline 100mg dogs https://doxycycline.forum/ doxycycline 50mg

# farmacia online più conveniente https://farmaciait.pro/ farmacie online sicure 2023/12/04 10:09 Farmacia
farmacia online più conveniente https://farmaciait.pro/ farmacie online sicure

# the best ed pill https://edpills.tech/# non prescription erection pills 2023/12/23 8:05 EdPills
the best ed pill https://edpills.tech/# non prescription erection pills

# where to get clomid without rx 2023/12/26 18:28 RaymondGrido
http://paxlovid.win/# paxlovid india

# prednisone online for sale https://prednisone.bid/ prednisone 10mg canada 2023/12/27 6:51 Prednisone
prednisone online for sale https://prednisone.bid/ prednisone 10mg canada

# can i order generic clomid pills 2023/12/29 17:37 RaymondGrido
https://paxlovid.win/# paxlovid buy

Post Feedback

タイトル
名前
Url:
コメント