<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d6016338663470588542\x26blogName\x3dA+Guide+to+Hell\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://shasini.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://shasini.blogspot.com/\x26vt\x3d-3621103276626359617', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

About

"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt."

Stuffs which learnt from thread-safe communication (block current thread to wait others) Friday, May 18, 2007 |

Recently I decide to rewrite my SharePoint upload tool, for improve its reliability and stabllity. One of my core idea is to use a htmlParser to analyse the HTML form out. And display all input box to users for customize the values and send to server.

After checked out a ton of c# htmlparser, I found most of them are not so intelligentized.

The one I needed is a light weight, simply use and better ability to analyse html page which has incorrect gramma.

Finally, I found the webbrowser control which integrated in vs2005 is fitting my needs.I decide to use it for my parser.

To implement a IParser interface, I must implement a Parse() method, which will return a well-formatted html document object.

Unfortunately the webbrowser control use a thread to navigate page. But my Parse() method is a single thread method, if I use webbrowser.navigate("http://sdfdf.com") directly, the Parse() method will return and I get nothing.

So, here is the problom: How to block current thread and wait for another thread finish?

My first idea is write a while(true) in main thread, and use a variable to mark whether the thread is continue or not, but it is hard to get throught because the documentComplete function will be fired by main thread -- navigate thread will send a Windows Message to main thread, then main thread invoke documentComplete function. But due to the main thread is blocked by while(true) it can't receive the message which sent by navigating thread.

So we must use some trick to make the main thread blocked and still receiving windows message.

After surfing on web, I found C# has encapluationed a WaitEventHandle (API WaitForSingleObject) and a application.DoEvent which implemented a message loop.

The waitEventHandld is the formal way to block a thread to wait the others. It use Windows Event machanism to help thread-safe communication, but it still can't receive message from queue when blocked.

Add Application.DoEvent to the while(true) scope is a feasible way. but because the main thread is waste a lot of resource for doing the loop. the cpu usage will raise up to 100%.

My mentor Jerry told me that he used to use the MsgWaitForMultipleObjects API to deal with this scenario. The MsgWaitForMultipleObjects will block if there is no message in queue and return if there is message need to be translated. A thread blocked by MsgWaitForMultipleObjects just waste a little bit system resource. It only let go my PeekMessage loop when a message comes. It is the best way to deal with this scenario. After I add a simply loop to receive message my parser works:)


WindowsEvent.MSG msg = new WindowsEvent.MSG();
mySafeWaitHandle = myEventWaitHandle.SafeWaitHandle;
while (WindowsEvent.MsgWaitForMultipleObjects(1, ref mySafeWaitHandle, false, 3000, WindowsEvent.QS_ALLEVENTS) != 0)
{
while (WindowsEvent.PeekMessage(ref msg, 0, 0, 0, WindowsEvent.PeekMessageOption.PM_REMOVE))
{

WindowsEvent.TranslateMessage(ref msg);
WindowsEvent.DispatchMessage(ref msg);
}
//you can use Application.DoEvents(); to instead of peekmessage... but I want to do it myself for learning C# P/invoke.
}




How to do a API invoke in c# (p/invoke)
http://msdn.microsoft.com/msdnmag/issues/03/07/NET/
A thread related to message loop help you understand it:
http://www.gamedev.net/community/forums/topic.asp?topic_id=207360
Understand the message loop:
http://www.winprog.org/tutorial/message_loop.html
Definition of MSG(struct of message):
http://msdn2.microsoft.com/en-us/library/aa929818.aspx
PeekMessage on MSDN:
http://msdn2.microsoft.com/en-us/library/aa924590.aspx
MsgWaitForMultipleObjectEx:
http://msdn2.microsoft.com/en-us/library/aa931971.aspx

Sample:
http://www.codeguru.com/forum/showthread.php?t=363382

Labels: ,

Fuck you google! your blogger publish functionallity suck! |

18 days... no even one log was published to my server...
I used blogger FTP publish function to update my blog everyday, before May, everything works fine, but now, you see, every guy thinks that I pretend to be disppeared from network because I never published a log in past 18 days! In the meaning time, I am trying again and again to publish my logs via ftp, but google's suck blogger publish page just displayed "connection exception: time out"!

I posted a topic in blogger help group, but no guys anwsered my question! No one!Even a single message!

FUCK YOU! BLOGGER!
FUCK YOU! GOOGLE!

Best solution for window.onload replacement |

In my last post, I mentioned a way to resolve fire javascript function after layout (the dom) is ready but before images were downloaded. I mean, "sometimes it is too late to use window.onload to fire js".

But the solution is sometime incorrectly using for resolve the problem.
In case of document.readyState is "interactive", the page must be refreshed. So if you newly navigated to a page, the readtState never be "interactive".
And after a deeply test of "complete" status, it has some behavior as window.onload.

Dean has a solution, it is smart and cross-browser(IE,Mozilla,Opera,Safari).
http://dean.edwards.name/weblog/2005/02/order-of-events/
I Traced the post above, read all the comments and finally got a perfect solution:


// Dean Edwards/Matthias Miller/John Resig

function init() {
// quit if this function has already been called
if (arguments.callee.done) return;

// flag this function so we don't do the same thing twice
arguments.callee.done = true;

// kill the timer
if (_timer) clearInterval(_timer);

// do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
init(); // call the onload handler
}
};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loadedcomplete/.test(document.readyState)) {
init(); // call the onload handler
}
}, 10);
}

/* for other browsers */
window.onload = init;

Labels: ,

Deeply thought of javascript dynamic image resize Tuesday, May 15, 2007 |

Dynamic image resizing is a useful functionality for forums and blogs, it widely spread and be used by many web-based systems.

Quchao.com posted a self-used images resizing encapsulation which I look deep into it and found massive improvable spaces.

http://www.quchao.com/entry/my-resizeimgs-func/#comment-3148

function resizeImgs() {
if (!document.getElementsByTagName) {
return;
}
var defaultWidth = 400; // Defualt value for the images to be resized
var imgs = document.getElementsByTagName('img');
var imgsLen = imgs.length;
for (var i = 0; i < resize =" imgs[i].getAttribute('resize'))" width =" resize;" width =" defaultWidth;">

//Sorry, currently Chinese only. I will update this to English soon.
遍历的话使用
for 在"杀猫贴"中会导致IE假死,解决的办法是使用 setTimeout来做,
但是setTimeout最小值只能为10,速度太慢.

而对于image完全可以使用document.images来定位,而不必遍历所有的ELEMENT,速度会快很多.

如果使用

for (var i = 0 ; i < document.images[document.images.length]; i++){
document.images....
}
放在文档的最后去做的话,速度会快很多,而且不需要window.onload
图片也可以在第一时间设置为指定大小.
但该方法不能很好够unobtrusively的形式对功能进行封装,因为无法确定最后使用者也能够正确的将Script放在document的最后,而且不够灵活.

要解决这个问题,就必须使用到document.onreadystatechange
当页面LAYOUT完成而图片未加载是,就开始执行JS代码
document.onreadystatechange = function(){
if (document.readyState == "interactive"){
alert(document.images[document.images.length -1].readyState);
}

Labels: ,

Is that possible to get a function name in Javascript Wednesday, May 9, 2007 |

Generally it is impossible but some guy done that with a informal way

<Script>
var varx = {
addChild: function Wlc_Framework_UI_UIGroup$addChild(child) {
var x = "hello dfsalkfj kasfj"
}
}
var pat = /[\(\{\n\r].*?[\)\}\n\r]/g


try {
alert(varx.addChild.toString().replace(pat,""))
}catch(ex){alert(ex.message);}

</script>

Labels: ,