Windows Develop Bookmark and Share   
 index > Windows Forms General > Killing a thread initiated by BeginInvoke()
 

Killing a thread initiated by BeginInvoke()

Hi:

I have the following:

private delegate void CallAsyncWorkerDelegate();

private void CallAsyncWorker()

{

byte[] buffer = new byte[1518];

this.DoRead(buffer); // Blocking call...

}

and the above is called when the user press a button:

private void btReceive_Click(object sender, EventArgs e)

{

// Start reception on another thread.

CallAsyncWorkerDelegate doWork = new CallAsyncWorkerDelegate(CallAsyncWorker);

doWork.BeginInvoke(null, null);

}

Now, as the DoRead() method is a blocking call, sometimes I would like to kill the started thread initiated with the CallAsyncWorker, so the blocking DoRead() call can me terminated. How can I make this?

Thanks;

Gus

GusSabina  Tuesday, July 15, 2008 6:55 PM
Which would be a good example of a disadvantage of a threadpool thread. See your other post.
nobugz  Tuesday, July 15, 2008 8:16 PM

Ok. Let me try to explain what I need to do.

I 'm using CreateFile, ReadFile, WriteFile to send and receive packets over a communication channel (that is, using the device driver as a file). The problem I have is when I call ReadFile, it blocksthe program until a packet is received. So, I 'm using BeginInvoke in order to have this working on a separate thread and not block the main thread where the UI is working.

The problem is that, while ReadFile is waiting for a packet, I can not use WriteFile to send a packet (becausethe device driver is blocked by the ReadFile function). So I would try to kill the thread where ReadFile is running, so I cansend a packet, and then call the ReadFile again until a response is received.

Another option (and the best one)would be using ReadFile and WriteFile asynchronously, but I did not find information enought in order to make it.

Last chance, would be opening two handlers (one for receive and other for send), but the device driverdoes not accept this...

Any idea?

Regards;

Gus

GusSabina  Tuesday, July 15, 2008 9:13 PM
You are talking about the unmanaged API functions to work with a device. Using them asynchronously involves bying into overlapped file I/O with the OVERLAPPED structure. That's sort of code is best written in C/C++, P/Invoking them is a major headache. On top of which is that canceling such I/O requests involves jumping through many delicate hoops as well.

Is there a compelling reason not to use managed classes to accomplish what you want to do? The System.Net.Sockets namespace has everything you could possibly need.
nobugz  Tuesday, July 15, 2008 10:13 PM

I need to send andreceivepackets over ethernet. Sockets involve IP configuration which I need to avoid.

Do you know any other way to work with raw ethernet data using managed code?

Thanks and regards;

Gus

GusSabina  Tuesday, July 15, 2008 10:31 PM

You can use google to search for other answers

Custom Search

More Threads

• Label Font.Size
• Urgent...Problem in Mozilla and IE... (W2K)
• Customize Toolstrip problem!
• Icon Colors
• Display Updated Windows Form
• How do you use SetUnhandledExceptionMode()
• RichTextBox question
• Canceling TreeNode editing on TreeView Control
• Memory Leak using FlowLayoutPanel?
• Where does visual studio load images/icons from?