C# - Using Async Methods Asp.net?

Jul 15, 2010

I have two methods on the page. One AddMessageToInboxOutbox(params[]) and other one SendNewMessageMail(params[]).

Once user sends an message to other other first message is added to DB and then sent to recipient's email. Sometimes SMTP is heavy loaded and it takes up to 5 seconds to get answer from it. I want to enclose SendNewMessageMail(params[]) with async call using other thread or something. I have never done that.

How do i perform this action right in ASP.NET?

View 2 Replies


ADVERTISEMENT

Making An Async Function Not Async?

Jun 3, 2010

I'm trying to write a 'developer friendly' wrapper for the Windows API EnumWindows - the problem is that the API uses a callback function that gets invoked on another thread, where as I want my wrapper function to simplify this and make it more useful by working like any other normal synchronous function and just returning a value.

Now if the API just called the callback once that would be fine, as I would just call the API and make the wrapper function wait until it got a signal from the callback function telling it to continue... but the API calls the callback function over 100 times (once for every window handle that exists). The biggest problem is that there is nothing that indicates that any particular call to this callback function is the last one, so my wrapper function has no idea when it is safe to continue and return the list of window handles to the caller.

The only solution I could think of is to do this:

1. The wrapper function calls the API and starts a timer that waits 2 seconds before it raises the Elapsed event. The wrapper function then pauses until it receives a signal (ManualResetEvent)

2. Each time the callback function is raised by the API on a different thread, it stops the timer (so this is before the 2 seconds have elapsed), adds the current window handle that was passed in by the API to the list of handles, then starts the timer again.

3. If the timer reaches its 2 second interval then it is assumed that we are at the end of the windows because otherwise the callback would have stopped the timer. So the timer's Elapsed event handler is what signals the original thread (that the wrapper function is executing on) to continue as we now have a complete list of windows to return.

This works fine and does exactly what I want... but I dont like it.

I dont like forcing the caller to wait an extra 2 seconds after the API has done its last callback but more importantly I dont like assuming that the API will never take longer than 2 seconds between callbacks. In reality on my PC it is never anywhere near that long between callbacks, it is something like 200 miliseconds, but I have no idea how long it would be on a slower PC and I want this to be completely reliable.

[URL]

View 14 Replies

Mvc - Repository Pattern Implements Methods By Adding Add1 But Should Use Methods Of The Baseclass?

Aug 29, 2011

This is the original code in c#

public class CategoryRepository: RepositoryBase<Category>, ICategoryRepository
{
public CategoryRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)

[Code]...

Does anyone has an idea what i should change to let it work and let my UserRepository use the methods in RepositoryBase while implementing the IUserRepository?

View 1 Replies

Cant See Available Methods List When Write Object.METHODS?

Jun 24, 2011

I am working with a vb program, but there is something strange on one of my .vb code pagewhen i put the "dot" afther the object name its dont show the methods availables for this objectbut on other vb code pages i can see it. but in this one no.for exmaplethis is a piece of code: Dim sb As New StringBuilder()

View 3 Replies

Making Class Methods Instead Of Instance Methods In .NET?

Mar 29, 2010

I am not sure how clear my question is by the title, but I am trying to make Class methods instead of Instance methods in Visual Basic that way I don't have to waste memory and code creating temporary objects to execute methods that don't need instance variables.

I am not sure if you can do that in VB but I know you can in Objective-C by using either a "+" or "-" sign in front of the method declaration. And in C++ (at least I think, I can't remember) you put the static keyword or const keyword in front of the function.How would I do this in VB if it is possible? Or should I just make a separate set of functions that are not members of a class?

View 2 Replies

Wpf - Update UI Async?

Nov 3, 2011

[Code]...

In my scenario the Button_Click is a command delegate in the VM, and the Thread.Sleep is some long-running process (about 2-10 seconds). I want, that when the user calls the command, it should immediately update the UI disabling the button so the user cannot execute it while it's running, then execute that operation, then, when operation completed, unblock the button.

[Code]...

View 3 Replies

2 Different Methods Using Same Object Methods?

Aug 26, 2010

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim someproc As Process

[Code]...

View 3 Replies

Async Callback From ThreadPool?

Jun 12, 2009

I have a bunch of operations that need to be executed simultaneously. For this I'm using the thread pool to execute the tasks. I'd like to know if it's possible to consume some sort of Async Callback (similar to BackgroundWorker.DoWorkComplete) when each thread is finished. The reasoning behind this is that I have a windows service running that uses IPC to update a GUI and I need to know the time at which each specific thread finishes I've used background workers to do a set of tasks and they work perfectly, however, they take longer than I would like. I've timed the execution on my BGW method for a designated number of said tasks and it took 27 seconds, and the same set of tasks takes 4 seconds using the threadpool method. I've also tried using managed threads, to the same effect as the background workers.

View 5 Replies

C# - .NET Async Lambda-expressions?

Apr 20, 2012

Async Sub like this: Dim f As Func(Of Task) = Async Sub() End Sub Produces compiler error: error BC36670: Nested sub does not have a signature that is compatible with delegate 'System.Func(Of System.Threading.Tasks.Task)'.Equivalent C# code compiles fine:

Func<Task> f = async () => { };Rewriting Async Sub into Async Function make code works.

Why does Async Sub() is not convertible to delegate types with return value of type Task?

View 1 Replies

How To Make WebRequest ASync

Oct 1, 2011

I think this is a simple question for you, but I don't understand other cases of webRequests, so I asked here: How can I make this webRequest asynchronous?
Dim sBuffer As String
Dim oRequest As WebRequest = WebRequest.Create(url)
oRequest.Method = "GET"
Dim oResponse As WebResponse = oRequest.GetResponse()
Dim oStream As New StreamReader(oResponse.GetResponseStream())
[Code] .....

View 1 Replies

Using Async CTP Await With Events?

Jul 15, 2011

Relevant to Silverlight 5 / Async CTP

I want to create an asynchronous function that initiates a layout update and then Awaits for the layout update to complete. Something like:

Private Async Function UpdateLayoutRoot() As Task
LayoutRoot.UpdateLayout()
Await LayoutRoot.LayoutUpdated <--- (NOT valid but shows desired outcome)
End Function

How can this be done? More generally, how can you use Await to wait for existing events?

View 2 Replies

Wpf - HttpWebRequest.BeginGetResponse Is Not Async?

Mar 31, 2011

I have some code that iterates a few 100 urls and requests the data from the web.It looks something like this

for each url in urls
Dim hwr = CType(WebRequest.Create(url), HttpWebRequest)
Dim rq = New ReqArgs

[code]....

How come the BeginGetresponse line takes about 2-3 seconds to complete before going to dim a=1?. Actually I debugged and I see that the FinishWebRequest procedure runs completely before the Dim a=1 is reached.So is this async?I'm not earning any time by using the async. am I? Or is there a different way to do this?The point is that the main sub should fire off 300 requests and return control to the UI, then the FinishWebRequest should process them slowly on its own thread and own time , as the requests come in.How do I do that?Btw, the main sub is running in a BackgroundWorker, but I checked with out the BackgroundWorker and the problem is the same?

View 1 Replies

.net - Using Task For Async Operation But UI Is Still Unresponsive

Jun 14, 2012

I have a WPF application in which i have to do a long running task (basically reading from network). Just to give you a snapshot I am doing the following thing on button click

Dim t As Task(Of String) = Task.Factory.StartNew(Of String)(Function()
'Thread.sleep is simulating long running task that will make UI unresponsive
Thread.Sleep(10000)

[Code].....

I cannot use Event based async methodology because the reading API actually exist in a dll which i refer in my program, that contains a function Public function ReadFromNetwork() as String. This API is making an async call to network to read a long string and return to UI. So, in short i m doing TextBlock1.Text = ExternalDll.ReadFromNetwork().

but the problem is that even if i use Task asynchrony, the UI is still unresponsive.

View 3 Replies

Async - Pinging All Computers In List

Nov 24, 2011

I've run into a bit of a problem using Async ping. Basically, I want to ping computers in the list before I run the next bit of my program. The computers who I can ping are added to a list and that is used in a background worker. Trouble is can't seem to work out how to wait until after pinging ALL computers in the list, either successful or timed-out. Is there something a long the lines of 'If Async Ping is running'?

Private Sub pingCompleted(ByVal sender As Object, ByVal e As System.Net.NetworkInformation.PingCompletedEventArgs)
If e.Reply.Status = Net.NetworkInformation.IPStatus.Success Then
complist.Add(e.UserState)
Else
'do nothing
End If
End Sub

I need the next bit to wait because if 'complist' is changed while it's being used I get collection errors.

View 7 Replies

Async Connect From VB 6 To VB 2008 Express

Dec 21, 2009

I am working on moving a project from VB6 to VB 2008 Express. [Code] When I connect to mysql db in VB6, I connect using adAsynConnect. This way, I have a "Loading..." animation display while a connection is attempted. Is there a way to do this in 2008 Express? Or something similar I could read up on?

View 4 Replies

Async Sockets Client-server?

Jul 25, 2011

I'm using vb .net 2008 with 3.5 framework. (the following 2 classes are put in a DLL that i import in a main application)

Server class:

Code:
Imports System.Xml
Imports System.Net.Sockets
Imports System.Text
Imports System.Net

[code]....

Basically in my main application i am instantiating the client class from this dll, as well as the server one. On the client form i got a "send data" (SendFISA sub) and a "connect" button. After connecting the client and clicking on the "send data" i am sending to the server a string formed of some information created when instantiating the client class from the dll (i can't make that type of data public, but i'm just using a constructor to assign values to a class object, nothing special).

Now, when the server receives the data sent by "send data" button click it responds with "PKG_SENT" corresponding to the "FISASENT" string terminator(in the module) of the string that was sent. The "WRONG_MSG" constant is for when the string terminator is different from the 2 from above.

On the local disk, if the server cannot be reached, the client saves an xml file with this data. In the dll i made a timer that ticks every 5 seconds and checks to see if there is any xml file on the disk and sends it to the server if the connection is ok (trimiteXML sub). The server would respond with "XML_SENT" corresponding to the string terminator "XMLSENT"(in the module) appended to the read string from the xml file. After the file is sent it is deleted from the local dir.

The problem occurs when i click "send data" AND there are XML files to be sent from the disk. I believe they are interpreted by the server somehow at the same time because the message is neither "PKG_SENT" nor "XML_SENT", but it becomes a combination of the "WRONG_MSG_TYPE" and "XML_SENT", "XML_SENTG_TYPE". I have no idea why this is happening and i basically want the socket to wait with sending the files from the disks until the "send data" finishes.

View 9 Replies

Async Web Service Call Within Console App

Nov 24, 2009

I have a console app that is supposed to async call a web service...i don't get any errors but the completed handler doesn't seem to complete correctly. I have a subroutine with a loop that is supposed to fire off say 6 seperate calls to another routine that calls the service. The completed event handler routine puts data into the database..If i put a thread.sleep under the call to the routine calling the web service. The completed event handler fires but only one record is put in the database instead of the hundreds expected and only that one webservice call appears to fire.I should mention that this works as expected when calling the sync method of the web service within a console app and the asyc calls work as expected from a windows forms app. I just need it to work async within a console app..

View 1 Replies

Cannot Receive More Than 1 Response With Async Sockets

Jun 16, 2009

I'm trying to setup an asynchronous client and I can send/receive one command and response, but when I try to send the second command, the command is sent (I verified by code inspection), but I am getting no response.

Heres the code, a majority of it is from the Microsoft example.

Option Explicit On
Imports System
Imports System.Net

[Code].....

View 1 Replies

Clean Up App And Deal With The Async Calls?

Mar 16, 2010

My app is basically talking to a device via RS232. I need to 'listen' to the data received and do certain things based on it, update labels, etc. When I started, I only had a couple cross-thread calls and I used JMC's example to get by that. Now my app is bigger and I am calling the ThreadProcSafe all the time. I am hoping there is an easier way so I don't have to create sub routines for every control. Here's the relevant parts of my code.

Private Sub StepperSerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles StepperSerialPort.DataReceived
Dim ReceiveBuffer As String

[Code].....

View 4 Replies

Communications :: Async Sockets Reconnection?

Dec 17, 2008

Alright Simple question I got my main thread running Form Load and I make a socket then called BeginAcceptthen I pass AR to a Function that handles the Accept and creates a whole another sockets out of it and then loops for recv then when it gets everything closes then exits the functionbut how do I make the Socket from Form_Load run BeginAccept again to wait for another connection.. Do I call that command and Point it to the same Block thats Currently running, The block that Handles the AR Param??

View 3 Replies

Not Receiving All Packets - VB Sockets - Async?

Oct 27, 2009

Here's the code on the sending side of my sockets:

Private Sub Send(ByRef Buffer() As Byte)
Dim obj_StateObject As New StateObject
obj_StateObject.WorkSocket = m_tmpSocket

[code].....

View 1 Replies

Picturebox Async Load Not Complete?

Dec 12, 2011

i load the images in my project, using the load async method, but sometime, the picturebox doesn't show the image loaded, but only, the default not complete load image preview (red, blue and green geometric forms).

View 1 Replies

Socket Async Send / Receive

Jan 5, 2012

I coded a small tcp server/client software implementing the async methods of the socket object. In order to keep track and control of the transfered data on both sides, I decided to wrap the bytes to be transfered into small 65 000 bytes packets, sent one after each other, in varying order.In most cases, it works perfectly, and it is impressively fast. However, when I want to transfer larger data than my usual requests, say like a dozen megabytes, crippling problems start to appear, out of shit.Effectively, it seems that somehow when executed without pauses, the server sends the packets too fast, or that the client receives it too slowly. However, if I put the SendAsync method's thread to sleep for like 100ms before sendingthe next packet, everything arrives without problem on the client.[code]

View 1 Replies

Stuck Trying To Create An Async Method

Oct 8, 2010

For the last week I've been trying to create an async method. I tried the MSDN article How to: Implement a Component That Supports the Event-based Asynchronous Pattern but it does the work in a method in the same class. The work I'm doing is a lot more complicated and is its own class. The problem I'm having is the class doing the work cannot post the progress or completion as the methods that handle this stuff is in the parent class.

View 2 Replies

VS 2005 Async Ping Request

Jun 3, 2009

I am using a DataGrid object that updates every 2 seconds using a timer control.I am trying to have the ping request sent asynchronously to speed up the process. I'll explain what I am trying to do in the code in rem statements..[code]

View 6 Replies

Use Async CTP VB With SqlDataAdapter For A Select Statement And A DataSet

Oct 10, 2011

I want to know how to use the ASYNC CTP to manage the querying of a SQL Database with a SQL Select statement and use a SQLDataAdapter to fill in a DataSet. I have downloaded the VS2010 SP1 and the ASYNC CTP, I have also reviewed the videos on the MSDN site and the documentation included.

View 1 Replies

.net - Avoid Memory Leak On Async Timeout?

Sep 9, 2011

I'm trying to find a memory leak in my application and I want to know if I must (or should) call EndInvoke in case of a timeout. I'm calling a external system and I must have a way to stop waiting if execution is too long:

Dim ar As IAsyncResult
Try
ar = deleg.BeginInvoke(Nothing, New Object)
If Not ar.AsyncWaitHandle.WaitOne(getTimeout, False) Then

[code]....

View 1 Replies

C# - Interacting With The UI Thread From An Async Callback Method?

Nov 15, 2009

I have a method that is asynchronously called when System.Net.Sockets.NetworkStream.BeginRead completes.

skDelegate = New AsyncCallback(AddressOf skDataReceived)
skStream.BeginRead(skBuffer, 0, 100000, skDelegate, New Object)

In that callback method, I need to interact with the UI thread.

Sub skDataReceived(ByVal result As IAsyncResult)
CType(My.Application.OpenForms.Item("frmMain"), frmMain).refreshStats(d1, d2)
End Sub

This causes an exception after the method completes. (when End Sub is executed)

[Code]...

View 4 Replies

FtpWebRequest Async Giving File Lock?

Apr 29, 2012

i have written a small app that:-Periodically captures a portion of a screen to jpeg file-Uploads this file to ftp serverAt first i used My.Computer.Network.UploadFileand it worked like a charm. Then i wanted to move to FtpWebRequest async. The reason was because i implemented a global keyboard hook and locking the main thread for upload, disabled my keyboard.I copied the example in MSDN regarding FtpWebRequest. the problem i have is that it seems that the file that is being uploaded is locke

View 1 Replies

Multithreading - How To Handle Message Responses (ASync)

Jul 20, 2009

I'm writing an app that sends messages over a network to another PC that processes the message and sends back a reply (e.g. a boolean or some other data). My network communication subs (based on WCF) just give me the ability to send a message to the other PC and process any received messages. The issue is that any response received is processed by my designated network message event handler sub, but I have no way of feeding it back to the sub that called for the original message to be sent.

To quickly illustrate, the situation is something like this:
Private Function NetSendMessage(ByVal message As String) As Boolean
'Send network message to PC with device connected
'....
'returns true if message sent
End Function
[Code] .....

I was thinking along the lines of maybe add a NetSendReceive function that, say, MoveRightWheel could call; NetSendReceive would call NetSendMessage, monitor the message queue for its response (perhaps via a delegate/IAsyncResult), and then return the response message (to MoveRightWheel, which would be waiting for it).

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved