Thanks Walden.
Even after using 2 different threads, im still having some problems.
Especially if I use the receive data queue in one thread ("data =
sc.FromBytes(DQRead.Read(okey, cwbx.cwbdqSearchOrderEnum.cwbdqEqual, -1))")
and a Microsoft web browser control (axshdocvw.axwebbrowser) on a form.
I can navigate the web page while the data queue reader is waiting on an
entry. If I remove the web browser control, the program works great. Maybe a
bug or something with MS control? Not sure.
Tim
-----Original Message-----
From: systemidotnet-bounces@xxxxxxxxxxxx
[mailto:systemidotnet-bounces@xxxxxxxxxxxx] On Behalf Of Walden H. Leverich
Sent: Friday, October 31, 2008 10:06 AM
To: .net use with the System i
Subject: Re: [SystemiDotNet] vb .net and dataqueue question
Tim,
What you want to do is move the receive of the dataq info onto another
thread. Assuming you want to actually do something with the data on the
foreground (winforms) thread then you should receive the data on the
background thread and "post" it into the winform and then do with it
what you want.
Here's some hacked together code to get you started. I want to note a
couple of things:
1) The form has a button called button1, and a listbox called listbox1.
2) I explicitly set the background thread to be a STA thread because you
will be (I assume) invoking a COM object to do the read of the dataq and
COM must be called from a STA thread.
3) It's c#, but you can translate it online if you can't read it.
http://www.carlosag.net/Tools/CodeTranslator/ for example.
4) It's _not_ production ready. There is no error handling, and no
logging. I strongly recommend you add lots of log4net code in here.
Multithreaded apps are commonly referred to as debug-proof. That is,
highly resistant to all forms of debugging. :-)
That being said:
----------------
using System;
using System.Windows.Forms;
using System.Threading;
namespace WinForm1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ParameterizedThreadStart pts = new
ParameterizedThreadStart(Background.DoLongProcess);
Thread getDataThread = new Thread(pts);
getDataThread.Name = "DataQGet";
//Go below normal, not time critical.
getDataThread.Priority =
ThreadPriority.BelowNormal;
//Auto-kill thread when foreground threads dies
getDataThread.IsBackground = true;
//Will use COM, go STA!
getDataThread.SetApartmentState(ApartmentState.STA);
//And we're off...
getDataThread.Start(this);
}
public void GotData(string TheData)
{
//We add to listbox here. Do what you will.
//Remember, you're now on the UI thread!
listBox1.Items.Add(TheData);
}
}
class Background
{
static public void DoLongProcess(object StateInfo)
{
Form1 theForm = (Form1)StateInfo;
//Repeat forever
while (true)
{
//This is the 'long process'
//You'd read the dataq here
System.Threading.Thread.Sleep(5000);
string theData =
DateTime.Now.ToString();
//Update the form w/the data from the
long process
//Remember, to update the UI from
another thread we must invoke() the update
if (theForm.InvokeRequired)
theForm.Invoke((MethodInvoker)delegate { theForm.GotData("Invoked: " +
theData); });
else
theForm.GotData(theData);
}
}
}
}
----------------
As an Amazon Associate we earn from qualifying purchases.