|
|
There are a vast number of Data Access Layer (DAL) samples around CP Search
However Pete's suggestion that you roll your own is absolutely valid, get some ideas from others but every competent developer should have written his own DAL. Start small and build on it as additional requirements arise.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I want to download same data from a web page. Using Excel webquery it works, while using C# it doesn't.
Here my code:
WebClient webClient = new WebClient();
a = "http://www.borsaitaliana.it/borsa/obbligazioni/mot/obbligazioni-in-euro/dati-completi.html?isin=DE000UB5WF78&lang=it";
string page = webClient.DownloadString(a);
System.Net.WebException: 'The remote server returned an error: (403) Forbidden.'
Have you any ideas?
Thanks for your time
|
|
|
|
|
|
I 'm trying to write audio to USB Modem via COM PORT. But it throwing me the error.
Can someone help. It really very important.
I am getting error in
waveSource_DataAvailable .
WaveIn waveSource;
waveSource = new WaveIn(this.Handle);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
.....
void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
portReceived.Write(e.Buffer, 0, e.BytesRecorded);
}
|
|
|
|
|
What kind of error? A compile-time error, a runtime exception? What did the error-text say?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
What error?
Is there a message?
What does the debugger show you?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Exception thrown: 'System.IO.IOException' in System.dll
An unhandled exception of type 'System.IO.IOException' occurred in System.dll
A device attached to the system is not functioning.
|
|
|
|
|
Any there is only one "device" in your question: the COM port connected USB modem.
So I'd start by using Hyperterminal or similar to find out why it isn't working, and what I needed to do to get it working.
Until you have reliable communications, there is no point writing code at all: it just adds complications and more "points of failure". Only ever start with code when you have baseline communications fully established and reliable.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks for reply.
I am trying to write a application which will communicate through AT Commands with USB Modem. I will make calls using AT Command and speak through mic and listen through Speaker. I have managed all things but I am not able to speak through mic. It gives error as above. Although I can hear what other end is saying very clearly. Please help...
|
|
|
|
|
As I said: start by connecting via Hyperterminal or similar, and use that to send AT commands: until you have established fully working communications, it doesn't matter if you code is right or wrong: it can't be tested until you know the link it relies upon works!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Yes I have tried. It's working.
|
|
|
|
|
I have figured out something. When the value of
e.BytesRecorded=1600
there is an exception. I need to convert microphone audio to 8Khz, 8 bit 1 channel so that it can be send to Port.
portReceived.Write(e.Buffer, 0, 960);
It works great but there too much echo and noise.
|
|
|
|
|
Assume that I have a pipe that consists of sections of variable radiuses and that I want to make calculations at different frequencies. So I want the calculations done at different threads for each frequency, but the list of pipe sections stays the same.
public class PipeSection
{
public PipeSection(double radius)
{
this.Radius = radius;
}
public double Radius { get; set; }
public double Calculate(double Frequency)
{
return Frequency * this.Radius;
}
}
To run the frequencies in parallel I use Rx:
CancellationTokenSource ctr = new CancellationTokenSource();
List<PipeSection> wn = new List<PipeSection>();
public MainWindow()
{
InitializeComponent();
PipeSection[] input = { new PipeSection(5), new PipeSection(6), new PipeSection(7) };
wn.AddRange(input);
var FreqData = new double[] { 0, 100, 2000, 3000 }.ToObservable<double>();
FreqData
.ObserveOn(Scheduler.Default)
.SelectMany(x => Task.Run<double>(() => DoCalcualtions(x,wn), ctr.Token))
.ObserveOnDispatcher()
.Subscribe(args => {
pgbProgress.Value += 1;
txtText.Text += Environment.NewLine + args.ToString();
},
() => {
txtText.Text += Environment.NewLine + "All done";
}
);
}
private double DoCalcualtions(double Freq, List<PipeSection> input)
{
double result=1;
for (int i = 0; i < input.Count(); i++)
result *= input[i].Calculate(Freq);
return result;
}
This does compile and run, but I have some questions regarding if the multithread calls:
1) Will I have to clone my list of Pipesections for each frequency (i.a. for each thread I want to run) ?
2) Can the DoCalcualtion function now be accessed in parallel or will I have to move it inside the PipeSection class to ensure this?
3) Is there a much better way of doing this?
-Kenneth
|
|
|
|
|
As long as your threads don't modify the data they're reading it's all perfectly thread-safe. No need to clone the data or to move DoCalcualtions . Moving DoCalcualtions into the PipeSection class wouldn't make a lot of sense anyway because it doesn't operate on a single PipeSection instance.
I assume in practice your input data set is a lot larger so threading is actually called for? There are of course alternative ways of doing this but I don't think there's an objectively better way. You're aware that you're ignoring the CancellationToken in DoCalcualtions ?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thank you for the answer.
I understand that they are thread safe, but what I was worried about was that there was just "one function" in memory, so when it was used no other threads could use the function, thereby making the multithreading single thread instead? (So I tried (and failed) to described a wrapper class that had several instances of the function in memory).
As for the reasoning behind using multithreading, the datasets itself are not so large, but I'm going to do some numerical integration inside DoCalcualtions that is very time-consuming. As for the non-use of
CancellationToken I just stripped the calculations to the bare minimum.
Kenneth
|
|
|
|
|
Kenneth Haugland wrote: what I was worried about was that there was just "one function" in memory, so when it was used no other threads could use the function, thereby making the multithreading single thread instead? No, there's no such thing as automatic method-entry-queuing. That's why there are synchronization objects. If you don't use any your threads will execute whatever "comes their way".
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
If I read you correctly, each thread will have its own function to use?
|
|
|
|
|
Code (instructions) only exist(s) once in memory and that's not a problem because it (usually) never changes. But each thread has its own stack for local variable allocation. When thread #2 enters DoCalcualtions while thread #1 is still executing DoCalcualtions they will have individual "instances" of the local variable result .
Ref: memory management - What and where are the stack and heap? - Stack Overflow[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I have to display thumbnails in a ListView control but the problem is that the control is locked until it finally loads and displays all thumbnails. Is there a simple way to display each thumbnail immediately after its loaded (in the same way Windows Exporer displays thumbnails in real time)?
This is the code for loading ListView with thumbnails:
void LoadListView()
{
int i = 0;
listView1.LargeImageList = imageList1;
foreach (string file in fileList)
{
Bitmap thumbnail = MakeThumbnail(file, 200, 200);
imageList1.Images.Add(thumbnail);
ListViewItem caption = new ListViewItem(Path.GetFileName(file));
caption.ImageIndex = i;
listView1.Items.Add(caption);
i++;
}
}
And this is the code for creating thumbnails with the correct aspect ratio:
Bitmap MakeThumbnail(string filename, int thumbWidth, int thumbHeight)
{
Bitmap thumbnail = null;
try
{
Bitmap bmp = new Bitmap(filename);
ImageFormat loFormat = bmp.RawFormat;
decimal ratio;
int newImgWidth = 0;
int newImgHeight = 0;
if (bmp.Width < thumbWidth && bmp.Height < thumbHeight) return bmp;
if (bmp.Width > bmp.Height)
{
ratio = (decimal)thumbWidth / bmp.Width;
newImgWidth = thumbWidth;
decimal temp = bmp.Height * ratio;
newImgHeight = (int)temp;
}
else
{
ratio = (decimal)thumbHeight / bmp.Height;
newImgHeight = thumbHeight;
decimal temp = bmp.Width * ratio;
newImgWidth = (int)temp;
}
thumbnail = new Bitmap(thumbWidth, thumbHeight);
Graphics g = Graphics.FromImage(thumbnail);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, thumbWidth, thumbHeight);
g.DrawImage(bmp,
(thumbWidth - newImgWidth) / 2,
(thumbHeight - newImgHeight) / 2,
newImgWidth, newImgHeight);
bmp.Dispose();
}
catch
{
return null;
}
return thumbnail;
}
modified 5-Nov-17 19:25pm.
|
|
|
|
|
The ListView control is not locked. There's no such concept as "locking" a control.
The problem is you have the UI (startup) thread doing all of the work of generating the thumbnails. Since the thread is tied up doing that work it can't get back to processing the message pump and handling all the WM_PAINT messages that are stacking up.
The solution is to move the thumbnail generation to a background thread. Look into using a BackgroundWorker to do this.
|
|
|
|
|
My bad. I meant the form is locked: it literally freezes when there are many thumbnails to process. Whoever did this ListView design at Microsoft did a bad job if it doesn't work smoothly out of the box.
|
|
|
|
|
The Form is just another control and it's bound by the same limitations OF YOUR CODE. You're bogging down the UI thread with the thumbnail generation. It has NOTHING AT ALL to do with the design of the ListView control. ALL controls and the form will not respond to anything until your method that generates the thumbnails is done and returns.
You have no choice but to move the "long running code" to a background thread. This frees up the UI thread to do all of the painting and response to mouse moves, clicks, keyboard, ...
This is how Windows works. It has nothing to do with the controls, the language your using, the type of app you're writing, ...
|
|
|
|
|
I added a BackgroundWorker to my project, but it doesn't work at all.
I started the BackgroundWorker with:
backgroundWorker1.RunWorkerAsync();
Then I called the function LoadListView():
void BackgroundWorker1DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
LoadListView();
}
|
|
|
|
|
You can't touch a UI control from a background thread. The worker should ONLY generate the thumbnail and return the resulting image. The code on the UI thread should get that image back from the background worker and update the ListView with it.
|
|
|
|