Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
HTTP in a Background Thread (Asynchronous HTTP)This example shows the technique one would follow to run any Chilkat HTTP method in a background task. (Only HTTP methods that communicate with an HTTP server are background-enabled. Methods that perform no HTTP communications return immediately and never need to be backgrounded.) CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @sTmp0 nvarchar(4000) DECLARE @sTmp1 nvarchar(4000) DECLARE @http int EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int -- Any string unlocks the component for the 1st 30-days. EXEC sp_OAMethod @http, 'UnlockComponent', @success OUT, 'Anything for 30-day trial' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- To run an HTTP method asynchronously in a background thread, set -- the UseBgThread property equal to 1 EXEC sp_OASetProperty @http, 'UseBgThread', 1 -- For those programming languages that support event callbacks: -- events are not fired when a task is running in the background thread. -- Instead, Chilkat has added the "event log" mechanism. While the -- background task is running, events that normally would've been fired -- are accumulated in the event log. Your application may periodically check -- the event log to keep track of the progress of the background task. -- To enable event logging, set the KeepEventLog property = 1 EXEC sp_OASetProperty @http, 'KeepEventLog', 1 -- Start an asynchronous HTTP download in a background thread. -- The method will return cktrue if the task was successfully started. -- Note: When the UseBgThread property = 1, all methods involving -- HTTP communications will be asynchronous. These methods include: -- SynchronousRequest, QuickGetStr, QuickGet, PostUrlEncoded, XmlRpc, -- XmlRpcPut, QuickPutStr, QuickGetObj, QuickDeleteStr, PutText, -- PutBinary, PostBinary, PostMime, GetHead, DownloadAppend, etc. EXEC sp_OAMethod @http, 'Download', @success OUT, 'http://www.chilkatsoft.com/download/ChilkatJava.zip', 'ChilkatJava.zip' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END ELSE BEGIN PRINT 'Initiated asynchronous HTTP download...' END -- Write a loop to wait for the background task to complete. -- Your application would typically do something different than this -- -- after all... there's no point in doing the task asynchronously if your application -- is simply going to wait for it to complete -- that's the same as doing it synchronously, -- and that could've been achieved by a single call to the http.Download method -- with the UseBgThread = 0. -- However... we do this here for the purpose of demonstration... EXEC sp_OAGetProperty @http, 'BgTaskRunning', @sTmp0 OUT WHILE (@sTmp0 = 1) BEGIN -- Show the events in the event log that have accumulated so far... DECLARE @n int EXEC sp_OAGetProperty @http, 'EventLogCount', @n OUT IF @n > 0 BEGIN DECLARE @i int SELECT @i = 0 WHILE @i <= @n - 1 BEGIN EXEC sp_OAMethod @http, 'EventLogName', @sTmp0 OUT, @i EXEC sp_OAMethod @http, 'EventLogValue', @sTmp1 OUT, @i PRINT @sTmp0 + ': ' + @sTmp1 SELECT @i = @i + 1 END EXEC sp_OAMethod @http, 'ClearEventLog', NULL END -- In some programming languages, you might wish to handle user-interface events -- For example, in C# you might call Application.DoEvents() -- Sleep .1 seconds -- to keep the CPU from being 100% busy... EXEC sp_OAMethod @http, 'SleepMs', NULL, 100 END -- Once the background task has completed, check it for success/failure: EXEC sp_OAGetProperty @http, 'BgTaskSuccess', @sTmp0 OUT IF @sTmp0 BEGIN PRINT 'Background task completed successfully.' END END GO |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.