|
Hi,
I'm not very knowledgeable on threading...
I'm using Invoke to receive Serial Port data. The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form?
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
serialPortData = serialPort1.ReadExisting();
this.Invoke(new EventHandler(SerialPortEvent));
}
thanks,
Ron
|
|
|
|
|
myNameIsRon wrote: The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form?
If you have any active threads, it good to stop them while you close the form. You can do this in the Dispose method of your form.
|
|
|
|
|
Thanks Navaneeth,
What code would I use to stop an "this.Invoke()" thread?
Ron
|
|
|
|
|
If you use myThread.IsBackground = true; the thread will close automatically after this.Close()
|
|
|
|
|
Hi Mo,
I'm using "this.Invoke()". Should I be using a different type of thread? If not, how do I stop "this.Invoke()"?
Ron
|
|
|
|
|
Hey Ron.
I'm using IsBackground property for closing Thread with closing form.
myNameIsRon wrote: I'm using "this.Invoke()". Should I be using a different type of thread? If not, how do I stop "this.Invoke()"?
I don't know exactly.
|
|
|
|
|
Control.Invoke puts a serialised delegate onto the windows message loop.
How much work are you doing in your SerialPortEvent method? If you are doing enough work to block the UI up then you are probably blocking the serial library as well.
Consider taking data in the DataRecieved event, placing it on a queue, and perhaps Monitor.Pulse a background thread and returning out the event handler immediately to return control to the serial port code. Then your background thread can consume the data events that are on the queue, and process them. Then when you want to make UI changes, you either use Form.Invoke, or .BeginInvoke to make the required changes.
|
|
|
|
|
Thanks Mark,
The SerialPortEvent calls on a Method to parse the c/c swipe data. Is Method run through the Invoke thread, or the main thread?
private void SerialPortEvent(object sender, EventArgs e)
{
SwipeMethod(serialPortData);
}
Also, if I use ShowDialog() (instead of Show()) to open the Form, there is no issue with using this.Close().
Ron
|
|
|
|
|
SerialPortEvent is executed on the UI thread. So SwipeMethod will run on that same thread.
Not sure on the ShowDialog vs Show deal... but I haven't had very much coffee today
My worker thread/queue setup was based on the assumption you were processing bunches of data from a serial port. If you are popping a form with "Swipe CC now" then it might be worth looking at just using Delegate.BeginInvoke and IAsyncResult. This will use a thread from the pool.
|
|
|
|
|
Yes, I've been playing around some. I'm not sure if it's a bug in .NET 2.0. After the transaction is approved, I want to exit the Form with "this.Close()" but it locks the app. If I switch to BeginInvoke it works ok. Also, I can use Invoke, if I open the form with ShowDialog instead of Show.
Useful link:
avoid invoke
Ron
|
|
|
|
|
hi guys,
i have developed an application that will convert every image file in selected folder to .png and scale images to some specified sizes with checking which one is best. Those sizes are 64 128 256 512 1024 2048. The app checks which is nearest size to scale. For instance, if image dimension is (65 x 125) then it will scale it to 64 x 128 because 65 is nearest to 64 than 128 and 125 is nearest to 128 than 64 and so on...so to this i have coded the app like this :
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
Xal.Clear();
getDirs(fbd.SelectedPath);
string[] tmp_filePaths = (string[])Xal.ToArray(typeof(string));
for (int a = 0; a < tmp_filePaths.Length; a++)
{
if (!tmp_filePaths[a].EndsWith(".txt"))
{
Image tmp_image = Image.FromFile(tmp_filePaths[a]);
int newwidth = roundToNearestPixel(tmp_image.Width);
int newheight = roundToNearestPixel(tmp_image.Height);
Bitmap tmp_bmp = new Bitmap(newwidth, newheight);
Graphics tmp_gra = Graphics.FromImage(tmp_bmp);
tmp_gra.DrawImage(tmp_image, 0, 0, newwidth, newheight);
tmp_bmp.Save(@"D:\texts\" + Path.GetFileNameWithoutExtension(tmp_filePaths[a]) + ".png", System.Drawing.Imaging.ImageFormat.Png);
tmp_gra.Dispose();
}
}
}
}
short[] dimensions = new short[] { 64, 128, 256, 512, 1024, 2048, 4096 };
int roundToNearestPixel(int value)
{
int result;
if (value <= 64)
{
result = 64;
return result;
}
int pointer = 0;
for (int a = 0; a < dimensions.Length; a++)
{
if (dimensions[a] >= value)
{
pointer = a;
break;
}
}
int shortdistance = value - dimensions[pointer - 1];
int largedistance = dimensions[pointer] - value;
if (shortdistance < largedistance)
result = dimensions[pointer - 1];
else
result = dimensions[pointer];
return result;
}
now its doing what i needed but it gives an error as well that is out of memory after every 174 images, i have commented that line. So can anybody tell me how to solve this ?
thanks
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Image,Graphics and Bitmap has a Dispose method which has to be called after using. GDI classes are making unmanaged code extensively. Wrap above said classes usage into using blocks which ensures Dispose call.
if (!tmp_filePaths[a].EndsWith(".txt"))
{
using (Image tmp_image = Image.FromFile(tmp_filePaths[a]))
{
int newwidth = roundToNearestPixel(tmp_image.Width);
int newheight = roundToNearestPixel(tmp_image.Height);
using (Bitmap tmp_bmp = new Bitmap(newwidth, newheight))
using (Graphics tmp_gra = Graphics.FromImage(tmp_bmp))
{
tmp_gra.DrawImage(tmp_image, 0, 0, newwidth, newheight);
tmp_bmp.Save(@"D:\texts\" + Path.GetFileNameWithoutExtension(tmp_filePaths[a]) +
".png", System.Drawing.Imaging.ImageFormat.Png);
}
}
} If the image you are loading is too big, I doubt Image class can handle it.
Xmen wrote: Xal.Clear(); // ArrayList
Use a generic List<string>, so you avoid casting.
|
|
|
|
|
oh, i thought just Graphics need to be dispose. No images arnt too big, it will be rare case if anyone need 4096 size
problem persists even after apply using
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Well, try loading the file into a Stream and use Image.FromStream method. Documentation for Image.FromFile says, it throws OutofMemoryException only when
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file. . Make sure your file won't come in any of these categories.
|
|
|
|
|
HAHAHAHA, damn one of them file was a bullshit, its a hml file that is giving error.
thanks man
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Xmen wrote: its a hml file
You have to check the file extension before loading.
|
|
|
|
|
yes i have added that one with txt
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN%
Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
--------------------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Ha
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 alpha 4a out now (29 May 2008) ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Hi all,
i got question here need some information. i'm new in in programming.
could anyone help.
<br />
#include <stdio.h><br />
int main()<br />
{<br />
int a,b,c:<br />
a=5;<br />
b=7;<br />
c=a+b;<br />
printf("%d+%d=%d\n",c,a,b);<br />
return 0;<br />
}<br />
</stdio.h>
Can, anyone help me go through with this. Thanks. in my mind, the answer display on the screen should be "12+5=17". i'm not sure is that correct. Thanks all.
|
|
|
|
|
A good place to start is on the correct message board:
Visual C++[^]
No, the code is not correct. The order of arguments passed to printf()
will yield
12+5=7
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
can you explain why it has to be 12+5=7?
|
|
|
|
|
Looking at your question again, I see what you're getting at...
The printf() function just inserts the variables (converted to
string form) into the formatting placeholder spots. No arithmetic
is done.
So substituting 12, 5, and 7 in "%d+%d=%d" yields "12+5=7".
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Member 4099222 wrote: Thanks. in my mind, the answer display on the screen should be "12+5=17".
This displays 12 + 5 = 7. To get 12+5=17, try printf("%d+%d=%d\n",c,a,c + a);
|
|
|
|
|
Don't you want 5+7=12?
Which would be:
printf("%d+%d=%d\n",a,b,c);
You need to pass the parameters in the order they are in the format.
printf("%d+%d=%d\n",a,b,c);
1 2 3 1 2 3
See it?
[edit] formatting got screwed up [/edit]
- S
50 cups of coffee and you know it's on!
A post a day, keeps the white coats away!
|
|
|
|
|
I need to save orders locally in an XML file and then when connection is available I need to save them to SQL DB. I have built classes for Order and OrderCollection. What is the best way to save them to an XML file so I can later save them to DB? I am thinking of saving them to a DataSet and calling the WriteXml method on it to write to xml file. Afterwards, I can call ReadXml and then write everything to DB. Is this Ok?
Also, do I have to load all orders from file to a DataSet and add a new order and then re-write it or is there another alternative?
|
|
|
|