|
Thanks, but those articles assume that the non-Form thread knows about the Form, and call Form methods.
In my case, this library object does NOT know about the Form, and therefore cannot use Invoke to call Form methods. This is by design - the DataTable in the library object has no idea who is connecting to its DataSource, nor should it.
So there is no clear place where Invoke can be used, that I can tell. On the DataGridView's side, the RowsAdded event is simply not firing, because it's on the wrong thread. So the DGV is not getting updated.
I think it comes down to this: should I redesign the library object so that it takes a delegate to call after Rows.Add (seriously ugly)? Or should I not expose the DataTable directly, and look to find another method of making the DataTable's content available?
|
|
|
|
|
JoeRip wrote: I think it comes down to this: should I redesign the library object so that it takes a delegate to call after Rows.Add (seriously ugly)? Or should I not expose the DataTable directly, and look to find another method of making the DataTable's content available?
Nothing "ugly" about a CallBack method; that's the way to go if you don't want to be passing a pointer to your form to the thread.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Okay, here is a very simplified version of the problem. Note that the GrowingTable() class is normally in a separate assembly and namespace.
So - I can't just convert the Timer to a FormTimer, unless I pass the form itself to the GrowingTable class, which would be incredibly ugly.
And there's no clear place where I can Invoke, as there is no actual update function (that is hidden in the DataSource mechanism).
using System;
using System.Windows.Forms;
namespace dgv
{
public partial class Form1 : Form
{
DataGridView dgvMain = new DataGridView();
GrowingTable ra = new GrowingTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Controls.Add(dgvMain);
dgvMain.Dock = System.Windows.Forms.DockStyle.Fill;
dgvMain.Location = new System.Drawing.Point(0, 45);
dgvMain.DataSource = ra.itemTable;
ra.Start();
}
public class GrowingTable
{
public System.Data.DataTable itemTable;
private System.Threading.Timer myTimer = null;
public GrowingTable()
{
itemTable = new System.Data.DataTable();
myTimer = new System.Threading.Timer(this.AddRow, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
for (int i = 0; i < 11; i++)
{
itemTable.Columns.Add(new System.Data.DataColumn(i.ToString(), typeof(System.DateTime)));
}
}
public void Start()
{
myTimer.Change(0, 500);
}
private void AddRow(object state)
{
if (itemTable.Rows.Count < 50)
{
System.Data.DataRow dr = this.itemTable.NewRow();
for (int i = 0; i < 11; i++) { dr[i.ToString()] = DateTime.Now; }
this.itemTable.Rows.Add(dr);
}
else
{
myTimer.Dispose();
myTimer = null;
}
}
}
}
}
|
|
|
|
|
add this to Growing Table
public Action<Action> InvokeOnUI{get;set;}
in the AddRow method replace
this.itemTable.Rows.Add(dr);
with
if (InvokeOnUI!=null)
InvokeOnUI(()=>this.itemTable.Rows.Add(dr));
else
this.itemTable.Rows.Add(dr);
and don't forget to set it up using:
ra.InvokeOnUI = action => Invoke(action);
the sample i made is as follows if it helps
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Updater ud = new Updater();
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = ud.Data;
ud.InvokeOnIU = action => Invoke(action);
}
private void button1_Click(object sender, EventArgs e)
{
Thread run = new Thread(ud.AddInBackGround);
run.Start();
}
}
public class Updater
{
public Updater()
{
Data = new DataTable();
Data.Columns.Add("Column 1");
Data.Columns.Add("Column 2");
Data.Columns.Add("Column 3");
}
public Action<Action> InvokeOnIU { get; set; }
public DataTable Data { get; set; }
public void AddInBackGround()
{
if (InvokeOnIU!=null)
{
var toAdd = Data.NewRow();
InvokeOnIU(()=>Data.Rows.Add(toAdd));
}
}
}
|
|
|
|
|
+5 for moving the dependency to a separate class, decoupling the form from the thread
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks! Very nice.
This is very similar to what I eventually settled on. Since GrowingTable() is actually in another (library) assembly completely, it can't have intimate knowledge of the UI's Form methods. And in fact, it may not be called from a Form at all.
So, I simply added an optional callBack delegate parameter to GrowingTables constructor, and I let the caller pass in a callBack function, to be called on each AddRow. That way, the caller can check NeedsInvoke on his side, and invoke the callBack over to the UI thread if necessary.
It's still ugly, but it's doable. I think the real solution is to not expose a DataTable directly, and handle the binding directly.
Actually, looking closer at what you've done here... this might be cleaner. Will play with it tomorrow.
Thanks!
|
|
|
|
|
I think this is not general enough for what I need. GrowingTable updates its table at arbitrary times. The caller (form) can't know when to call InvokeOnIU. If I understand the code above correctly, the form is only calling InvokeOnIU when it wants to.
How about this: GrowingTable will have a property, method and event:
bool fCallerNeedsUpdateOnItsOwnContext
void UpdateGTDataSource(GTUpdates updates)
event UpdateDataAgainstYourOwnContext
When GrowingTable needs to update its data, it will create a queue of updates (typically, rows to add, rows to replace other rows by id, or id's of rows to be deleted). If fCallerNeedsUpdateOnItsOwnContext is true, it will raise the UpdateDataAgainstYourOwnContext event, passing the queue of updates. Then the subscriber can call or Invoke GT.UpdateGTDataSource(updates) as it needs, to insure thread context safety.
If fCallerNeedsUpdateOnItsOwnContext is false, GrowingTable will simply execute the queue of updates itself.
What say you, coders?
|
|
|
|
|
the code sample i gave you puts the class in charge of refreshing the data in total control, the one call made in the UI is just letting the GrowingTable class know how to perform tasks in sync with the UI thread. after that stage has been performed the UI has no more involvement.
I'm glad you have found a solutino but the assumptin that the form is callint InvokeOnUI is not quite right.
|
|
|
|
|
Well, it works. Not sure about design quality, but it was better than the hacks I was using before. Thanks for all the help! Let me know if it sucks. Here's a sample which demonstrates the pattern; first the caller/form:
using System;
using System.Windows.Forms;
using GrowingDataTable;
namespace dgv
{
public partial class Form1 : Form
{
public DataGridView dgvMain;
public delegate void delEventHandler(object sender,
GrowingDataTable.UpdateDataTableOnYourThreadContextEventArgs e);
public GrowingDataTable gt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dgvMain = new DataGridView();
Controls.Add(dgvMain);
dgvMain.Dock = System.Windows.Forms.DockStyle.Fill;
gt = new GrowingDataTable();
gt.RaiseUpdateTableAgainstYourThreadContextEvent += new
EventHandler<GrowingDataTable.UpdateDataTableOnYourThreadContextEventArgs>
(gt_RaiseUpdateTableAgainstYourThreadContextEvent);
gt.UpdateDataTableOnCallerThreadContext = true;
dgvMain.DataSource = gt.itemTable;
gt.Start(3000, 1000);
}
void gt_RaiseUpdateTableAgainstYourThreadContextEvent(object sender,
GrowingDataTable.UpdateDataTableOnYourThreadContextEventArgs e)
{
if (InvokeRequired) {
Invoke(new
delEventHandler(gt_RaiseUpdateTableAgainstYourThreadContextEvent),
new object[] { sender, e}); }
else
{
if ((e.Updates != null) || (e.Updates.Count > 0))
{
gt.UpdateTableOnMyThreadContext(e.Updates);
}
}
}
}
}
Then the library class GrowingDataTable:
using System;
using System.Collections.Concurrent;
namespace GrowingDataTable
{
public class GrowingDataTable
{
public System.Data.DataTable itemTable;
public System.Threading.Timer myTimer = null;
private ConcurrentQueue<System.Data.DataRow> qUpdates = new
ConcurrentQueue<System.Data.DataRow>();
public GrowingDataTable()
{
itemTable = new System.Data.DataTable();
myTimer = new System.Threading.Timer(this.AddRows, null,
System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
for (int i = 0; i < 10; i++) {
itemTable.Columns.Add(new System.Data.DataColumn
(i.ToString(), typeof(System.DateTime))); }
}
public bool UpdateDataTableOnCallerThreadContext { get; set; }
public class UpdateDataTableOnYourThreadContextEventArgs : EventArgs
{
private ConcurrentQueue<System.Data.DataRow> qu;
public UpdateDataTableOnYourThreadContextEventArgs
(ConcurrentQueue<System.Data.DataRow> Qu)
{
qu = Qu;
}
public ConcurrentQueue<System.Data.DataRow> Updates
{
get { return qu; }
}
}
public event EventHandler<UpdateDataTableOnYourThreadContextEventArgs>
RaiseUpdateTableAgainstYourThreadContextEvent;
public void UpdateTableOnMyThreadContext(
ConcurrentQueue<System.Data.DataRow> Q)
{
System.Data.DataRow dr = null;
while (Q.TryDequeue(out dr))
{
if (dr != null)
{
itemTable.Rows.Add(dr);
}
}
}
public void Start(int delay, int interval)
{
myTimer.Change(delay, interval);
}
private void AddRows(object state)
{
if (itemTable.Rows.Count < 10)
{
System.Data.DataRow dr = this.itemTable.NewRow();
for (int i = 0; i < 11; i++) { dr[i.ToString()] = DateTime.Now; }
if (UpdateDataTableOnCallerThreadContext)
{
qUpdates.Enqueue(dr);
OnRaiseUpdateDataTableAgainstYourThreadContextEvent(new
UpdateDataTableOnYourThreadContextEventArgs(qUpdates));
}
else
{
this.itemTable.Rows.Add(dr);
}
}
else
{
myTimer.Dispose();
myTimer = null;
}
}
protected virtual void OnRaiseUpdateDataTableAgainstYourThreadContextEvent(
UpdateDataTableOnYourThreadContextEventArgs q)
{
EventHandler<UpdateDataTableOnYourThreadContextEventArgs> handler =
RaiseUpdateTableAgainstYourThreadContextEvent;
if (handler != null)
{
handler(this, q);
}
}
}
}
|
|
|
|
|
Hello Friends,,
well this is the situation im trying to simulate Keys but only when a proccess is actually running
this is what i have
Private Sub letter_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles letter.Tick
Dim rprocess() As System.Diagnostics.Process = Process.GetProcessesByName("Test.exe")
If System.IO.File.Exists("C:\test\Listener.txt") Then
Else
System.IO.File.Create("C:\Test\Listener.txt")
End If
If rprocess.Length = 1 Then
ElseIf (GetAsyncKeyState(Keys.A)) Then
Dim ReadIt As String = System.IO.File.ReadAllText("C:\Test\Listener.txt")
System.IO.File.WriteAllText("C:\Test\Listener.txt", ReadIt & "A")
End If
End Sub/pre>
but is stil reading the "A" even if the proccess is off or not present .. HELP !!
|
|
|
|
|
SORRY WORLD .. found the problem =)
if anyone tries then is
if rprocess.length = 1 then
getasynckeystate(keys.a)
remove elseif and the "()"
|
|
|
|
|
Hi Everyone,
I did a search on capturing screen shots in vb.net and I was wondering if there is a straight forward way to capture the active window? Or do I need to first get the handle of the active window, get the position and size and then capture it? I do not want to use any type of sendkeys method or use the clipboard. I want to save it to jpg format.
Thanks!
Done so far but its capturing the entire window is
Module patternAload
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Integer
Const SRCCOPY As Integer = &HCC0020
Private oBackground As Bitmap
Private FW, FH As Integer
Public Sub CaptureScreen()
Dim hSDC, hMDC As Integer
Dim hBMP, hBMPOld As Integer
Dim r As Integer
Dim fgw As Long = GetForegroundWindow()
hSDC = CreateDC("DISPLAY", "", "", "")
hMDC = CreateCompatibleDC(hSDC)
FW = GetDeviceCaps(hSDC, 8)
FH = GetDeviceCaps(hSDC, 10)
hBMP = CreateCompatibleBitmap(hSDC, FW, FH)
hBMPOld = SelectObject(hMDC, hBMP)
r = BitBlt(hMDC, 40, 40, FW, FH, hSDC, 0, 0, SRCCOPY)
hBMP = SelectObject(hMDC, hBMPOld)
r = DeleteDC(hSDC)
r = DeleteDC(hMDC)
oBackground = Image.FromHbitmap(New IntPtr(hBMP))
DeleteObject(hBMP)
End Sub
|
|
|
|
|
I suspect that the following code should help:
<DllImport("user32.dll")> _
Public Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll", ExactSpelling := True, CharSet := CharSet.Auto)> _
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Public Shared Sub CaptureWindow(fileName As String, format As ImageFormat)
Dim hWnd As IntPtr = GetForegroundWindow()
If hWnd = IntPtr.Zero Then
Return
End If
Dim srcRect As RECT
If Not GetWindowRect(hWnd, srcRect) Then
Return
End If
Dim width As Integer = srcRect.Right - srcRect.Left
Dim height As Integer = srcRect.Bottom - srcRect.Top
Using bmp As New Bitmap(width, height)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(srcRect.Left, srcRect.Top, 0, 0, New Size(width, height), CopyPixelOperation.SourceCopy)
bmp.Save(fileName, format)
End Using
End Using
End Sub If you want to do it in C#, the same code would look like this:
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static void CaptureWindow(string fileName, ImageFormat format)
{
IntPtr hWnd = GetForegroundWindow();
if (hWnd == IntPtr.Zero) return;
RECT srcRect;
if (!GetWindowRect(hWnd, out srcRect)) return;
int width = srcRect.Right - srcRect.Left;
int height = srcRect.Bottom - srcRect.Top;
using (Bitmap bmp = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(srcRect.Left, srcRect.Top, 0, 0,
new Size(width, height), CopyPixelOperation.SourceCopy);
bmp.Save(fileName, format);
}
}
}
|
|
|
|
|
OK i will retry with this code lets see what happens ..
Thanks
|
|
|
|
|
|
i got confused sorry ... so the public sub to save the file will be ???
i try Private Sub Form1_load()
CaptureWindow(fileName:="C:\test.jpg", format:="")
bmp.Save("C:\test.jpg")
End Sub
and its not working
|
|
|
|
|
hey my bad im sorry =)
i got it
bmp.Save("test.bmp", jpeg)
i did change it and it wasnt working i had to put
bmp.save("c:\test.bmp", system.drawing.imaging.ImageFormat.Jpeg)
and on the click
CaptureWindow("C:\test.bmp", Jpeg)
that was it
Thanks
|
|
|
|
|
Sweet. Not too bad when you consider I just knocked that up in Notepad. I'm glad it's working for you.
|
|
|
|
|
hehe Thanks a lot man
i have another question its killing me lol ima post the new topic =) is different lol .. thanks a gain man
|
|
|
|
|
I have an application that uses Comparer class to compare the objects. In 4.0 framework, the function public int Compare(object a, object b) took much time than 2.0. Please help me to resolve this issue!!!
modified 8-Aug-12 8:32am.
|
|
|
|
|
Could you show us the code you were using here? That might help to narrow things down.
|
|
|
|
|
Thanks for your reply. When i change the Target FrameWork from 4 to 2 in Project Properties in VS 10, it took less time.
Please find the below code snippet:
public static int CompareKeyColumn(DataRow drSource, DataRow drTarget, ArrayList arrForeignKey)
{
int iComareVal = 0;
string strColumnName = "";
Comparer cRow = new Comparer(new CultureInfo("es-ES", false));
for (int j = 0; j < arrForeignKey.Count; j++)
{
strColumnName = arrForeignKey[j].ToString();
if (drSource[strColumnName] != DBNull.Value && drTarget[strColumnName] != DBNull.Value)
{
iComareVal = cRow.Compare(drSource[strColumnName], drTarget[strColumnName]);
}
else if (drSource[strColumnName] == DBNull.Value)
iComareVal = -1;
else if (drTarget[strColumnName] == DBNull.Value)
iComareVal = 1;
if (iComareVal > 0)
return 1;
else if (iComareVal < 0)
return -1;
}
return 0;
}
|
|
|
|
|
My suspicion is that the problem actually lies with the culture. This is what happens inside Compare:
public int Compare(object a, object b)
{
if (a == b)
{
return 0;
}
if (a == null)
{
return -1;
}
if (b == null)
{
return 1;
}
if (this.m_compareInfo != null)
{
string str = a as string;
string str2 = b as string;
if ((str != null) && (str2 != null))
{
return this.m_compareInfo.Compare(str, str2);
}
}
IComparable comparable = a as IComparable;
if (comparable == null)
{
throw new ArgumentException(Environment.GetResourceString("Argument_ImplementIComparable"));
}
return comparable.CompareTo(b);
} To test this hypothesis out, try dropping the CultureInfo out and see what happens. (The m_compareInfo.Compare part is the test that occurs when you set the CultureInfo ).
|
|
|
|
|
Sorry! I didnt get your answer. How to drop the CultureInfo? May i use Comparer.Default.Compare(object a, object b) function?
|
|
|
|
|
To test it, change
Comparer cRow = new Comparer(new CultureInfo("es-ES", false)); to
Comparer cRow = new Comparer();
|
|
|
|
|